Redis Cheatsheet

Senior Backend Engineer
Install Redis cli
$ brew install redis-cli
General
PING # Test connection, returns PONG
AUTH <password> # Authenticate with password
SELECT <db> # Switch to a specific database (default is 0)
DBSIZE # Get total number of keys in the current DB
FLUSHDB # Remove all keys from the current DB
FLUSHALL # Remove all keys from all DBs
KEYS <pattern> # Find keys matching a pattern (use with caution in production)
SCAN 0 MATCH <p> # Incrementally iterate keys (safer than KEYS)
TYPE <key> # Get data type of a key
TTL <key> # Get remaining time to live of a key
EXPIRE <key> <s> # Set a key’s expiration in seconds
PERSIST <key> # Remove the expiration from a key
Strings
SET <key> <val> # Set value
GET <key> # Get value
MSET k1 v1 k2 v2 # Set multiple keys
MGET k1 k2 # Get multiple keys
INCR <key> # Increment integer value
DECR <key> # Decrement integer value
APPEND <key> <val> # Append value to existing key
STRLEN <key> # Get length of value
Hashes
HSET <key> <field> <val> # Set field in a hash
HGET <key> <field> # Get field value
HMSET <key> f1 v1 f2 v2 # Set multiple fields
HMGET <key> f1 f2 # Get multiple fields
HGETALL <key> # Get all fields and values
HDEL <key> <field> # Delete a field
HLEN <key> # Number of fields
HKEYS <key> # Get all field names
HVALS <key> # Get all values
HEXISTS <key> <field> # Check if field exists
Lists
LPUSH <key> <val> # Push value to head
RPUSH <key> <val> # Push value to tail
LPOP <key> # Remove and return first element
RPOP <key> # Remove and return last element
LRANGE <key> 0 -1 # Get all elements
LLEN <key> # Get list length
LINDEX <key> <i> # Get element at index
LREM <key> <count> <val> # Remove elements equal to value
LTRIM <key> <start> <end> # Keep a sublist
Sets
SADD <key> <val> # Add member
SREM <key> <val> # Remove member
SMEMBERS <key> # Get all members
SISMEMBER <key> <val> # Check if member exists
SCARD <key> # Get number of members
SINTER key1 key2 # Intersection
SUNION key1 key2 # Union
SDIFF key1 key2 # Difference
Sorted Sets
ZADD <key> <score> <val> # Add member with score
ZRANGE <key> 0 -1 # Get all members by score (lowest to highest)
ZREVRANGE <key> 0 -1 # Get all members (highest to lowest)
ZRANK <key> <val> # Get rank of member
ZREVRANK <key> <val> # Get reverse rank
ZSCORE <key> <val> # Get score of member
ZCARD <key> # Number of members
ZREM <key> <val> # Remove member
Transactions
MULTI # Start transaction
EXEC # Execute transaction
DISCARD # Cancel transaction
WATCH <key> # Watch key for changes
UNWATCH # Stop watching keys
Pub/Sub
PUBLISH <chan> <msg> # Publish message
SUBSCRIBE <chan> # Subscribe to channel
PSUBSCRIBE <pattern> # Subscribe to channel pattern
UNSUBSCRIBE <chan> # Unsubscribe
Persistence and Config
SAVE # Save DB synchronously
BGSAVE # Save DB asynchronously
CONFIG GET <param> # Read config
CONFIG SET <param> <val> # Update config
INFO # Get server info
MONITOR # Stream all commands (debugging)
Advanced Topics
Replication:
SLAVEOF host port(deprecated, useREPLICAOF)Cluster:
CLUSTER NODES,CLUSTER INFOSecurity: Use
ACL LIST,ACL SETUSERPerformance: Prefer
SCANoverKEYSin production


