🔴
Redis cheat sheet
Commands for redis-cli. Be careful on production: KEYS blocks the server on large datasets (use SCAN), and FLUSHALL erases everything.
Click any command to copy it.
Connect
| Command | What it does |
|---|---|
redis-cli | Connect to localhost:6379. |
redis-cli -h host -p 6379 -a password | Connect to a remote server (passing a password on the command line is visible in process lists). |
redis-cli -n 1 | Select database 1. |
PING | Check the server responds (expects PONG). |
Strings and keys
| Command | What it does |
|---|---|
SET key value | Set a value. |
GET key | Get a value. |
DEL key | Delete a key. |
EXISTS key | Check whether a key exists. |
EXPIRE key 60 | Expire a key in 60 seconds. |
TTL key | Seconds until a key expires (-1 none, -2 missing). |
INCR counter | Atomically increment a number. |
TYPE key | Show the type of a key. |
Other data types
| Command | What it does |
|---|---|
HSET user:1 name Alice age 30 | Set fields in a hash. |
HGETALL user:1 | Get all fields of a hash. |
LPUSH queue job1 | Push onto a list. |
LRANGE queue 0 -1 | Read an entire list. |
SADD tags a b c | Add to a set. |
SMEMBERS tags | List a set's members. |
ZADD scores 100 alice | Add to a sorted set with a score. |
ZRANGE scores 0 -1 WITHSCORES | Read a sorted set with scores. |
Find keys safely
| Command | What it does |
|---|---|
SCAN 0 MATCH user:* COUNT 100 | Iterate keys incrementally; repeat with the returned cursor. |
redis-cli --scan --pattern 'user:*' | Scan matching keys from the shell. |
KEYS user:* | Destructive: Lists all matching keys but blocks the server on big datasets. Avoid in production. |
DBSIZE | Number of keys in the current database. |
redis-cli --bigkeys | Sample the keyspace and report the largest keys. |
Server and diagnostics
| Command | What it does |
|---|---|
INFO | Server statistics (add a section such as INFO memory). |
INFO replication | Replication role and lag. |
CONFIG GET maxmemory | Read a configuration value. |
SLOWLOG GET 10 | The ten most recent slow commands. |
CLIENT LIST | Connected clients. |
MONITOR | Stream every command the server receives (heavy; use briefly). |
BGSAVE | Write a snapshot to disk in the background. |
FLUSHDB | Destructive: Delete every key in the current database. |
FLUSHALL | Destructive: Delete every key in every database. |
Frequently asked questions
Why shouldn't I use KEYS in production?
KEYS scans the whole keyspace in a single blocking operation. On large datasets that stalls every other client. SCAN returns results in small batches.
How do I see how much memory Redis uses?
Run INFO memory and look at used_memory_human and maxmemory_human.