Appearance
Redis and Caching at Scale
49. How does Redis persistence work?
The short answer
Redis keeps data in memory and offers two ways to save it to disk. RDB takes a point-in-time snapshot of the whole dataset every so often, using a forked child process; it is compact and fast to restart from, but you lose everything since the last snapshot. AOF, the append-only file, logs every write command and replays it on restart; with the default appendfsync everysec you lose at most about one second of writes. Most production setups use both: AOF for durability, with an RDB-format base inside it for fast restarts. A pure cache can turn persistence off.
| RDB snapshot | AOF log | |
|---|---|---|
| Saves | The whole dataset at a moment | Every write command |
| Data lost on crash | Since the last snapshot, often minutes | About 1 second with everysec |
| File size | Small | Larger; rewritten in the background to shrink it |
| Restart speed | Fast | Slower, replays commands |
| Cost | Fork and copy-on-write memory during a snapshot | A disk write every second |
text
save 3600 1 300 100 60 10000 # snapshot after 1 change in an hour, 100 in 5 minutes, or 10000 in 1 minute
appendonly yes
appendfsync everysec # flush the log to disk once a second
aof-use-rdb-preamble yes # rewritten AOF starts with an RDB snapshot for fast loadingChoosing a setting:
| Redis is used as | Persistence |
|---|---|
| A pure cache | None; it can be rebuilt from the database |
| Sessions, rate limits | AOF everysec; losing a second is acceptable |
| A queue or the only copy of data | AOF plus replicas, and think hard about whether Redis is the right home |
The fork trap. An RDB snapshot forks the Redis process. Pages changed during the snapshot are copied, so a busy instance can briefly need much more memory. Leave headroom, typically keeping maxmemory well below the machine's RAM.
50. How does Redis Pub/Sub work?
The short answer
Publishers send a message to a channel with PUBLISH, and Redis immediately pushes it to every client currently subscribed to that channel, with SUBSCRIBE or a pattern with PSUBSCRIBE. It is fire and forget: messages are not stored, so a subscriber that is disconnected, restarting or slow simply misses them, and there are no acknowledgements or retries. It suits live notifications and cache-invalidation broadcasts. For messages that must not be lost, use Redis Streams with consumer groups, or a real message broker.
ts
const sub = redis.duplicate() // (1)
await sub.connect()
await sub.subscribe('cache:invalidate', (message) => {
localCache.delete(message) // (2)
})
await redis.publish('cache:invalidate', 'product:42') // (3)- A separate connection for subscribing, because a subscribed connection cannot run normal commands.
- Every app instance clears its in-memory copy when told.
- Any instance announces a change. Instances that are offline at this moment miss it, which is acceptable here because the local cache also has a short TTL.
| Pub/Sub | Streams | |
|---|---|---|
| Messages stored | No | Yes, until trimmed |
| Missed if offline | Yes | No; read from where you left off |
| Acknowledgements | No | Yes, with consumer groups |
| Load balancing across workers | No; everyone gets everything | Yes; each message to one consumer in a group |
| Use for | Live broadcasts, invalidation | Job queues, event processing |
51. How does Redis clustering work?
The short answer
Redis Cluster spreads keys across several primary nodes using 16,384 hash slots. Each key's slot is CRC16(key) mod 16384, and each primary owns a range of slots, such as 0 to 5460. Each primary has one or more replicas, and if a primary fails, the others promote its replica automatically. Clients learn the slot map and send each command to the right node; if they are wrong, the node replies MOVED with the correct address. Commands touching several keys only work if all keys are in the same slot, which you force with a hash tag, such as {user:42}:cart.
Hash tags keep related keys together:
text
SET {user:42}:cart "…"
SET {user:42}:prefs "…"
MGET {user:42}:cart {user:42}:prefs # works: only "user:42" inside the braces is hashed, so both share a slot
MGET user:42:cart user:99:cart # fails with CROSSSLOT unless they happen to share a slot| Setup | Gives | Use when |
|---|---|---|
| Single node | Simplicity | Small caches, development |
| Primary with replicas and Sentinel | Automatic failover, no sharding | Data fits on one node |
| Redis Cluster | Sharding plus failover | Data or traffic exceeds one node |
| Managed service | The above, run for you | Most teams |
52. What are cache eviction policies?
The short answer
When a cache is full, an eviction policy decides which keys to remove to make room. LRU, least recently used, removes keys not read for the longest time. LFU, least frequently used, removes keys read the fewest times, which protects popular items from being pushed out by one-off reads. TTL-based removes keys closest to expiring. Random picks any. In Redis, set maxmemory and maxmemory-policy. The default, noeviction, refuses writes when full, which suits a data store but not a cache, where allkeys-lru or allkeys-lfu is usual.
| Redis policy | Removes | Use for |
|---|---|---|
noeviction | Nothing; writes fail when full | Redis as a data store |
allkeys-lru | Least recently used, any key | General caches |
allkeys-lfu | Least frequently used, any key | Caches with a stable set of popular items |
volatile-lru, volatile-lfu | As above, only keys with a TTL | Mixed cache and permanent data in one instance |
volatile-ttl | Keys nearest to expiry | When TTLs reflect value |
allkeys-random, volatile-random | Random keys | Rarely the best |
text
maxmemory 4gb
maxmemory-policy allkeys-lfuApproximate, not exact. Redis does not keep a perfect LRU list of millions of keys. It samples a few keys, five by default, and evicts the best candidate among them. That is close to true LRU at a fraction of the memory cost.
An LRU cache in code is a classic interview exercise: a Map in JavaScript keeps insertion order, so delete and re-insert on read, and evict the first key when full (the LRU coding question).
53. What is a cache stampede, and how can it be prevented?
The short answer
A cache stampede, or thundering herd, happens when a popular cached key expires, or the cache restarts, and many requests miss at the same moment. They all go to the database to rebuild the same value, which can overload it and cause an outage. Prevent it by letting only one request rebuild while others wait or get the old value, using a lock or request coalescing; by refreshing early, before expiry; by serving stale data while refreshing in the background; and by adding jitter to TTLs so many keys do not expire together.
ts
async function getHomepage(): Promise<string> {
const cached = await redis.get('homepage')
const meta = await redis.get('homepage:fresh') // (1)
if (cached && meta) return cached
const gotLock = await redis.set('homepage:lock', '1', { NX: true, EX: 10 }) // (2)
if (gotLock) {
try {
const html = await renderHomepage() // (3)
await redis.set('homepage', html, { EX: 3600 }) // (4)
await redis.set('homepage:fresh', '1', { EX: 60 })
return html
} finally {
await redis.del('homepage:lock')
}
}
if (cached) return cached // (5)
await sleep(100)
return getHomepage()
}- Two keys: the value lives for an hour, but a separate "fresh" marker lives for one minute. After a minute the value is stale but still usable.
- Only one request wins the lock.
NXmeans "set only if absent", and the 10-second expiry frees the lock if that request crashes. - The winner does the expensive work once.
- Store the new value and reset the fresh marker.
- Everyone else returns the stale copy immediately, or, on a cold cache, waits briefly and tries again.
| Technique | How |
|---|---|
| Lock, single rebuild | SET lock NX EX, as above |
| Request coalescing | In one process, share one in-flight promise per key |
| Stale while revalidate | Serve the old value and refresh in the background |
| Early refresh | Refresh before expiry, with a probability that rises as expiry nears |
| TTL jitter | Spread expiries (choosing a TTL) |
54. What is cache warming?
The short answer
Cache warming means filling the cache with likely-needed data before users ask for it, so the first requests after a deploy, a cache restart or a new region going live do not all miss and hit the database at once. You warm the known-hot keys: the homepage, top products, configuration, popular search results. Do it from a job that reads access logs or a "top N" list, at a controlled rate, with jittered TTLs. Do not warm everything; warming data nobody reads wastes memory and pushes useful keys out.
Ways to warm:
| Method | When |
|---|---|
| A startup or post-deploy job that loads the top N keys | After cache restarts and big deploys |
| Replaying recent read traffic against a new cache node | Adding capacity, a new region |
| Refreshing hot keys on a schedule, before they expire | Data everyone reads, such as the homepage |
| Rolling restarts of cache nodes, one at a time | So most of the cache stays warm |
ts
async function warmTopProducts() {
const { rows } = await pool.query('SELECT id FROM products ORDER BY views_7d DESC LIMIT 500')
for (const batch of chunk(rows, 50)) {
await Promise.all(batch.map((r) => getProduct(r.id))) // (1)
await sleep(200) // (2)
}
}- Use the normal read path, so warming fills the cache exactly as a real request would.
- Pause between batches, so warming itself does not overload the database.
55. How do browser caching and CDN caching work?
The short answer
Both obey HTTP caching headers. The browser cache stores responses on the user's device. On the next request, if the copy is still fresh under Cache-Control: max-age, the browser uses it with no network at all; if stale, it revalidates with If-None-Match and an ETag, getting a tiny 304 Not Modified if nothing changed. A CDN is a network of servers around the world that caches responses close to users, shared across everyone, so most requests never reach your servers. CDNs follow s-maxage and public, can be purged on demand, and must never cache private responses.
The standard setup for a web app:
| Resource | Header | Why |
|---|---|---|
app.3f9a.js, with a content hash in the name | public, max-age=31536000, immutable | A new build gets a new file name |
index.html | no-cache | Always revalidate, so users get the new build's file names |
| Public API list | public, s-maxage=60, stale-while-revalidate=30 | CDN shares it; short lifetime |
| Per-user API data | private, no-store | Never in a shared cache |
Two CDN traps: a CDN that ignores Vary can serve one user's language or one origin's CORS headers to another (Vary and CDNs), and a misconfigured CDN that caches a logged-in page can serve one user's account to everyone. Mark personal responses private and verify with the CDN's cache-status response header.
56. How would you design distributed caching for a high-traffic backend?
The short answer
Layer it. A CDN for public pages and assets. A small, short-TTL in-process cache in each instance for the hottest keys. A Redis cluster with replicas as the shared cache, using cache-aside with TTLs plus invalidation on change through events. Protect the database from stampedes with single-flight rebuilds and jitter, and from hot keys with local caching or key replication. Design keys carefully, with versions and tenant IDs. Treat the cache as optional: if Redis fails, the app must degrade, with timeouts, a circuit breaker and rate-limited fallback to the database. Measure hit rate, latency and evictions.
The design, decision by decision:
| Decision | Choice and reason |
|---|---|
| Pattern | Cache-aside with TTLs; delete on change (cache-aside) |
| Key format | v3:tenant:{t}:product:{id}: version for schema changes, tenant for isolation |
| Invalidation | Database change events, from the outbox or change data capture, delete keys and broadcast |
| Hot keys | One key read 100,000 times a second overloads one Redis node: cache it locally for a few seconds, or copy it to several keys |
| Big values | Keep values small, under about 100 KB; split or compress large ones |
| Stampedes | Single-flight lock, stale-while-revalidate, jitter (question 53) |
| Failure | 50 ms timeout on Redis calls; circuit breaker; fall back to the database with a concurrency limit |
| Capacity | Memory with headroom; allkeys-lfu eviction; replicas for failover |
| Metrics | Hit rate per key prefix, p99 latency, evictions, memory, hot keys |
Numbers that make the case. At 50,000 requests per second with a 95% hit rate, the database receives 2,500 per second. Drop the hit rate to 80% and the database receives 10,000 per second, four times the load. A small fall in hit rate is a big rise in database traffic, which is why hit rate is the first metric to alert on.