Databases
Reads scale out, writes scale up — until you shard.
A single primary handles every write. Read replicas multiply read capacity but not write capacity; sharding splits both. Connection limits and failover decide how the database behaves on its worst day.
Concepts
- Primary / read replicas
- Writes go to the primary; replicas serve reads with a small replication lag.
- Write cost
- A write costs several reads (indexes, WAL, fsync) — write-heavy workloads saturate the primary first.
- Sharding
- Partitioning data across primaries multiplies write capacity, at the cost of cross-shard queries.
- Failover
- Promoting a replica takes seconds; restarting a crashed primary takes much longer.
Lab
Problems
- URL Shortener — Easy — A read-heavy redirect service loses its cache. Watch the database melt.
- E-commerce Flash Sale — Hard — Black Friday: 5× traffic, and every order hits a single MySQL primary.
- Ticketmaster: Stadium Tour On-Sale — Hard — The biggest tour of the year goes on sale at T+20s: six times the fans, a scalper botnet, and every seat of the show in one database partition. Be ready before the doors open.
- Dropbox: Monday 9 a.m. — Hard — Monday morning: sync traffic ×2.5, uploads ×2.5 — and at T+90s the metadata primary dies. Every request counts, so the system must be ready before nine.
- 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.
- 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.
- DoorDash: Friday Dinner Rush — Hard — Dinner rush triples orders for three minutes — more writes than the orders primary can take — and halfway through, the payment provider browns out.
- Booking.com: Holiday Search Surge — Hard — Holiday deals go live: searches ×5. The search index, the search tier’s threads and the availability database’s connections all run out — each for a different reason.
- 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.
- The Primary’s Disk Is Dying — Easy — SMART errors say the primary’s disk will fail within a minute. A restart is 30 s of total outage; a failover is 6 s — still too long on its own.
- Replicas Don’t Take Writes — Easy — A flash sale triples orders for 90 seconds. MySQL’s primary is the only node that can write — and it has 151 connections.
- One Seller, One Shard — Easy — Traffic doubles and 15% of all writes go to one viral seller, whose listings live in one chunk range. Doubling the shards is nowhere near enough.
- Readers on Demand — Easy — A deploy will change the cache key prefix at T+20s: every read misses for a minute. Aurora can add readers in moments — if you add enough.
- Search Goes Prime Time — Easy — A TV spot quadruples searches. Primary shards are fixed at index creation, indexing keeps running, and search-api’s threads wait on every slow query.
- Too Many Parts — Easy — Every service inserts its events into ClickHouse one row at a time. A traffic spike doubles the inserts and the merges cannot keep up.
- The Supernode — Easy — A celebrity joins and half of all friend-of-friend traversals touch their node. Adding cluster members barely raises the ceiling.
- The Devices Come Back — Easy — A regional network outage ends and a million sensors flush their buffers at once — 4× the normal write rate, with firmware that retries three times.
- The Chatbot Went Viral — Easy — The support chatbot trends on social media: 6× the questions, most of them the same few. Each answer needs a 30 ms nearest-neighbour search.
- Month-End Close — Easy — Month-end: five times the BI queries, each taking seconds. The warehouse queues, then the BI API runs out of threads waiting on it.
- More Members, Same Leader — Easy — A fleet-wide rollout floods etcd with status writes. Every write goes through one leader and a quorum — adding members will not help.
- 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.
- 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.
- AWS us-east-1: The Endpoint That Vanished — Hard — The regional database endpoint stops resolving. Two services depend on it, the replica is 75 ms away, and nothing new can launch until the database is back.
- 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.
- 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.
- Coinbase: The QR Code Commercial — Hard — A bouncing QR code during the big game: a hundred times the visitors for one minute, sign-ups calling a slow identity vendor, and existing customers checking prices at the same time.
- Shopify: The Celebrity Flash Sale — Hard — A celebrity’s product drop brings eighty times their usual traffic onto the shared platform. No other merchant may notice, no fan’s order may be lost — and the drop starts in one minute.
- 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.
- 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.
- 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.
- SaaS: The Enterprise Onboarding — Hard — Your biggest customer ever starts importing ten years of data on a Tuesday afternoon — on the shared stack — while half your customers’ webhook endpoints slow down. Existing tenants must not notice.
Codelays runs in your browser: turn on JavaScript to use it.