Sharding & Hot Keys
Partitioning spreads keys, not load.
Sharding splits data so each node handles a slice. It scales evenly distributed traffic — but a single hot key always lives on one node, no matter how many you add.
Concepts
- Hash partitioning
- Keys are spread across nodes by hash; adding nodes moves some keys.
- Hot keys
- A celebrity post or a flash-sale item concentrates traffic on one shard.
- Mitigations
- Local caches, key replication or splitting hot keys spread the load again.
Lab
Problems
- WhatsApp: Message Delivery — Medium — A flaky network link to Kafka, then a Cassandra compaction storm.
- Twitter: Celebrity Tweet — Medium — A celebrity tweets: traffic doubles and one Redis key gets half of all reads.
- Live Leaderboard: The World Final — Hard — The world final starts: four times the spectators on live connections, twice the score updates, and the final’s leaderboard becomes the hottest partition in DynamoDB.
- Slack: The All-Hands Announcement — Hard — The CEO posts in #general: everyone comes online at once, #general becomes the hottest partition in Cassandra, and then a zone of the gateway fleet disappears.
- Instagram: The Celebrity Post — Hard — A celebrity posts a photo: one image takes 70% of image traffic, and the post’s reads and likes all land on one Cassandra partition — likes cannot be cached.
- Digital Wallet: Payday Friday — Hard — Payday: balance checks and transfers ×2.5 on a distributed SQL ledger with a per-node connection limit — then the bank’s top-up API slows to a crawl behind two retries.
- The Live Chat Room — Easy — A live event doubles chat traffic and a third of it lands in one room’s partition. More Cassandra nodes cannot spread one partition.
- Writes That Postgres Couldn’t Take — Easy — A promo doubles write-heavy traffic and the API autoscales into the per-node connection limit. Every CockroachDB node is a writer and a gateway.
- Throttled at 30,000 RCU — Easy — A flash-sale item makes 40% of reads and writes hit one partition key. Reads and writes throttle separately — and raising RCU makes the reads worse.
- Pokémon GO: Fifty Times the Plan — Hard — Launch day brings eight times the worst case you planned for. Clients retry three times, one park has every spawn, the world database is write-bound and map tiles melt their API.
- Uber: New Year’s Eve at Midnight — Hard — At midnight, five times the riders open the app in the same few city blocks, drivers double their pings, surge prices update on one hot partition — and the payment provider throttles. You know exactly when it starts.
- Betting Exchange: The Last-Minute Goal — Hard — A goal in the last minute: six times the punters for forty seconds, all on one market, with a 120 ms latency budget. Nothing boots in time, the ledger cannot grow, and a lost bet is a regulatory incident.
- Payments: The Hot Merchant Account — Hard — A merchant goes viral: payments double and a third of them update one balance row. Then the fraud vendor browns out, and month-end reconciliation starts scanning the ledger. Every payment must be exact.
Codelays runs in your browser: turn on JavaScript to use it.