Redis Cheat Sheet

Comprehensive Redis commands for redis-cli, key lifecycle, strings, expiration, and core operational workflows.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Connecting and basic redis-cli use

Connect, authenticate, select databases, and inspect the server.

Connect to local Redis

Start an interactive session against localhost:6379.

bashANYredisredis-cliconnect
bash
redis-cli
Notes

Use this when Redis is running locally with default host and port.

Connect to a remote host

Connect to a remote Redis server.

bashANYredisredis-cliconnect
bash
redis-cli -h redis.example.com -p 6379

Connect with password

Authenticate during connect.

bashANYredisauthredis-cli
bash
redis-cli -h redis.example.com -p 6379 -a 'your-password'

Connect using a Redis URL

Use a single connection string, including DB number.

bashANYredisurlredis-cli
bash
redis-cli -u redis://:password@redis.example.com:6379/0

Connect over TLS

Connect securely to a TLS-enabled Redis endpoint.

bashANYredistlsredis-cli
bash
redis-cli -h redis.example.com -p 6380 --tls --cacert ca.pem

Ping the server

Basic liveness check; returns PONG when reachable.

bashANYredispinghealth
bash
PING
Notes

Run inside redis-cli interactive mode, or prefix it with `redis-cli` on the shell.

Authenticate after connecting

Authenticate the current connection.

bashANYredisauth
bash
AUTH your-password

Select logical database

Switch to a different logical database index.

bashANYredisdatabaseselect
bash
SELECT 1

Echo a string

Round-trip a string to test command execution.

bashANYredisecho
bash
ECHO 'hello redis'

Negotiate protocol

Switch to RESP3 and get connection metadata.

bashANYredisresp3hello
bash
HELLO 3

List clients

Inspect connected clients.

bashANYredisclientadmin
bash
CLIENT LIST

Show server info

Return server statistics and configuration info.

bashANYredisinfoadmin
bash
INFO

Show memory info

Inspect memory usage and allocator metrics.

bashANYredisinfomemory
bash
INFO memory

Show replication info

Check role, replicas, and sync state.

bashANYredisinforeplication
bash
INFO replication

Count keys in current DB

Return the number of keys in the selected database.

bashANYredisdbsize
bash
DBSIZE

Get memory statistics

Inspect memory allocator and overhead statistics.

bashANYredismemorystats
bash
MEMORY STATS

Inspect command docs

Ask Redis for built-in command documentation.

bashANYrediscommanddocs
bash
COMMAND DOCS GET SET HGETALL

Keys and expiration

Key lookup, iteration, TTLs, rename, and lifecycle operations.

Check if key exists

Return how many specified keys exist.

bashANYrediskeyexists
bash
EXISTS user:1

Delete keys

Delete one or more keys.

bashANYredisdelkey
bash
DEL cache:page:1 cache:page:2

Inspect key type

Return the Redis type stored at a key.

bashANYredistypeinspect
bash
TYPE session:123

List keys by pattern

Find matching keys. Use only on small datasets.

bashANYrediskeysdanger
bash
KEYS user:*
Notes

Prefer SCAN in production because KEYS blocks and can be expensive.

Iterate keys incrementally

Cursor-based key iteration for production-safe scanning.

bashANYredisscaniteration
bash
SCAN 0 MATCH user:* COUNT 100

Return a random key

Pick an arbitrary key from the current DB.

bashANYredisrandomkey
bash
RANDOMKEY

Rename a key

Rename a key, overwriting destination if it exists.

bashANYredisrename
bash
RENAME cache:old cache:new

Rename only if destination absent

Rename without overwriting an existing destination key.

bashANYredisrenamesafe
bash
RENAMENX cache:old cache:new

Set expiration in seconds

Set a TTL in seconds.

bashANYredisexpirettl
bash
EXPIRE session:123 3600

Set expiration in milliseconds

Set a TTL in milliseconds.

bashANYredispexpirettl
bash
PEXPIRE job:1 5000

Expire at a Unix timestamp

Expire a key at a specific second timestamp.

bashANYredisexpireatttl
bash
EXPIREAT report:1 1767225600

Get remaining TTL in seconds

Check remaining time to live.

bashANYredisttl
bash
TTL session:123

Get remaining TTL in milliseconds

Check remaining time to live in milliseconds.

bashANYredispttl
bash
PTTL job:1

Remove expiration

Clear the TTL and keep the key permanently.

bashANYredispersist
bash
PERSIST session:123

Update LRU/LFU idle time

Mark keys as accessed without reading values.

bashANYredistouch
bash
TOUCH user:1 user:2

Serialize a key

Return serialized value in RDB format.

bashANYredisdumpbackup
bash
DUMP user:1

Restore serialized key

Create a key from serialized payload.

bashANYredisrestorebackup
bash
RESTORE user:1-copy 0 '\x00\x01...'

Copy a key

Duplicate a key inside the current or another DB.

bashANYrediscopy
bash
COPY user:1 user:1:backup

Move a key to another logical DB

Move a key to another database index.

bashANYredismovedatabase
bash
MOVE user:1 2

Strings

Simple values, counters, ranges, and conditional set patterns.

Set a string value

Write a string value.

bashANYredisstringset
bash
SET user:1:name 'Alice'

Set value with TTL

Store a string and set expiry atomically.

bashANYredissetexpire
bash
SET session:token 'abc123' EX 3600

Set only if key does not exist

Common lock-style pattern using NX and EX.

bashANYredissetnxlock
bash
SET lock:job 'token' NX EX 30

Get a string value

Read a string value.

bashANYredisgetstring
bash
GET user:1:name

Get and delete a key

Atomically fetch and delete a key.

bashANYredisgetdelstring
bash
GETDEL temp:token

Get a value and update expiration

Read and extend or modify TTL atomically.

bashANYredisgetexttl
bash
GETEX session:token EX 1800

Get multiple values

Fetch many strings in one round trip.

bashANYredismget
bash
MGET user:1:name user:2:name user:3:name

Set multiple values

Write many string values at once.

bashANYredismset
bash
MSET site:name 'CheatSheet' site:env 'prod'

Set many keys only if all are absent

All-or-nothing conditional multi-key set.

bashANYredismsetnx
bash
MSETNX a 1 b 2

Append to a string

Append bytes to an existing string.

bashANYredisappend
bash
APPEND log:events '
new event'

Get string length

Return the byte length of a string.

bashANYredisstrlen
bash
STRLEN user:1:name

Increment integer

Atomically increment a counter.

bashANYrediscounterincr
bash
INCR page:view:123

Increment by a specific value

Increase an integer counter by N.

bashANYrediscounterincrby
bash
INCRBY page:view:123 10

Decrement integer

Atomically decrement a counter.

bashANYrediscounterdecr
bash
DECR stock:item:1

Increment floating-point value

Increase a floating-point number.

bashANYredisfloatcounter
bash
INCRBYFLOAT account:balance 19.95

Overwrite a range within a string

Replace bytes starting at an offset.

bashANYredisstringrange
bash
SETRANGE blob 5 'XYZ'

Get a substring

Read a portion of a string by byte offsets.

bashANYredisstringrange
bash
GETRANGE blob 0 9

Set one bit

Flip a single bit within a string.

bashANYredisbitsetbit
bash
SETBIT feature:flags 7 1

Read one bit

Read a bit at a specific offset.

bashANYredisbitgetbit
bash
GETBIT feature:flags 7

Count set bits

Count the number of bits set to 1.

bashANYredisbitcountbitmap
bash
BITCOUNT feature:flags

Recommended next

No recommendations yet.