Hot keys and the celebrity problem: an interactive shard load simulator
Sharding spreads keys evenly across machines, but not traffic. A hot key, such as a celebrity’s profile, a viral post’s like counter or a flash sale’s inventory, sends a large share of all requests to the one shard that owns it, which overloads while the rest sit idle. Adding shards does not help, because the key still lives on one of them. The fix depends on whether the heat is reads or writes.
The playground sends 50,000 operations a second to 8 shards of 10,000 each and points a share of them at one key. Change how hot it is and how many of its operations are writes, and compare five strategies.
50,000 operations a second across 8 shards that each handle 10,000; 30% of them, 15,000 a second, hit one key, and 5% of those are writes. Left alone, that key's shard runs at 194% of capacity while the others idle at 44%. The traffic is mostly reads, which a cache or replicas spread out; splitting the key backfires, because every read now reads all 8 pieces and every shard overloads. Strategies that keep every shard under capacity: cache it, replicate it, split and cache.
- Busiest shard
- 194%
- Over capacity
- 9,375/s
- Extra operations
- none
- Stale reads
- none
- Busiest shard
- 52%
- Over capacity
- none
- Extra operations
- none
- Stale reads
- up to 1 s
- Busiest shard
- 87%
- Over capacity
- none
- Extra operations
- 2,250/s
- Stale reads
- replication lag
- Busiest shard
- 187%
- Over capacity
- 69,750/s
- Extra operations
- 99,750/s
- Stale reads
- none
- Busiest shard
- 45%
- Over capacity
- none
- Extra operations
- 350/s
- Stale reads
- up to 1 s
Reads: copy the key
When 30% of traffic reads one key, its shard runs at 194% of capacity while the other seven idle at 44%. Reads can be answered by copies. A cache in each app server, refreshed every second, turns 14,250 reads a second into 50 refreshes and brings the shard down to 52%, at the price of reads up to a second stale. Replicating the key to 4 shards spreads the reads, 87%. Splitting the key is the wrong tool here: each read has to fetch all 8 pieces, and every shard ends up at 187%.
Writes: split the key
When the same traffic is 95% writes, a cache does nothing, 187%, and replicas make it worse, since every copy must apply every write. Writes need the key split into sub-keys on different shards: a write picks one sub-key at random, and a read adds them all up. For a like counter that brings the busiest shard to 69%. With heavy reads and heavy writes together, no single fix is enough, and the answer is both: split the key for writes and cache its total for reads, 54% here.
- Find hot keys before they find you. Sample the keys each shard serves, or track the top keys with a count-min sketch, and alert on any key taking a large share.
- Plan for known ones. Celebrities, big customers and launch-day products are predictable, and can be split or cached in advance.
- Buffer writes. For counters, aggregate increments in memory or a queue and write a total every second, a write-back approach.
- Protect the shard anyway. Per-key rate limits and request coalescing stop one key from taking the shard down for every other key on it.
A split counter with a cached total
import random, time
import redis
r = redis.Redis() # a Redis Cluster in production: each sub-key hashes to its own slot
SPLIT = 8
_cache = {} # per app server: key -> (value, fetched_at)
def like(post_id):
# spread writes over SPLIT sub-keys, so no single shard takes them all
r.incr(f"likes:{post_id}:{random.randrange(SPLIT)}")
def like_count(post_id, ttl=1.0):
hit = _cache.get(post_id)
if hit and time.monotonic() - hit[1] < ttl:
return hit[0] # most reads never leave the app server
keys = [f"likes:{post_id}:{i}" for i in range(SPLIT)]
total = sum(int(v or 0) for v in r.mget(keys)) # one read of every piece
_cache[post_id] = (total, time.monotonic())
return totalIn a system design interview
Bring up hot keys yourself whenever traffic follows popularity: “Most posts get a few likes, but a viral one can take thousands a second, all on one shard. So a post’s like counter is split into 16 sub-keys once it crosses a threshold, writes go to a random sub-key, and the displayed count is the sum, cached for a second. Reads of hot posts are served from the CDN and app caches.” Expect the follow-ups: how you detect a key turning hot, how reads of a split counter stay cheap, and what happens to ordering and exact counts.
Read the guide to hot keys and skew or counting at scale, see choosing a shard key, or watch a strong candidate design a news feed.
Questions people ask
What is a hot key?
A key that receives a disproportionate share of requests, such as a celebrity’s profile or a viral post’s counter. Because each key lives on one shard, that shard overloads while the others have spare capacity, and adding more shards does not help.
How do you handle a hot key that is mostly read?
Serve the reads from copies. Cache the key in application servers or a CDN for a short time, or replicate it to several shards or read replicas and spread reads across them. The cost is that reads can be slightly stale.
How do you handle a hot key that is mostly written?
Split it into several sub-keys on different shards, often by appending a random suffix, so each write goes to one of them. Reads then fetch every sub-key and combine them, which is cheap for counters and sums. Buffering writes in memory and flushing totals periodically also helps.
Why does caching not fix a write-heavy hot key?
A cache serves reads, but every write still has to reach the key’s owner to be durable. Replicas make write-heavy keys worse, because each copy has to apply every write.
What is key salting?
Adding a suffix or prefix, such as a random number from 0 to N, to a hot key so that it becomes N different keys that hash to different shards. Writes spread across them; reads must query all N and merge the results.
How do you detect hot keys?
Sample the keys each shard serves and track the most frequent, for example with a count-min sketch or a top-k structure, and alert when one key takes a large share of a shard’s traffic. Redis has a hot keys option in redis-cli that uses its LFU counters.
What is the celebrity problem?
The system design name for hot keys caused by very popular users: a celebrity’s posts are read by millions and their counters written by thousands at once, overloading the shard that holds them. Designs handle celebrities separately, for example by not fanning out their posts on write and by splitting and caching their counters.