← Building blocks

Spread a hot key

Latency & performancesenior

One celebrity record or one counter overwhelms the single cache node that owns it.

Say it like this

One key is taking more traffic than a single cache node can serve, so I replicate it under suffixed names across nodes and add a tiny per-server cache, trading brief staleness on that key for linear read capacity.

Basic solution

Replicate the key under suffixed names (key#0..N) and read a random copy; or add a tiny in-process cache in each app server.

local caches (Caffeine, Guava)key suffixingRedis replicas

Three layers between a viral key and one cache node

The in-process cache absorbs most reads, the remaining ones pick a random suffixed copy across nodes, and writes fan out to every copy. Only the misses reach the source.

How it works

  1. Detect the hot key: per-key request counts on the cache client (a Count-Min sketch or a sampled counter) show one key receiving far more reads than any other.
  2. Fan the key out: instead of storing post:42 once, store sixteen copies, post:42#0 through post:42#15. Each name hashes differently, so the sixteen copies land on different cache nodes.
  3. Reads pick a random suffix: hash(post:42 + random 0..15) selects one of the sixteen copies, so the read load for the key spreads across sixteen nodes instead of landing on one.
  4. Writes update all sixteen copies in one pipelined call, or the copies carry a short TTL so each one refreshes from the source within seconds of a change.
  5. In front of all of that, keep a small in-process cache (a few thousand entries, TTL of 100–500 ms) in every app server, so the hottest keys are answered from the server's own memory and never leave the process.

Why this and not a bigger node

  • A single key's traffic cannot be split by sharding because every request for that key hashes to the same node; only replication, having copies on several nodes, divides it.
  • The in-process layer removes the network hop entirely and its capacity grows with the app fleet.

Worked example

A celebrity post in the news feed

  • A post from an account with 50M followers appears in millions of timelines; hydration requests for its body hit one cache key at ~200K reads/s within a minute.
  • A Redis node sustains ~100K simple GETs/s, so one node is saturated twice over and every timeline that contains the post slows down, not only the celebrity's followers.
  • Replicate the post body under 16 suffixes across a 32-node cluster: ~12.5K reads/s per copy, comfortably inside one node's budget.
  • In-process cache in 400 app servers with a 200 ms TTL: each server asks Redis for the post at most 5 times a second, so Redis sees at most 2,000 reads/s for that key in total, and the suffix copies are barely needed.
  • Staleness: an edit to the post takes up to 200 ms to appear everywhere, plus the TTL on the Redis copies if writes do not fan out. Acceptable for a post body; not acceptable for an account balance.

Variants and when each wins

Suffixed copies in the distributed cache

  • The same value stored under several key names so it spreads across nodes, with reads picking one at random. It wins when one node is still saturated after an in-process cache; writes must touch every copy or rely on TTL expiry.

In-process (near) cache per app server

  • A tiny cache inside each server's memory with a TTL of a few hundred milliseconds. It wins as the first move for read-mostly hot keys, because it removes the network hop and scales with the fleet; the cost is staleness up to its TTL.

Read replicas of the cache node

  • Redis replicas that serve reads for every key on that node. They win when the whole node is hot, not when one key is; replication lag applies.

Client-side request coalescing

  • Simultaneous misses for the key are collapsed into one fetch. It wins against the miss storm when the key expires; it does nothing for steady-state hit volume.

Pick the in-process cache first for read-mostly hot keys; add suffixed copies when one node is still saturated; use replicas when the whole node, not one key, is hot.

Failure modes

Copies drift

  • Users see different versions of the same post for a few seconds. It happens because a write updated one copy and the other fifteen are waiting for their TTL to expire. Fix it by writing all suffixes in one pipeline, or by publishing an invalidation that deletes every copy at once.

In-process cache serves stale after a delete

  • A deleted post keeps appearing for some users. It happens because each app server holds its own copy with its own TTL. Fix it by keeping that TTL short (hundreds of ms), or by broadcasting invalidations over pub/sub so every server drops the entry immediately.

Hot key not detected

  • One cache node is pegged and nobody can say which key is responsible. It happens because there are no per-key metrics. Fix it by sampling requests on the client into a top-k sketch and alerting when any key exceeds a per-node budget.

Memory blow-up from suffixing everything

  • Cache memory doubles. It happens because every key was replicated, not only the hot few. Fix it by replicating only the keys the detector flags, and expiring the copies when their traffic falls.

Hot write key

  • A counter (likes on the same post) saturates a node and replication does not help. It happens because writes cannot be spread by copying; every increment must land on the one authoritative counter. Fix it by sharding the counter into buckets that are incremented at random and summed on read, or by batching increments locally and flushing them periodically.

Interviewer follow-ups

Answer out loud before you open each one.

Why does sharding not solve a hot key?

Sharding places each key on exactly one node by hashing it, so all traffic for that key lands on that node no matter how many nodes there are. Only replication, having the same key on several nodes, divides the read load.

How do you find the hot keys in the first place?

Instrument the cache client to sample requests into a top-k sketch per server and aggregate centrally, or use the cache server-side hot key report if it has one. Alert when a single key exceeds a fraction of one node capacity, say 20%.

How do you keep sixteen copies consistent on a write?

Either the writer updates all suffixes in one pipelined call, or the writer deletes them all and the next reads repopulate. For post bodies a short TTL on the copies is also fine. For anything that must be exact, such as a balance, do not replicate the key; serve it from the source with a lock.

What about a hot write key such as a like counter?

Copies do not help writes. Split the counter into N sub-keys, increment a random one, and sum them on read; or have each app server accumulate increments locally and flush every 100 ms as one INCRBY. Both trade exact real-time counts for throughput.

Connections

In study paths
Scaling reads
Built on
LRU cache: hash map + doubly linked list · Count-Min sketch
Pairs with
Protect against a cache stampede · Fix a hot shard · Collapse duplicate in-flight requests · Cache the hot reads · Batch small operations
Pulls against
Do read-modify-write atomically
Used in
Distributed Key-Value Cache (Redis / Memcached-like)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon EC2 Auto ScalingApp serverCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon AuroraPosts storeAlloyDB for PostgreSQL, Cloud SQLAzure Database for PostgreSQL or MySQLPostgreSQL, MySQL, CockroachDB