Problems / Real world, hard

Twitter / X timeline: from launch build to real traffic

The home timeline as first built: timelines read from Manhattan with no Redis cache, and the timeline API, ranker, tweet API, fan-out workers and search at an eighth of their real size. Real traffic of timeline reads, new tweets and searches arrives, and most requests fail.

Unlock it once for 25 credits; everything inside it is free (runs, changes, Submit, results, hints). Sign in with Google to start; new accounts get free credits.

Your goals

Grow it into the real architecture: every goal must hold at 6,000 requests a second. You may add components, connect them and resize anything.

Test cases

Like a coding problem, a solution has to pass every case, not just the one it was built for. Submit in the lab runs all 6; 2 of them are hidden until then.

  1. As given. The problem’s own traffic, scheduled failures and seed.
  2. Peak hour. 25% more traffic than the problem states, with the same failures.
  3. A slow dependency. The main data store or model answers twice as slowly for part of the run.
  4. A server dies. One replica of the service with the most replicas is killed for part of the run.

The traffic

6,000 requests a second for 20 seconds: 80% home (reads), 10% tweet (writes), 10% search (reads).

The system

What each component does in this design, and what calls it. The simulator mimics each one from these settings.

ComponentIts job hereSettings
DDoS protectionDDoS protectionTraffic enters here.Calls API gateway.Volumetric floods are absorbed at the network edge, spread over many locations, before they reach you.100000 slots
API gatewayAPI gatewayDDoS protection forwards to it.Calls Timeline API, Tweet API, Search API.Gateways centralise auth, rate limits and routing so each service does not reimplement them.50000 slots · 1,500,000 calls/min
Timeline APIApp serviceAPI gateway forwards to it for /home.Calls Timelines (Manhattan), Timeline ranker.Little's law: throughput ≈ concurrency ÷ latency. 200 slots at 50 ms serve about 4,000 req/s.1 replicas × 100 slots · timeout 1.0 s
Timelines (Manhattan)Key-value storeTimeline API calls it for reads. Fan-out workers calls it.Key-value stores spread keys over partitions; one very popular key can overload a single partition.16 partitions × 2,000 ops/s
Timeline rankerRanking modelTimeline API calls it.Feeds rank a few hundred candidates per load with a learned model; retrieval narrows millions of posts first.128 slots
Tweet APIApp serviceAPI gateway forwards to it for /tweet.Calls Tweets (MySQL, sharded), Tweet events (Kafka) (publishes).Little's law: throughput ≈ concurrency ÷ latency. 200 slots at 50 ms serve about 4,000 req/s.1 replicas × 100 slots · timeout 1.0 s
Tweets (MySQL, sharded)SQL databaseTweet API calls it.Postgres runs one process per connection, which is why poolers like PgBouncer sit in front of busy databases.4 vCPU · 50 connections
Tweet events (Kafka)Event streamTweet API publishes to it.Calls Fan-out workers.Kafka keeps order only within a partition, so the partition key decides what stays in sequence.8 partitions · retention 600 s
Fan-out workersWorkerTweet events (Kafka) consumes from it.Calls Timelines (Manhattan), Search index (Earlybird).Queues deliver at least once, so a worker can see the same message twice.32 concurrent jobs
Search APIApp serviceAPI gateway forwards to it for /search.Calls Search index (Earlybird).Little's law: throughput ≈ concurrency ÷ latency. 200 slots at 50 ms serve about 4,000 req/s.1 replicas × 100 slots · timeout 1.0 s
Search index (Earlybird)Search indexFan-out workers calls it. Search API calls it.Search engines build an inverted index: each word points to the documents that contain it.64 slots

More real world problems