Redis Cheatsheet - Redis Command Reference

For developers using Redis as a cache who must care about memory and TTL strategy. The commands are easy, but big keys blowing up memory, hot keys concentrating load on one node, and cache penetration/avalanche are the real business problems. By the end you can pick the right command per data structure, use INFO memory and object encoding to find top memory consumers, understand the RDB/AOF persistence trade-off, and use SCAN/UNLINK to handle big keys without blocking the instance.

Databases·43 commands·Last updated 2026-07-21
redisCachingnosqlMemory

Connection 6

redis-cli -h 127.0.0.1 -p 6379 -a password
Connect to instance, -a specifies password (warns about security)
redis-cli --scan --pattern "user:*"
Safely scan matching keys, avoid KEYS (blocking)
redis-cli -r 5 -i 1 INFO memory
Collect memory info every second, 5 times, for monitoring
AUTH password
Authenticate after connecting
SELECT 0
Switch to database 0 (not supported in cluster mode)
DBSIZE
View number of keys in current database

Data Structure 11

SET key value EX 300
Set key-value with 300s expiry, atomic operation
GET key
Get string value
MSET k1 v1 k2 v2
Batch set, reduces round trips
HSET user:1 name "tom" age 30
Set hash fields
HGETALL user:1
Get all hash fields and values
LPUSH queue task1
Left push to list (head)
BRPOP queue 30
Blocking right pop (tail), 30s timeout
ZADD rank 100 "user1" 200 "user2"
Add sorted set members with scores
ZRANGE rank 0 -1 WITHSCORES
Return all members sorted by score ascending
SADD tags "python" "redis"
Add set members
SINTER set1 set2
Intersection of sets

Key Space 8

EXPIRE key 300
Set expiry in 300 seconds
TTL key
View remaining TTL (-1 forever, -2 does not exist)
TYPE key
Check data type of key
DEL key1 key2
Delete multiple keys
UNLINK key
Async delete large key, won't block main thread
RENAME old_key new_key
Rename key
RANDOMKEY
Return a random key
OBJECT ENCODING key
View internal encoding (ziplist/hashtable/intset)

Persistence & Memory 7

BGSAVE
Background async RDB snapshot, non-blocking
LASTSAVE
View Unix timestamp of last successful RDB save
BGREWRITEAOF
Background async AOF rewrite, compact file size
INFO memory
View memory details, check used_memory_human and mem_fragmentation_ratio
MEMORY USAGE key
Check memory usage of a single key in bytes
CONFIG GET maxmemory
View max memory limit
CONFIG GET maxmemory-policy
View eviction policy (noeviction/allkeys-lru etc.)

Slowlog & Monitor 6

SLOWLOG GET 10
View last 10 slow queries
SLOWLOG RESET
Clear slow query log
CONFIG GET slowlog-log-slower-than
View slow query threshold (microseconds, 0=all, -1=disabled)
CLIENT LIST
List all connections, find idle connections and source IPs
CLIENT KILL ID <id>
Kill a specific connection
INFO clients
View client statistics, check connected_clients and blocked_clients

Cluster & Replication 5

CLUSTER INFO
View cluster status, cluster_state should be ok
CLUSTER NODES
View all node roles and connection status
CLUSTER KEYSLOT key
Check hash slot of a key (0-16383)
INFO replication
View master/slave role and sync status
ROLE
Quick check role (master/slave) and offset

Tips

  • Never use KEYS * in production — use SCAN instead. KEYS blocks the main thread and causes stuttering.
  • Use UNLINK instead of DEL for large keys (lists/hashes/sets) — DEL blocks the main thread.
  • mem_fragmentation_ratio > 1.5 indicates high fragmentation — consider restarting or enabling activedefrag.

FAQ

What is the difference between Redis and Memcached, and when should I use Redis?

Redis supports multiple data structures (strings, hashes, lists, sets, sorted sets) with built-in persistence, while Memcached is simple key-value and pure memory. Choose Redis when you need complex data operations, persistence, leaderboards, message queues, or session storage.

Why should I avoid the KEYS * command in production?

KEYS * traverses the entire keyspace and blocks the Redis main thread; with large datasets it causes stalls and timeouts. Use SCAN instead, which iterates keys incrementally without blocking.

Can Redis lose data? How do I ensure persistence?

Redis offers RDB (point-in-time snapshots) and AOF (append-only log) persistence. RDB restores fast but may lose data after the last snapshot; AOF is safer but larger. Production usually enables both to balance performance and safety.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us