Redis Cheatsheet
Every essential Redis command: strings, hashes, lists, sets, sorted sets, expiration, pub/sub, and transactions, with syntax and real use cases.99 commands · 7 sections
Redis is an in-memory data store used for caching, queues, sessions, and leaderboards. This cheatsheet covers the commands you use daily: strings and counters, hashes, lists as queues, sets and sorted sets, key expiration, pub/sub, and transactions.
Every command shows its syntax with placeholders followed by the use case.
Strings17
SET key valueGET keySET key value EX 60SET key value NXSET key value XXSETEX key 60 valueINCR keyINCRBY key 10DECR key / DECRBY key 5SETNX key valueGETSET key newvalueMGET k1 k2 k3MSET k1 v1 k2 v2APPEND key valueSTRLEN keyGETRANGE key start endGETEX key EX 60Hashes11
HSET key field valueHGET key fieldHGETALL keyHMGET key f1 f2HINCRBY key field 1HDEL key fieldHEXISTS key fieldHLEN keyHKEYS keyHSETNX key field valueHSCAN key 0 MATCH user:* COUNT 100Lists15
LPUSH key valueRPUSH key valueLPOP keyRPOP keyBLPOP key 5BRPOP key 5LRANGE key 0 -1LRANGE key 0 9LLEN keyLTRIM key 0 99LINDEX key indexLSET key index valueLREM key count valueLPUSHX / RPUSHX key valueRPOPLPUSH source destSets & Sorted Sets20
SADD key memberSMEMBERS keySISMEMBER key memberSREM key memberSCARD keySINTER k1 k2SUNION k1 k2SDIFF k1 k2SISMEMBER key memberSADD key member EX 3600ZADD key score memberZINCRBY key 10 memberZRANGE key 0 -1 WITHSCORESZREVRANGE key 0 9 WITHSCORESZRANK key memberZSCORE key memberZREM key memberZCARD keyZRANGEBYSCORE key min maxZREMRANGEBYSCORE key min maxExpiration & Keys12
EXPIRE key 60TTL keyPERSIST keyKEYS patternSCAN 0 MATCH user:* COUNT 100EXISTS keyDEL key1 key2TYPE keyRENAME old newRANDOMKEYUNLINK keyCOPY key newkeyPub/Sub & Transactions10
PUBLISH channel messageSUBSCRIBE channelPSUBSCRIBE news.*UNSUBSCRIBE channelMULTIEXECDISCARDWATCH keyEVAL script numkeys key1 arg1SCRIPT LOAD scriptAdmin & Monitoring14
PINGSELECT 0INFOINFO keyspaceCLIENT LISTCLIENT KILL ADDR 1.2.3.4:5678CONFIG GET maxmemoryCONFIG SET maxmemory 512mbDBSIZEFLUSHDBFLUSHALLMONITORSLOWLOG GET 10SHUTDOWN NOSAVERedis Cheatsheet
Every essential Redis command: strings, hashes, lists, sets, sorted sets, expiration, pub/sub, and transactions, with syntax and real use cases.
Redis is an in-memory data store used for caching, queues, sessions, and leaderboards. This cheatsheet covers the commands you use daily: strings and counters, hashes, lists as queues, sets and sorted sets, key expiration, pub/sub, and transactions.
Every command shows its syntax with placeholders followed by the use case.
Strings
SET key value: Store a string value.GET key: Read a string value.SET key value EX 60: Set with expiration: cache entries that auto-expire.SET key value NX: Set only if the key does NOT exist: locks and dedup.SET key value XX: Set only if the key already exists: update-only.SETEX key 60 value: Set with expiration in one command.INCR key: Increment a counter: page views, API usage.INCRBY key 10: Increment by a specific amount.DECR key / DECRBY key 5: Decrement a counter: inventory, tokens.SETNX key value: Set-if-not-exists: the distributed lock primitive.GETSET key newvalue: Get the old value AND set a new one atomically.MGET k1 k2 k3: Read multiple keys in one round-trip.MSET k1 v1 k2 v2: Write multiple keys atomically.APPEND key value: Append to a string value: log builders.STRLEN key: Length of a string value.GETRANGE key start end: Substring of a value.GETEX key EX 60: Get a value and set its expiration in one call: touch pattern.Hashes
HSET key field value: Set one field of a hash: user profiles.HGET key field: Read one field.HGETALL key: Read ALL fields: the full object.HMGET key f1 f2: Read multiple fields in one call.HINCRBY key field 1: Increment a numeric field inside a hash.HDEL key field: Delete a field from a hash.HEXISTS key field: Check if a field exists.HLEN key: Number of fields.HKEYS key: List all field names.HSETNX key field value: Set a field only if it does not exist.HSCAN key 0 MATCH user:* COUNT 100: Incrementally scan hash fields: no blocking HGETALL on huge hashes.Lists
LPUSH key value: Push to the LEFT (head): newest-first lists.RPUSH key value: Push to the RIGHT (tail): queue enqueue.LPOP key: Pop from the LEFT: queue dequeue (FIFO).RPOP key: Pop from the RIGHT: LIFO stack.BLPOP key 5: Blocking pop with timeout: wait for work instead of polling.BRPOP key 5: Blocking right pop: priority processing.LRANGE key 0 -1: Read the whole list.LRANGE key 0 9: First 10 items: pagination.LLEN key: List length: queue depth.LTRIM key 0 99: Keep only the first 100: cap logs and history.LINDEX key index: Read an item by index.LSET key index value: Overwrite an item by index.LREM key count value: Remove occurrences of a value.LPUSHX / RPUSHX key value: Push only if the list exists: avoid recreating deleted lists.RPOPLPUSH source dest: Pop from one list, push to another: processing + retry queue pattern.Sets & Sorted Sets
SADD key member: Add a member to a set: deduplicated membership.SMEMBERS key: List all members.SISMEMBER key member: Membership check: is this user online?SREM key member: Remove a member.SCARD key: Set size: online count.SINTER k1 k2: Intersection: mutual followers, common tags.SUNION k1 k2: Union: merge audiences.SDIFF k1 k2: Difference: in k1 but not k2.SISMEMBER key member: O(1) membership: the reason sets beat lists for checks.SADD key member EX 3600: Set member with TTL (Redis 7.4+): temporary membership.ZADD key score member: Add a member with a score: leaderboards.ZINCRBY key 10 member: Increase a member's score: winner moves up.ZRANGE key 0 -1 WITHSCORES: List members ordered by score.ZREVRANGE key 0 9 WITHSCORES: Top 10: reverse order by score.ZRANK key member: A member's position (lowest score = 0).ZSCORE key member: A member's score.ZREM key member: Remove a member.ZCARD key: Number of members.ZRANGEBYSCORE key min max: Members within a score range: rating bands.ZREMRANGEBYSCORE key min max: Delete members in a score range: prune stale entries.Expiration & Keys
EXPIRE key 60: Set a TTL on an existing key: session timeout.TTL key: Seconds until expiry: -1 means no TTL.PERSIST key: Remove the TTL: keep a key forever.KEYS pattern: Find keys by pattern: DANGER in production (blocking, full scan).SCAN 0 MATCH user:* COUNT 100: Incrementally scan keys: the safe KEYS alternative.EXISTS key: Check if a key exists.DEL key1 key2: Delete keys.TYPE key: What data type is this key?RENAME old new: Rename a key.RANDOMKEY: Random key: sampling for debugging.UNLINK key: Delete in the background: non-blocking DEL for big values.COPY key newkey: Duplicate a key value.Pub/Sub & Transactions
PUBLISH channel message: Send a message to all subscribers.SUBSCRIBE channel: Subscribe and receive messages in real time.PSUBSCRIBE news.*: Subscribe with a pattern: multi-channel fans.UNSUBSCRIBE channel: Stop subscribing.MULTI: Start a transaction: commands queue until EXEC.EXEC: Run all queued commands atomically.DISCARD: Cancel a queued transaction.WATCH key: Optimistic lock: abort EXEC if the key changes.EVAL script numkeys key1 arg1: Run a Lua script atomically: complex operations in one shot.SCRIPT LOAD script: Cache a Lua script: returns its SHA to call later.Admin & Monitoring
PING: Check connectivity: the redis health check.SELECT 0: Switch database index: logical partitions.INFO: Server stats: memory, clients, keyspace.INFO keyspace: How many keys in each database.CLIENT LIST: Show connected clients.CLIENT KILL ADDR 1.2.3.4:5678: Disconnect a specific client.CONFIG GET maxmemory: Read a config value.CONFIG SET maxmemory 512mb: Change a config at runtime.DBSIZE: Number of keys in the current database.FLUSHDB: Delete ALL keys in the current database.FLUSHALL: Delete EVERYTHING in every database.MONITOR: Stream every command live: debugging, never in production.SLOWLOG GET 10: Last 10 slow commands: find the offenders.SHUTDOWN NOSAVE: Stop the server without saving.Frequently asked questions
How do I set an expiration on a key?
Use EXPIRE <key> <seconds> to set TTL on an existing key, or SET <key> <value> EX <seconds> to set it at write time. Check remaining time with TTL and remove expiration with PERSIST.
What is the difference between lists and sets?
Lists are ordered sequences with duplicates: use them for queues with LPUSH/RPUSH and LPOP/RPOP. Sets are unordered, unique collections: ideal for tags, followers, and membership checks with SADD/SISMEMBER.
How do sorted sets work?
Sorted sets map members to scores and keep them ordered by score: ZADD <key> <score> <member>. They power leaderboards, rate limiters, and range queries with ZRANGE and ZREVRANGE.
How do transactions work in Redis?
MULTI starts a transaction, subsequent commands queue, and EXEC runs them atomically in order. WATCH provides optimistic locking: if a watched key changes before EXEC, the transaction aborts.