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.
Quick reference — reach for these first
SET session:ID "<value>" EX 3600Write a key that self-expires in 3600s. Add NX to set only if absent (lock/idempotency).GET session:IDFetch the value; a nil reply means the key is missing or already expired.INCR api:calls:IDCreates key at 0 then +1 atomically, no race. INCRBY for steps, INCRBYFLOAT for decimals.redis-cli --statOne sample/sec of ops, hit/miss, keys, memory, clients — first look at a live instance.TTL 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)
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 & write | Operate & scale | Diagnose |
|---|---|---|
| Pick the right type for the access pattern | TYPE key | Returns 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 300 | GET user:42 returns the JSON; TTL user:42 is ~300 and counts down |
| Store object fields without re-serializing the whole blob | HSET user:42 name Ada plan pro ; HGETALL user:42 | HGET user:42 plan returns "pro" without rewriting other fields |
| Rate-limit or count with expiry window | INCR rl:ip:1.2.3.4 ; EXPIRE rl:ip:1.2.3.4 60 NX | Counter rises per request; TTL resets only when NX guard is absent, so the window is fixed |
| Build a leaderboard with a sorted set | ZADD board 100 ada 90 bo ; ZREVRANGE board 0 9 WITHSCORES | Members returned high-to-low; ZRANK/ZSCORE give one member's place in O(log n) |
| Cap memory and choose eviction for a pure cache | CONFIG SET maxmemory 512mb ; CONFIG SET maxmemory-policy allkeys-lru | CONFIG GET maxmemory-policy echoes allkeys-lru; evicted_keys in INFO stats rises instead of OOM errors |
| Make a multi-step update atomic | MULTI ; DECR stock:7 ; LPUSH orders 7 ; EXEC | EXEC 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 blocking | SCAN 0 MATCH user:* COUNT 200 | Returns a cursor + batch; iterate until cursor 0. Never run KEYS * on a live instance — it is O(n) and blocks the server |
| Catch slow commands | CONFIG SET slowlog-log-slower-than 10000 ; SLOWLOG GET 10 | SLOWLOG lists commands over 10ms with args and timing; SLOWLOG RESET clears after triage |
| Check hit rate and memory health | INFO stats ; INFO memory | keyspace_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 persistence | CONFIG GET save ; CONFIG GET appendonly | save lists RDB snapshot triggers; appendonly yes means AOF is on (better durability). Combine both for snapshot + replay |
| Watch a replica's lag | INFO replication | On 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 sending | MONITOR | Streams every command in real time — use briefly, it adds overhead; Ctrl-C to stop |
| [DESTRUCTIVE — dev/test only] Clear a logical DB | FLUSHDB ASYNC | DBSIZE 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
redis-cli INFO stats | grep -E 'keyspace_(hits|misses)'
# keyspace_hits:12044
# keyspace_misses:98210 -> ratio ~0.11, almost every read missesredis-cli TTL user:42
# (integer) 4 -> TTL was set to ~5s, keys expire before the next request reuses themredis-cli INFO stats | grep evicted_keys ; redis-cli CONFIG GET maxmemory-policy
# evicted_keys:874120 (climbing fast)
# maxmemory-policy: noeviction -> wrong signal: also near maxmemoryredis-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# 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)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
| Symptom | First check | Establishes | Next check |
|---|---|---|---|
| Cache barely helps — DB still hot, latency unchanged | redis-cli INFO stats | grep keyspace, compute hits/(hits+misses) | Whether misses dominate (ratio well under ~0.8) versus the cache simply not being read | If 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 cap | INFO memory for used_memory vs maxmemory, then CONFIG GET maxmemory-policy | Whether memory is capped and whether the policy evicts or rejects writes | For 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 out | SLOWLOG GET 20 after setting slowlog-log-slower-than 10000 | Which specific commands exceed the threshold and with what arguments | Replace 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 log | INFO persistence — look at rdb_last_bgsave_status, aof_rewrite_in_progress, latest_fork_usec | Whether a BGSAVE/AOF-rewrite fork (copy-on-write) is the stall source | Ensure 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 behind | INFO replication on the replica: master_link_status and master_last_io_seconds_ago | Whether the link is up and how many seconds behind the replica is | If 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 stored | INFO memory mem_fragmentation_ratio, then MEMORY USAGE <key> on suspects and redis-cli --bigkeys | Whether 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
- Redis data types overviewredis.io
- Key eviction & maxmemory-policyredis.io
- Persistence: RDB and AOFredis.io
- EXPIRE / key TTL commandredis.io
- SLOWLOG GETredis.io
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).