Skip to content

Interview PrepCaching · Advanced

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 snapshotAOF log
SavesThe whole dataset at a momentEvery write command
Data lost on crashSince the last snapshot, often minutesAbout 1 second with everysec
File sizeSmallLarger; rewritten in the background to shrink it
Restart speedFastSlower, replays commands
CostFork and copy-on-write memory during a snapshotA 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 loading

Choosing a setting:

Redis is used asPersistence
A pure cacheNone; it can be rebuilt from the database
Sessions, rate limitsAOF everysec; losing a second is acceptable
A queue or the only copy of dataAOF 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)
  1. A separate connection for subscribing, because a subscribed connection cannot run normal commands.
  2. Every app instance clears its in-memory copy when told.
  3. 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/SubStreams
Messages storedNoYes, until trimmed
Missed if offlineYesNo; read from where you left off
AcknowledgementsNoYes, with consumer groups
Load balancing across workersNo; everyone gets everythingYes; each message to one consumer in a group
Use forLive broadcasts, invalidationJob 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.

clientCRC16(key) mod 16384primary Aslots 0–5460primary Bslots 5461–10922primary Cslots 10923–16383replica of Areplica of Breplica of Ca key in slot 9000
Slots, not keys, are assigned to nodes. The client hashes the key to a slot and sends the command to the primary owning that slot. Moving slots between nodes is how the cluster grows. Replicas take over if their primary fails.

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
SetupGivesUse when
Single nodeSimplicitySmall caches, development
Primary with replicas and SentinelAutomatic failover, no shardingData fits on one node
Redis ClusterSharding plus failoverData or traffic exceeds one node
Managed serviceThe above, run for youMost 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 policyRemovesUse for
noevictionNothing; writes fail when fullRedis as a data store
allkeys-lruLeast recently used, any keyGeneral caches
allkeys-lfuLeast frequently used, any keyCaches with a stable set of popular items
volatile-lru, volatile-lfuAs above, only keys with a TTLMixed cache and permanent data in one instance
volatile-ttlKeys nearest to expiryWhen TTLs reflect value
allkeys-random, volatile-randomRandom keysRarely the best
text
maxmemory 4gb
maxmemory-policy allkeys-lfu

Approximate, 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.

stampede2,000 requestskey expireddatabase: 2,000 queriesone rebuild2,000 requests1 gets the lockrest: stale copydatabase: 1 query
One rebuild instead of two thousand. On the left, every request that misses goes to the database. On the right, the first request takes a short lock and rebuilds; everyone else gets the previous value until the new one is ready.
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()
}
  1. 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.
  2. Only one request wins the lock. NX means "set only if absent", and the 10-second expiry frees the lock if that request crashes.
  3. The winner does the expensive work once.
  4. Store the new value and reset the fresh marker.
  5. Everyone else returns the stale copy immediately, or, on a cold cache, waits briefly and tries again.
TechniqueHow
Lock, single rebuildSET lock NX EX, as above
Request coalescingIn one process, share one in-flight promise per key
Stale while revalidateServe the old value and refresh in the background
Early refreshRefresh before expiry, with a probability that rises as expiry nears
TTL jitterSpread 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:

MethodWhen
A startup or post-deploy job that loads the top N keysAfter cache restarts and big deploys
Replaying recent read traffic against a new cache nodeAdding capacity, a new region
Refreshing hot keys on a schedule, before they expireData everyone reads, such as the homepage
Rolling restarts of cache nodes, one at a timeSo 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)
  }
}
  1. Use the normal read path, so warming fills the cache exactly as a real request would.
  2. 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.

browserapp.3f9a.jsfresh 1 year: no requestCDN edge, nearbyhit: ~20 msshared by all usersyour originonly on a CDN miss/api/productsmiss
Two caches before your server. A fingerprinted file is served straight from the browser's cache. A shared API response is served from the nearest CDN server, and only a miss travels to your origin.

The standard setup for a web app:

ResourceHeaderWhy
app.3f9a.js, with a content hash in the namepublic, max-age=31536000, immutableA new build gets a new file name
index.htmlno-cacheAlways revalidate, so users get the new build's file names
Public API listpublic, s-maxage=60, stale-while-revalidate=30CDN shares it; short lifetime
Per-user API dataprivate, no-storeNever 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.

usersCDNpublic pagesapp instanceslocal LRU, 5 s TTLsingle-flight rebuildtimeouts, breakerRedis cluster+ replicasdatabasechange event → delete Redis key→ broadcast: clear local copies
Each layer absorbs most of the load before the next. The CDN takes public traffic. Local caches take the hottest keys. Redis takes the rest. The database sees only misses and writes. A change in the database flows back out as invalidations.

The design, decision by decision:

DecisionChoice and reason
PatternCache-aside with TTLs; delete on change (cache-aside)
Key formatv3:tenant:{t}:product:{id}: version for schema changes, tenant for isolation
InvalidationDatabase change events, from the outbox or change data capture, delete keys and broadcast
Hot keysOne 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 valuesKeep values small, under about 100 KB; split or compress large ones
StampedesSingle-flight lock, stale-while-revalidate, jitter (question 53)
Failure50 ms timeout on Redis calls; circuit breaker; fall back to the database with a concurrency limit
CapacityMemory with headroom; allkeys-lfu eviction; replicas for failover
MetricsHit 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.

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Interview Prep
Databases & Caching
Foundations
SQL
Performance
Concurrency
Scaling
Caching
Other topics
Nothing picked yet