← All cheatsheets

Databases

Redis — data types, caching patterns, and persistence

Deep reference for Redis under pressure: pick the right data type, expire and evict keys deliberately, implement cache-aside with stampede protection, keep writes atomic, and diagnose low hit rate, OOM, slow commands, and persistence stalls with INFO/SLOWLOG/MONITOR.

Docs-checkedrediscachedata-typespersistencettl

Quick reference — reach for these first

Set with TTLSET session:ID "<value>" EX 3600Write a key that self-expires in 3600s. Add NX to set only if absent (lock/idempotency).
ReadGET session:IDFetch the value; a nil reply means the key is missing or already expired.
Atomic counterINCR api:calls:IDCreates key at 0 then +1 atomically, no race. INCRBY for steps, INCRBYFLOAT for decimals.
Live statsredis-cli --statOne sample/sec of ops, hit/miss, keys, memory, clients — first look at a live instance.
Inspect / refresh TTLTTL key EXPIRE key 600TTL = seconds left (-1 no expiry, -2 gone). EXPIRE (re)arms the countdown; PERSIST removes it.

Cache-aside read path (read-through on miss, write-back with TTL)

Cache-aside read path with RedisThe app queries Redis first. On a cache hit it returns the stored value. On a miss it reads the primary database, then writes the value back into Redis with an expiry so subsequent reads are served from cache.AppRediscache · TTLPrimaryDB1 · GET key2a · hit → value2b · miss3 · read row on miss4 · SET key EX ttl

Text equivalent: An app reads Redis first. On a hit, Redis returns the cached value. On a miss, the app reads the primary database, then writes the value back into Redis with a TTL so later reads hit the cache.

Common tasks

Model & write · Operate & scale · Diagnose

Model & writeOperate & scaleDiagnose
Pick the right type for the access patternTYPE keyReturns string/hash/list/set/zset/stream — string=counter/cache blob, hash=object fields, list=queue, set=membership, zset=leaderboard, stream=event log
Cache a DB row (cache-aside)SET user:42 "<json>" EX 300GET user:42 returns the JSON; TTL user:42 is ~300 and counts down
Store object fields without re-serializing the whole blobHSET user:42 name Ada plan pro ; HGETALL user:42HGET user:42 plan returns "pro" without rewriting other fields
Rate-limit or count with expiry windowINCR rl:ip:1.2.3.4 ; EXPIRE rl:ip:1.2.3.4 60 NXCounter rises per request; TTL resets only when NX guard is absent, so the window is fixed
Build a leaderboard with a sorted setZADD board 100 ada 90 bo ; ZREVRANGE board 0 9 WITHSCORESMembers returned high-to-low; ZRANK/ZSCORE give one member's place in O(log n)
Cap memory and choose eviction for a pure cacheCONFIG SET maxmemory 512mb ; CONFIG SET maxmemory-policy allkeys-lruCONFIG GET maxmemory-policy echoes allkeys-lru; evicted_keys in INFO stats rises instead of OOM errors
Make a multi-step update atomicMULTI ; DECR stock:7 ; LPUSH orders 7 ; EXECEXEC returns an array of replies; no other client observed a half-applied state. Use EVAL (Lua) when later steps depend on earlier results
Enumerate keys in production without blockingSCAN 0 MATCH user:* COUNT 200Returns a cursor + batch; iterate until cursor 0. Never run KEYS * on a live instance — it is O(n) and blocks the server
Catch slow commandsCONFIG SET slowlog-log-slower-than 10000 ; SLOWLOG GET 10SLOWLOG lists commands over 10ms with args and timing; SLOWLOG RESET clears after triage
Check hit rate and memory healthINFO stats ; INFO memorykeyspace_hits vs keyspace_misses gives hit ratio; used_memory vs maxmemory and mem_fragmentation_ratio (~1.0–1.5 healthy) show pressure
Choose and verify persistenceCONFIG GET save ; CONFIG GET appendonlysave lists RDB snapshot triggers; appendonly yes means AOF is on (better durability). Combine both for snapshot + replay
Watch a replica's lagINFO replicationOn the replica, master_link_status:up and master_last_io_seconds_ago near 0; master_repl_offset minus slave offset is the backlog
Trace what a client is actually sendingMONITORStreams every command in real time — use briefly, it adds overhead; Ctrl-C to stop
[DESTRUCTIVE — dev/test only] Clear a logical DBFLUSHDB ASYNCDBSIZE returns 0 afterward. FLUSHDB wipes the current DB, FLUSHALL wipes every DB — irreversible; never run against production, and confirm the target with CLIENT INFO / SELECT first

