Click any item to expand the explanation and examples.
🔌 Connection
redis-cli connect
redis-cli # Connect to localhost:6379
redis-cli -h myhost -p 6380 # Custom host/port
redis-cli -a mypassword # With password
redis-cli --tls # TLS connection
redis-cli ping # Test connection → PONG
redis-cli info # Server info
redis-cli monitor # Watch all commands in real-time
📝 Strings
SET / GET / DEL strings
SET name "Alice"
GET name # "Alice"
DEL name # Delete key
SETNX name "Bob" # Set only if not exists
MSET a 1 b 2 c 3 # Set multiple
MGET a b c # Get multiple
APPEND name " Smith" # Append to string
STRLEN name # String length
INCR / DECR (counters) strings
SET visits 0
INCR visits # 1
INCR visits # 2
INCRBY visits 10 # 12
DECR visits # 11
DECRBY visits 5 # 6
TTL / EXPIRE strings
SET session "abc123" EX 3600 # Expires in 3600 seconds
SET token "xyz" PX 60000 # Expires in 60000 milliseconds
EXPIRE key 300 # Set TTL on existing key
TTL key # Seconds remaining (-1 = no expiry, -2 = gone)
PERSIST key # Remove expiry
📋 Hashes
HSET / HGET / HGETALL hashes
Hashes are like objects/dictionaries — perfect for storing records.
HSET user:1 name "Alice" email "alice@example.com" age 30
HGET user:1 name # "Alice"
HGETALL user:1 # All fields and values
HMGET user:1 name email # Multiple fields
HDEL user:1 age # Delete a field
HEXISTS user:1 name # 1 (true)
HKEYS user:1 # All field names
HVALS user:1 # All values
HINCRBY user:1 age 1 # Increment numeric field
📚 Lists
LPUSH / RPUSH / LRANGE lists
Lists are ordered sequences — good for queues and recent items.
RPUSH queue "task1" "task2" # Push to right (end)
LPUSH queue "task0" # Push to left (front)
LRANGE queue 0 -1 # Get all items
LRANGE queue 0 9 # First 10 items
LPOP queue # Pop from left
RPOP queue # Pop from right
LLEN queue # Length
LINDEX queue 0 # Get by index
🎯 Sets & Sorted Sets
SADD / SMEMBERS / SINTER sets
Sets are unordered collections of unique values.
SADD tags "python" "redis" "docker"
SMEMBERS tags # All members
SISMEMBER tags "python" # 1 (true)
SCARD tags # Count
SREM tags "docker" # Remove
SINTER set1 set2 # Intersection
SUNION set1 set2 # Union
SDIFF set1 set2 # Difference
ZADD / ZRANGE (sorted sets) sorted-sets
Sorted sets have a score for ordering — perfect for leaderboards and rankings.
ZADD leaderboard 100 "alice" 85 "bob" 92 "charlie"
ZRANGE leaderboard 0 -1 WITHSCORES # Ascending
ZREVRANGE leaderboard 0 2 WITHSCORES # Top 3
ZSCORE leaderboard "alice" # 100
ZINCRBY leaderboard 5 "bob" # Add 5 to bob's score
ZRANK leaderboard "alice" # Rank (0-based)
ZCOUNT leaderboard 90 100 # Count in score range
🔑 Key Management
KEYS / SCAN / TYPE keys
KEYS user:* # Find keys (DON'T use in production — blocks)
SCAN 0 MATCH user:* COUNT 100 # Safe iteration
TYPE mykey # String, list, set, hash, zset
EXISTS mykey # 1 or 0
RENAME oldkey newkey
DBSIZE # Total key count
FLUSHDB # Delete all keys in current DB
📡 Pub/Sub
SUBSCRIBE / PUBLISH pubsub
# Terminal 1: Subscribe
SUBSCRIBE notifications
# Terminal 2: Publish
PUBLISH notifications "New order received"
# Pattern subscribe
PSUBSCRIBE user:* # Match user:login, user:logout, etc.
See also: Docker cheat sheet | PostgreSQL cheat sheet