Hot keys: when one entry is too popular
Keys are spread out. Requests are not.
Section titled “Keys are spread out. Requests are not.”One cache machine cannot hold or serve everything, so a cache cluster splits the keys across shards: a function of the key decides which shard owns it. Spread across enough shards, the load looks even, and adding shards adds capacity.
That works only if the requests are spread as evenly as the keys. Real traffic is not. A few keys draw far more requests than the rest, and the studies of web request logs describe popularity as “Zipf-like”, with the exponent varying from one log to the next. A key lives on one shard, so a very popular key puts all of its traffic on one machine. Adding shards does not move it.
Facebook’s memcache paper gives a sense of the scale: they note that a single key can account for 20% of a server’s requests. A server that holds such a key is much busier than its neighbours, however well the other keys are balanced.
See it
Section titled “See it”Here the cache has four shards, and each can answer about a thousand operations per second. Traffic is 2,000 requests per second, and one key takes 40% of it.
Keys are spread over four cache shards. When one key draws a large share of the traffic, the shard that owns it saturates while the others stay quiet. Spread the key over several shards and compare.
- Cache hit
- Cache miss, goes on to the database
- Waits for another request’s read
- Request fails
Each dot stands for many requests. The numbers show the real rate.
Cache shard 3 is 100% busy while the other shards average 29%. One hot key can saturate a shard that has plenty of neighbours.
History
p99 latency
106 msUse the left and right arrow keys to read values at each moment.
Database reads per second
64/sUse the left and right arrow keys to read values at each moment.
Hit ratio
100%Use the left and right arrow keys to read values at each moment.
Data table
| Simulated time | p99 latency | Database reads per second | Hit ratio |
|---|---|---|---|
| 5 s | 120 ms | 168/s | 100% |
| 10 s | 115 ms | 226/s | 100% |
| 15 s | 106 ms | 64/s | 100% |
| Component | State | Utilisation | Queue |
|---|---|---|---|
| Cache, Shard 1 | Healthy | 26% | 1 |
| Cache, Shard 2 | Healthy | 38% | 0 |
| Cache, Shard 3 | Overloaded | 100% | 79 |
| Cache, Shard 4 | Healthy | 23% | 0 |
| Database | Healthy | 4% | 0 |
- Traffic
- 2K/s
- Successful
- 2K/s
- Error rate
- 0%
- Hit ratio
- 100%
- Database reads
- 135.8/s
- p50
- 80 ms
- p95
- 110 ms
- p99
- 120 ms
- Entries cached
- 200 / 200
- Requests that write
- 0%
- Stale hits
- 0%
- Coalesced requests
- 0/s
- Skipped the cache
- 130.4/s
Figures describe the last 5 simulated seconds.
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- Each request asks for a key drawn independently from a fixed popularity distribution. Real traffic has bursts and trends that this does not capture.
- Every operation on a cache shard (lookups, stores, deletions) joins one queue served by a few slots, so a busy shard slows everything on it.
- Each shard serves about 1,000 operations per second. Reads, stores, and deletions each take one slot.
Simplifications
- An entry’s lifetime starts when it is stored. Reading it does not extend it.
- Only a version number is tracked for each key, not the data. A cached copy is stale when its version is behind the database’s.
- The cache never runs out of memory, so no entry is evicted early. Real caches evict the least recently used entries when they are full.
- A write updates the database and then deletes the cached copy (cache-aside). Write-through and write-behind are compared in the trade-off explorer, not simulated here.
- Refresh before expiry reloads an entry in the background when a request reads it in the last part of its life. Production systems often use a probabilistic variant.
- A cache that goes down loses everything it held and comes back empty. A cache with persistence would not.
- The application learns that the cache is down at once, on the request that fails. Health checks and circuit breakers are not modelled.
- When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
Parameter values are illustrative. They are not benchmarks of any real product.
Watch the diagram’s cache shards and the p99 latency chart.
- One shard is saturated. With the starting settings it runs at its limit while the others run at about 25% to 37% of theirs.
- The 99th-percentile latency is about 120 milliseconds, instead of about 10 when no key is dominant. Move Share of traffic on the hot key to 0% and compare.
- Look at Database reads. They are about 177 a second, against about 7 when no key is dominant. A shard that is too busy to answer behaves, to the application, like a cache that is not there, and in this lab the application then goes to the database. The requests the shard turns away end up on the database, which had nothing to do with the hot key.
The innocent bystanders matter too. Every other key owned by the saturated shard suffers the same delay, even though nobody is asking for them in unusual numbers.
Spread the key
Section titled “Spread the key”Move Copies of the hot key (shards) to 4. The hot key is now stored on all four shards, and each request goes to one of them. The shards settle at about half of their capacity each, the latency falls to about 10 milliseconds, and the database reads fall back to about 7 per second.
With 2 copies the saturation is already gone for this load, and the latency is about 16 milliseconds. With a 20% share and 1 copy the shard runs at about 90% and the latency is about 35 milliseconds, so the problem does not wait for an extreme share to start.
The technique is key replication: store the value under several keys (product:42#0, product:42#1, and so on), and have each reader pick one at random. Facebook’s memcache paper describes replicating a category of keys within a pool when the request rate is much higher than one server can handle. Their case is mostly requests that fetch many keys at once from a small data set, not one dominant key, so it is the closest published example of the technique and not of this exact problem.
It has a price, which their paper states plainly: invalidations must be delivered to every replica to keep them consistent. A write now deletes four copies, not one, and for a moment the copies can disagree.
Other ways to cope
Section titled “Other ways to cope”- A small local cache in front. Each application instance keeps the hottest entries in its own memory for a very short time, a few seconds, and serves most requests without touching the shard at all. This removes the load rather than spreading it. The cost is that every instance has its own copy, which can be stale for as long as that short time, and you cannot invalidate it from a distance as easily.
- Don’t let one key carry everything. If a counter is updated and read for every request, split it into several counters and add them up when someone needs the total. The traffic is spread by design.
- Don’t make it worse when a shard fails. When a cache server fails, it is tempting to spread its keys over the remaining servers. Facebook’s engineers point out that this can cascade: the server that inherits a hot key can be overloaded in turn. Lesson 5 returns to this.
Request coalescing, from the last lesson, does not help here. A hot key that is in the cache is a stream of hits, and coalescing only merges misses.
How dominant will the top key be?
Section titled “How dominant will the top key be?”If popularity follows a Zipf distribution with skew s over a million keys, this is the share of all requests that go to the single most popular key:
| Skew | Share of all requests for the top key |
|---|---|
| 0.8 | 1.3% |
| 1.0 | 7% |
| 1.2 | 19% |
| 1.5 | 38% |
| 2.0 | 61% |
So a key with 40% of the traffic is not typical of moderate skew. It takes a very steep distribution, or an event: a celebrity posts, a product goes viral, a sale begins. The lab’s 40% is a share added on top of the ordinary popularity, standing in for exactly that. The lesson is not to guess a share. It is to find out how skewed your own traffic is, and how fast it can change.
What happens if the key gets hotter?
Section titled “What happens if the key gets hotter?”Set Copies of the hot key back to 1 and move Share of traffic on the hot key to 80%. The shard that owns it is saturated, and the others drop to about a tenth of their capacity. The database now serves about 740 reads a second, nearly all of them requests the saturated shard turned away. Put Copies of the hot key at 4 and the picture is balanced again, with the latency at about 9 milliseconds.
Adding a fifth, sixth or seventh shard to the cluster would not have changed the first picture at all.
Trade-offs
Section titled “Trade-offs”| Approach | What it protects | What it costs | When it is not enough |
|---|---|---|---|
| Replicate the hot key | The shard that owns it | Memory for each copy, and every write must invalidate every copy | A key so hot that the copies themselves are saturated |
| Small local cache | The shard, and the network too | Staleness up to the local lifetime, and no easy remote invalidation | Data that must never be stale |
| Split the key | The shard, by design | More complex reads and writes | Keys that cannot be divided |
| More shards | The cluster’s total capacity | Money and operations | One key that dominates: the shard that owns it does not change |
- Keys are spread across shards. Requests are spread only as evenly as the keys’ popularity.
- One dominant key saturates the shard that owns it, slows the unrelated keys that share it, and sends the requests it turns away to the database.
- More shards do not help a single hot key. Copies of the key do.
- Replicating a key costs memory and makes invalidation touch every copy. A short-lived local cache removes the load, at the price of local staleness.
- How dominant the top key is depends on the skew of your traffic, and it can change in minutes.
Next: the cache itself goes away, and what the application does next decides whether you have a slow afternoon or an outage.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.