Kafka & Queues
Decouple producers from consumers — and keep an eye on lag.
A queue acknowledges writes immediately and lets consumers process them at their own pace. Users stay happy during bursts, but the work is only done when consumers catch up: lag, not errors, is the signal.
Concepts
- Asynchronous writes
- The producer gets "accepted" right away; the work completes later. Reads can never go through a queue.
- Partitions
- Kafka consumer parallelism is capped by partitions: more consumers than partitions sit idle.
- Consumer lag
- How far consumers are behind. Growing lag means the backlog never drains.
- Poison messages
- One bad record retried forever stalls a partition — that is what dead-letter queues are for.
Lab
Problems
- YouTube: Viral Video — Medium — A video goes viral: 4× traffic and an exploding transcoding backlog.
- Uber: Rush Hour — Hard — A poison message stalls ride matching, then rush hour doubles traffic.
- Payment System — Hard — A third-party fraud API slows down and drags the whole payment flow with it.
- WhatsApp: Message Delivery — Medium — A flaky network link to Kafka, then a Cassandra compaction storm.
- 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.
- Notification Service: Breaking-News Push — Hard — A breaking story multiplies notifications by eight for the rest of the hour, then the SMS vendor browns out. Alerts must still land within a minute.
- 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.
- Stock Exchange: The Opening Bell — Hard — At the opening bell order flow doubles for a minute and four times the retail users stream quotes. Orders must be acknowledged within 45 ms at p99, and nothing can boot in time.
- 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.
- Web Crawler: When the Web Slows Down — Hard — The sites you crawl slow to 1.5 s per page and a transit link starts dropping packets. Your fetchers have idle CPUs and a growing frontier — and you cannot add machines.
- LeetCode: Weekly Contest Start — Hard — The contest starts: eight times the coders for two minutes, every one opening the same four problems and submitting — and each submission holds a serverless execution for its whole judge run.
- 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.
- 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.
- Poison Pill — Easy — One malformed order is retried forever and the order pipeline stops moving — but every API call still succeeds.
- Two Consumer Groups — Easy — The analytics consumer crashes. Orders keep flowing — but analytics falls minutes behind.
- Idle Consumers — Easy — Traffic doubles. Autoscaling adds consumers, CPU looks fine — and lag keeps growing.
- Producers Rejected — Easy — A flash sale doubles writes and a third of checkouts fail at publish time. The consumers are idle.
- Absorb the Burst — Easy — A one-minute burst of writes overloads the database. There is no Kafka yet — add it.
- Publishers Blocked — Easy — Order placement starts failing — at the API. Nothing is wrong with the API: RabbitMQ hit its memory alarm because the consumers slowed down.
- Two Million Records, One Afternoon — Easy — A partner dumps 60× its normal import volume for two and a half minutes. The Lambda importers hit their concurrency limit and the partner’s retries make it worse.
- ProvisionedThroughputExceeded — Easy — A firmware update makes 2M devices report 80% more often. The stream’s shards throttle — and every rejected device retries three times.
- Lose the Local Cluster — Easy — The local Pulsar cluster goes down mid-day. A geo-replicated cluster in the other region is healthy — but nothing publishes to it.
- Twelve Seconds to the Open — Easy — The market opens in 12 seconds and order flow will double. The risk engine cannot scale in time, and the gateway’s p99 budget is 45 ms.
- Firehose on GCP — Easy — A live show doubles an 80,000 events/s clickstream for a minute, 12 seconds from now. The enrichment service cannot scale in time, and you run on GCP.
- Drain Without Drowning — Easy — A seller bulk-upload floods the order queue for 90 seconds. The workers must catch up within 100 seconds — and their autoscaler is about to drown the database shoppers read from.
- Every Click Is a Row — Easy — A campaign doubles the clickstream. The writer inserts one ClickHouse row per click, ClickHouse falls behind — and the lag never stops growing.
- 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.
- 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.
- Healthcare.gov: Launch Day — Hard — Six times the expected visitors on day one: plan browsing that was never cached, a synchronous identity vendor, a data hub that waits on three agencies, and an autoscaling tier with a fixed database.
- 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.
- 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.
- 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.
- 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.