Module 3 / Caching / Distributed caches and hot keys

Lesson9 minModule 3 · lesson 7 of 10

Distributed caches and hot keys

When one cache node is not enough, how keys are spread, and what to do when one key is too popular.

A single cache server is limited in memory and in requests per second. When your working set no longer fits, or traffic outgrows one machine, you spread keys across several cache nodes. The client (or a proxy) must decide which node owns a key, and everyone must agree, otherwise a key is stored on one node and looked for on another.

The naive rule is node = hash(key) mod N. It works until N changes. Going from 4 nodes to 5 keeps a key in place only when hash mod 4 equals hash mod 5, which is true for 1 in 5 keys. So adding one node moves about 80 percent of all keys, and 80 percent of requests suddenly miss. Consistent hashing fixes this: keys and nodes are placed on a ring, each key belongs to the next node clockwise, and adding or removing a node only moves the keys next to it, roughly 1/N of them.

App serversHash ringCache node 1Cache node 2Cache node 3hash(key)
Keys map to nodes through a hash; consistent hashing limits what moves when nodes change.

That is the hot key problem. Suppose a celebrity restaurant's page key receives 150,000 reads per second, and one cache node can serve about 100,000. Adding nodes does nothing, because the hash always sends that key to the same node. The solutions copy the hot key: keep a tiny in-process local cache (even 1 second long) in each app server so most reads never leave the machine, or store the key under several names such as rest:7#1, rest:7#2, rest:7#3 and have each request pick one at random. Three copies turn 150,000 reads per second into 50,000 per node.

Handling a hot key

Step 1 of 3

Detect it

Track the top keys by request rate, or watch for one node with far more load than its peers.

Question 1

You go from 4 cache nodes to 5 using hash(key) mod N. About what share of keys change node?

Question 2

One cache key receives more traffic than any node can serve. What helps most?

Your turn

In your ticket shop, one concert goes on sale and its event page gets 80,000 reads per second while a cache node handles about 30,000. Propose a fix with numbers.

Stuck? Divide the load among copies or avoid reaching the cache at all.

What to remember

  1. Distributed caches spread keys over nodes; clients must agree on which node owns a key.

  2. Hash-mod-N remaps most keys when N changes; consistent hashing moves only about 1/N.

  3. A single very popular key defeats sharding, so copy it (local caches or replicas).

  4. Losing a cache node sends its load to the database, so keep headroom.