When hashing is not enough
Hash sharding spreads keys evenly, but not necessarily traffic. If one key is vastly more popular than the rest, the shard that owns it melts while its neighbors idle. That key is a hot key, and it is a real, recurring production problem.
Classic examples:
- A celebrity's profile on a social network (one user_id, millions of readers)
- A viral post or video
- A flash-sale product page
No resharding scheme fixes this, because the load is concentrated in a single key that must live somewhere. The standard remedies:
- Cache it. Unit 3 to the rescue: a popular key is exactly what caches are best at
- Split the key. Store
celebrity#1,celebrity#2, ... on different shards and pick one at random per read - Replicate the hot data extra times and spread reads across the copies
Code exercise · python
Run this hot key demonstration. 9,000 requests hit one celebrity key and 1,000 requests spread across 1,000 normal users. Watch the per-shard request counts.
Problem
In the simulation above, shard 0 handled 9,216 of 10,000 requests. Roughly what percent of all traffic did one shard absorb? (Answer with the whole percent, like 78)
Quiz
A flash sale makes one product page receive 95% of site traffic for an hour. Which response uses what you already built in units 2 and 3?