Skip to content

Redis Cheatsheet — Complete Quick Reference (2026)

DodaTech Updated 2026-06-20 4 min read

In this tutorial, you'll learn about Redis Cheatsheet. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Redis is an in-memory data structure store used as a cache, Message Broker, and database, supporting strings, hashes, lists, sets, sorted sets, and more with sub-millisecond latency.

Strings

Command Example Description
SET SET key "value" Set key value
GET GET key Get key value
GETSET GETSET key "new" Set + return old
MGET MGET k1 k2 Get multiple
INCR INCR counter Atomic increment
INCRBY INCRBY counter 5 Increment by
APPEND APPEND key "x" Append to value
STRLEN STRLEN key Get string length
SETEX SETEX key 60 "val" Set with TTL

Hashes

Command Example Description
HSET HSET user:1 name "alice" Set field
HGET HGET user:1 name Get field
HGETALL HGETALL user:1 All fields
HMSET HMSET user:1 name alice age 30 Multi-set
HDEL HDEL user:1 age Delete field
HEXISTS HEXISTS user:1 name Check field
HINCRBY HINCRBY user:1 age 1 Atomic incr
HKEYS HKEYS user:1 All keys
HVALS HVALS user:1 All values

Lists

Command Description
LPUSH key value Push to head
RPUSH key value Push to tail
LPOP key Pop from head
RPOP key Pop from tail
LLEN key List length
LRANGE key 0 -1 Get all elements
LTRIM key 0 99 Trim to 100 items
BLPOP key timeout Blocking pop (head)
BRPOP key timeout Blocking pop (tail)

Sets

Command Description
SADD key member Add member
SREM key member Remove member
SMEMBERS key All members
SISMEMBER key member Check membership
SINTER k1 k2 Intersection
SUNION k1 k2 Union
SDIFF k1 k2 Difference
SCARD key Cardinality
SPOP key count Pop random
SSCAN key 0 Iterate (cursor-based)

Sorted Sets (ZSET)

Command Description
ZADD key score member Add with score
ZRANGE key 0 -1 Get by rank (asc)
ZREVRANGE key 0 -1 Get by rank (desc)
ZRANGEBYSCORE key min max Get by score range
ZREM key member Remove member
ZCARD key Cardinality
ZCOUNT key min max Count in score range
ZINCRBY key incr member Increment score
ZRANK key member Rank (asc)

Expiration & TTL

Command Description
EXPIRE key seconds Set TTL
TTL key Remaining seconds (−1 none, −2 gone)
PEXPIRE key ms Set TTL in ms
SETEX key seconds value Set + TTL
PERSIST key Remove TTL

Pub/Sub

SUBSCRIBE channel               # listen to channel
PUBLISH channel "message"       # send message
PSUBSCRIBE news:*               # pattern subscribe
UNSUBSCRIBE channel             # stop listening

Transactions & Pipelines

MULTI               # start transaction
SET k1 v1
SET k2 v2
EXEC                # execute atomically
DISCARD             # abort

Pipelining sends multiple commands without waiting for replies (no atomicity).

Lua Scripting

-- EVAL script numkeys key1 arg1
EVAL "return redis.call('SET', KEYS[1], ARGV[1])" 1 mykey myvalue
-- Atomic CAS pattern
EVAL "local v = redis.call('GET', KEYS[1]); if v == ARGV[1] then redis.call('SET', KEYS[1], ARGV[2]) return 1 else return 0 end" 1 mykey old new

Configuration

CONFIG GET maxmemory
CONFIG SET maxmemory 512mb
CONFIG GET requirepass
CONFIG REWRITE               # save to redis.conf
INFO memory                  # memory stats
CLIENT LIST                  # connected clients
SLOWLOG GET 10               # slow queries

Must-Know Items

  • Use EXPIRE on all cache keys to prevent memory exhaustion
  • BLPOP/BRPOP are essential for work queues with blocking consumers
  • Redis is single-threaded for command execution — avoid KEYS * in production, use SCAN
  • Lua scripts run atomically — use for CAS, Rate Limiting, and multi-key operations
  • INFO command provides memory, CPU, hit rate, and Replication stats
  • RDB snapshots and AOF logs provide persistence — AOF is more durable
  • Clustering uses hash slots ({hash_tag} to co-locate keys)
  • Use RENAME carefully — it's not atomic with EXISTS checks
What is the difference between Redis transactions and Lua scripting?

Redis transactions (MULTI/EXEC) queue commands atomically but cannot use previous results as inputs. Lua scripts run atomically on the server and can incorporate logic (branches, loops), making them suitable for CAS patterns and multi-key operations.

When should I use Redis pipelines instead of transactions?

Pipelines send commands in batch without waiting for individual replies, reducing network round trips. Unlike transactions, commands are not executed atomically — each is processed as it arrives. Use pipelines for bulk writes when atomicity isn't needed.

See full Redis tutorials for Caching and queue patterns.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro