Caching
Serve hot data from memory — and survive the day the cache goes away.
A cache in front of the database absorbs most reads, so the database is sized for the misses. That works until the cache restarts cold, a key gets hot, or every miss arrives at once.
Concepts
- Cache-aside
- The service reads the cache first and falls back to the database on a miss; writes go to the database.
- Hit rate
- The share of reads the cache answers. At 95%, the database sees 1 read in 20 — at 80% it sees 4× more.
- Cold cache & stampede
- After a restart every read misses at once. Stampede protection lets one request refill a key while the others wait.
- Multi-tier caching
- An in-process local cache (L1) in front of Redis (L2) absorbs hot keys with no network hop.
Lab
Problems
- URL Shortener — Easy — A read-heavy redirect service loses its cache. Watch the database melt.
- Twitter: Celebrity Tweet — Medium — A celebrity tweets: traffic doubles and one Redis key gets half of all reads.
- Netflix: Season Premiere at Midnight — Hard — Midnight premiere: five times the viewers, every edge cold for the new episode, an origin that cannot scale — and then recommendations slow to a crawl.
- Search Autocomplete: The Name Everyone Types — Hard — News breaks: searches triple, a third of them start with the same name, and the trending pipeline floods the index with updates. Suggestions must stay fast and fresh.
- 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.
- Ad Click Aggregator: The Big-Game Commercial — Hard — A commercial airs to 100 million viewers: clicks ×6 for the rest of the hour, and then every advertiser opens the live dashboard at once. Two consumer groups, one shard limit, one analytical database.
- 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.
- AI Assistant: Launch Day — Hard — The assistant goes viral: five times the questions, a vector database that cannot grow this week, an LLM provider with a hard quota — and then that provider slows down.
- Datadog: The Agent Reconnect Storm — Hard — A network blip ends and a million agents flush their buffers at once — while every engineer opens the same dashboards to see what happened.
- Stripe: The Noisy Tenant — Hard — One integration starts a runaway backfill at 15× its normal rate, and then the identity provider that validates every API key goes down for fifteen minutes.
- Planned FLUSHALL — Easy — A key-format migration will flush Redis in 20 seconds. Postgres can absorb a fifth of the reads — not all of them.
- Celebrity in 15 Seconds — Easy — In 15 seconds a celebrity posts and 70% of reads ask for one key. Adding Redis nodes cannot save the node that owns it.
- Launch Day, One Hot Item — Easy — A launch multiplies catalog reads by 5 and half of them ask for one item. A Redis cluster tops out below the load at any size.
- The CMS That Cannot Scale — Easy — In 40 seconds a reindex job multiplies reads 8×. The vendor CMS serves 22,000 requests/s — and that is all it will ever serve.
- GameDay: Lose a Zone — Easy — The chaos team takes down a zone at T+20s. The session grid rejects what it cannot hold, and restarting it logs everyone out.
- Fastly: When the CDN Disappeared — Hard — Your only active CDN returns errors worldwide. A second CDN is under contract but cold, the origin can serve a fifth of the traffic, and every second counts.
- GitHub: Forty-Three Seconds — Hard — A 43-second partition made the database fail over to the other coast. Every query now crosses the country, the new primary is still catching up — and background jobs want the same write capacity as users.
- Netflix: Christmas Eve — Hard — On Christmas Eve the primary region’s load balancers lose their state. The other region has half the instances, a cold cache, a small recommendations fleet — and two seconds to take everyone.
- Roblox: The Service Registry Meltdown — Hard — The service registry every call depends on slows to a crawl, health-check agents keep writing to it, and restarting it does not help. Players must keep playing.
- Microservices: The Cascade — Hard — A deploy drops an index: one database gets slow. Three layers of retries, timeouts below the new latency and a non-critical dependency on the critical path turn it into a full outage.
- 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.
- Fortnite: 3.4 Million Concurrent — Hard — A new season launches: three times the players at once. The profile database runs out of write capacity, the presence service out of connections, and every login hammers the token service.
- Hotstar: Twenty-Five Million for the Final Over — Hard — A cricket final: traffic doubles at the toss and jumps fivefold when the star walks in. Autoscaling is minutes too slow, ranking cannot scale at all, and everyone reads the same live score.
- Black Friday: The Whole Day — Hard — Doors open at T+150s — and then the payment provider browns out, the catalog cache crashes and a zone disappears, one after another. Instances take two minutes to boot. One plan has to survive all of it.
- Cutover Night: Migrating the Core — Hard — Move all traffic from the legacy core to the new stack tonight. The new stack is cold, its first real traffic exposes a latent bug, the morning starts at T+240s, and the legacy hardware is switched off at T+300s.
- The Outage Is Over — Now Bring It Back — Hard — The database comes back after ten minutes down — cold, at half capacity. Two million devices flush their offline queues at once, the cache has expired, and the backlog must be gone before the next shift. Recover too fast and you are down again.
- 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.
- Active-Active, Half-Broken — Hard — Two regions share the traffic. East starts dropping packets to its database — health checks stay green — the evening peak arrives, and then West’s cache restarts. Moving everyone to West is the trap.
- Inference Platform: The GPU Shortage — Hard — A viral demo quintuples the free tier, a customer starts a batch backfill, and then half the GPUs disappear for ninety seconds. There are no more GPUs anywhere — and the paid customers have an SLA.
- Social Network: Election Night — Hard — Results night: three times the readers, five times the posts, half of all timeline reads on two candidates, and the push provider slows down. Fan-out, hot keys, search and notifications all at once.
Codelays runs in your browser: turn on JavaScript to use it.