A cache that isn't helping: from low hit rate to a fix you can verify

cache-aside diagnosis

1
Confirm the symptom: measure the actual hit ratio instead of assuming
redis-cli INFO stats | grep -E 'keyspace_(hits|misses)'
# keyspace_hits:12044
# keyspace_misses:98210   -> ratio ~0.11, almost every read misses
2
Check whether entries even survive long enough to be reused
redis-cli TTL user:42
# (integer) 4    -> TTL was set to ~5s, keys expire before the next request reuses them
3
Check whether memory pressure is evicting hot keys underneath you
redis-cli INFO stats | grep evicted_keys ; redis-cli CONFIG GET maxmemory-policy
# evicted_keys:874120  (climbing fast)
# maxmemory-policy: noeviction   -> wrong signal: also near maxmemory
4
Fix the two root causes: a realistic TTL and a cache-appropriate eviction policy
redis-cli CONFIG SET maxmemory-policy allkeys-lru
# in app code on write: SET user:42 "<json>" EX 300
# OK  -> cold keys now evicted (not random hot ones), entries live 5 min
5
Protect the repopulate path from a stampede so a mass-expiry doesn't hammer the DB
# on miss, let one caller rebuild:
SET lock:user:42 1 NX EX 5   # winner rebuilds + SETs; others briefly serve stale/retry
# OK for the single winner, nil for the rest (SET..NX returns OK/nil; (integer) 1 is the legacy SETNX)
6
Verify the fix: let traffic run, then re-measure the ratio and eviction trend
redis-cli INFO stats | grep -E 'keyspace_(hits|misses)|evicted_keys'
# keyspace_hits:帯 881204  keyspace_misses:33110  -> ratio ~0.96
# evicted_keys flat -> hit rate climbed, DB load dropped. Problem resolved.

Troubleshooting — symptom → first check → next check

SymptomFirst checkEstablishesNext check
Cache barely helps — DB still hot, latency unchangedredis-cli INFO stats | grep keyspace, compute hits/(hits+misses)Whether misses dominate (ratio well under ~0.8) versus the cache simply not being readIf misses dominate: TTLs too short (TTL key shows seconds), keys evicted (evicted_keys rising), or key naming mismatch between write and read path
Writes failing with OOM command not allowed / used_memory at the capINFO memory for used_memory vs maxmemory, then CONFIG GET maxmemory-policyWhether memory is capped and whether the policy evicts or rejects writesFor a cache set an allkeys-* policy (lru/lfu) so writes evict cold keys; for a data store raise maxmemory or shard. noeviction returns OOM by design
Intermittent latency spikes / commands timing outSLOWLOG GET 20 after setting slowlog-log-slower-than 10000Which specific commands exceed the threshold and with what argumentsReplace O(n) offenders — KEYS → SCAN, huge HGETALL/LRANGE/SMEMBERS → paginate, big MULTI/Lua → split; one blocking command stalls all clients (single-threaded)
Periodic stall or latency bump every few minutes; fork errors in logINFO persistence — look at rdb_last_bgsave_status, aof_rewrite_in_progress, latest_fork_usecWhether a BGSAVE/AOF-rewrite fork (copy-on-write) is the stall sourceEnsure vm.overcommit_memory=1 so fork succeeds; tune auto-aof-rewrite-percentage, move snapshots to a replica, or use faster disk. latest_fork_usec shows fork cost
Replica serving stale reads / falling behindINFO replication on the replica: master_link_status and master_last_io_seconds_agoWhether the link is up and how many seconds behind the replica isIf link down: check network/auth (masterauth) and repl_backlog; if up but lagging: write volume exceeds replica throughput — reduce big writes or scale the replica
used_memory far exceeds the data you think you storedINFO memory mem_fragmentation_ratio, then MEMORY USAGE <key> on suspects and redis-cli --bigkeysWhether it is real big keys vs allocator fragmentation (ratio >1.5)For big keys: split collections or add TTLs; for fragmentation: enable activedefrag yes or restart during a window. --bigkeys samples the largest keys per type

References

Commands target Redis 7.x via redis-cli (OSS/Stack). Managed variants (AWS ElastiCache, Redis Cloud, Azure Cache) often disable CONFIG SET, FLUSHALL/FLUSHDB, and DEBUG — set maxmemory/policy in the provider console there. Cluster mode changes KEYS/SCAN and multi-key/MULTI semantics (keys must share a hash slot).