Cheatsheets
Redis
Data types, TTL & eviction, caching patterns, locks, streams, and persistence.
Visualize
Redis — key expiry (TTL)
Keys set with EX expire when their countdown hits zero; a GET then returns nil.
Each key has a TTL — press play to run the clock forward.
52 entries
Strings6
SET key valueSet a string value (overwrites, clears TTL unless KEEPTTL).
GET keyRead a string value (nil if missing).
SETEX key seconds valueSet a value with a TTL atomically.
INCR key / DECR keyAtomic +1 / -1 on an integer string.
APPEND key valueAppend to a string, returns new length.
MSET k1 v1 k2 v2 / MGET k1 k2Set/get multiple keys in one round trip.
Hashes3
HSET key field value [field value ...]Set one or more fields of a hash.
HGET key field / HGETALL keyRead one field, or the whole hash as field/value pairs.
HINCRBY key field nAtomically add to a numeric hash field.
Lists (queues)4
LPUSH / RPUSH key value [value ...]Push onto the head (L) or tail (R) of a list.
LPOP / RPOP key [count]Pop element(s) from the head/tail.
BRPOP key [key ...] timeoutBlocking pop — wait for an element instead of polling.
LRANGE key start stop / LTRIM key start stopRead a slice / trim a list to a slice (capped list).
Sets & sorted sets6
SADD key member [member ...]Add member(s) to an unordered set of unique strings.
SINTER / SUNION / SDIFF key [key ...]Set algebra: intersection, union, difference.
ZADD key score member [score member ...]Add member(s) to a sorted set, ranked by score.
ZRANGE / ZREVRANGE key start stop [WITHSCORES]Read members by rank (position), low-to-high or high-to-low.
ZRANGEBYSCORE key min maxRead members whose score falls in a range.
ZRANK / ZREVRANK key memberGet the 0-based rank of a member (low/high first).
Streams, bitmaps, HLL & geo6
XADD stream * field value [field value ...]Append an entry to a stream; '*' auto-generates an ID.
XREAD COUNT n BLOCK ms STREAMS key idRead new entries after an ID; optionally block for new ones.
XGROUP CREATE + XREADGROUP + XACKConsumer groups: distribute entries across workers with acks.
SETBIT key offset 0|1 / BITCOUNT keyBitmap: set a single bit; count set bits.
PFADD key element [element ...] / PFCOUNT keyHyperLogLog: approximate unique-count in ~12 KB.
GEOADD key lon lat member / GEOSEARCHGeospatial index: store points, query by radius/box.
Expiry & eviction6
EXPIRE key seconds / PEXPIRE key msAttach a TTL to an existing key (seconds / milliseconds).
TTL key / PTTL keyHow long until a key expires (seconds / ms).
PERSIST keyRemove a key's TTL, making it permanent again.
How Redis expires keys: lazy + activeExpiry is not instantaneous — two mechanisms reclaim memory.
CONFIG SET maxmemory 2gbCap memory so Redis acts as a bounded cache.
maxmemory-policy: choosing an eviction policyWhat Redis discards when memory is full — pick by workload.
Caching patterns5
Cache-aside (lazy loading / read-through)App checks cache; on miss, loads from DB and backfills.
Write-through vs write-behindWhen to update the cache on writes — sync vs async.
Cache invalidation (DEL / SET on write)Keep the cache correct after the source of truth changes.
Cache stampede / thundering herdMany clients miss the same key at once and all hit the DB.
Hot-key problemOne key gets so much traffic it saturates a single shard.
Concurrency & messaging6
SET key token NX PX 30000 (distributed lock)Acquire a lock atomically: set only if absent, with a TTL.
Unlock via Lua (compare-and-delete)Release a lock ONLY if you still own it — atomically.
MULTI / EXEC / WATCH (optimistic transaction)Queue commands and run them atomically; WATCH aborts on conflict.
EVAL (Lua) vs pipelining — atomic vs throughputLua runs atomic server-side logic; pipelining just batches RTTs.
PUBLISH / SUBSCRIBE (Pub/Sub)Fire-and-forget broadcast to currently-connected subscribers.
Pub/Sub vs Streams — which messaging primitive?Ephemeral broadcast vs durable, replayable log with groups.
Persistence & durability4
RDB snapshots (SAVE / BGSAVE)Point-in-time binary dumps of the whole dataset.
AOF (appendonly + appendfsync)Append-only log of every write; replayed on restart.
Replication (replicaof)Async copies of a primary for read scaling and failover.
Sentinel vs ClusterHA failover (Sentinel) vs horizontal sharding (Cluster).
Observability & ops6
SCAN cursor MATCH pattern COUNT n (NEVER KEYS in prod)Incrementally iterate keys without blocking the server.
INFO [section]Server stats: memory, clients, persistence, replication, stats.
SLOWLOG GET [n] / SLOWLOG RESETLog of commands that exceeded a latency threshold.
MONITOR (DANGER: kills throughput in prod)Stream every command the server processes — debug only.
MEMORY USAGE key / OBJECT ENCODING keyBytes used by a key, and its internal encoding.
Keyspace notifications (CONFIG notify-keyspace-events)Get Pub/Sub events when keys change or expire.