AlgoViz

Design a news feed like Facebook's

hard 50 min

Show each person the best recent posts from everyone they follow, in a fifth of a second.

Numbers that decide

Work out a number when the answer changes what you build.

Capacity

The two products that decide

Traffic

2B×1086,400≈231,000 reads/s2B×20%86,400≈4,630 posts/s\frac{2\text{B}\times 10}{86{,}400} \approx 231{,}000\ \text{reads/s} \qquad \frac{2\text{B}\times 20\%}{86{,}400} \approx 4{,}630\ \text{posts/s}
Peak is 3x: 694,000 reads/s and 13,900 posts/s. About 50 reads for each post.

Push against pull

4,630×500⏟push=2.3 M inserts/s231,000×500⏟pull=116 M reads/s\underbrace{4{,}630\times 500}_{\text{push}} = 2.3\,\text{M inserts/s} \qquad \underbrace{231{,}000\times 500}_{\text{pull}} = 116\,\text{M reads/s}
At peak 6.9M against 347M. Push is 50 times less work, and nobody waits for it.

Ready feeds

500×20 B=10 KB,10 KB×2.5 B users=25 TB,×2 copies=50 TB500 \times 20\,\text{B} = 10\,\text{KB},\quad 10\,\text{KB}\times 2.5\,\text{B users} = 25\,\text{TB},\quad \times 2\ \text{copies} = 50\,\text{TB}
Assume 2.5 billion people were active in the last 7 days (it cannot be fewer than the 2 billion of one day). An entry is a post ID and a time, both stored compactly. At 200 GB usable a node, 250 nodes: 125 primaries, 125 replicas.

All the numbers

QuantityWorkingResult
New visits3 × 2B / 86,40069,000/s, 208,000 at peak
Candidates scored heavily69,000 × 1006.9M/s, 21M at peak
Full posts shown231,000 × 204.6M/s, 14M at peak
One big post, one at a time10M × 1 ms10,000 s, nearly 3 hours
One big post at 1M inserts/s10M; 100M10 s; 100 s, if nothing else runs
Recent posts of big accounts1M accounts × 100 posts × 20 B2 GB: fits in every feed server
Ranked visit kept 10 minutes69,000 × 600 s × 1 KB42 GB, 125 GB at peak
Open connections25% of 2B, 100,000 a server500M, 5,000 servers
Polling instead500M / 30 s16.7M/s: 72 times the feed traffic
Fan-out workers6.9M / 10,000 inserts/s each694 at peak, so 1,024 partitions
Post text and fields400M × 1 KB400 GB a day, 730 TB in 5 years, 2.2 PB with 3 copies
Images70% of posts × 200 KB56 TB a day, 20 PB a year
Video20% of posts × 20 MB1.6 PB a day, 584 PB a year
Feed replies694,000 × 20 × 3 KB42 GB/s, 333 Gbps at peak
Images delivered694,000 × 70% × 10 × 200 KB972 GB/s, 7.8 Tbps at peak
Image traffic a montha third of peak, 30 days0.84 EB; 8.4Matanassumed8.4M at an assumed 0.01/GB
Inputs (200 KB images, 100,000 connections, 10,000 inserts a worker) are assumptions. Say so and offer to measure.

So what?

The decision each number makes

NumberDecisionWould change if
2.3M pushes against 116M pullsReady feeds, filled when people postPeople post as often as they read
10M followers: 10 s to 3 hoursPull for big accountsFollower counts are limited to a few thousand
2 GB for all big accountsKeep them in feed server memoryThere are 100 times more big accounts
14M full posts a second at peakA post cache; at 99% hits the store sees 140,000/sPages show three posts
25 TB of ready feedsOnly warm users, IDs onlyUnder 100 GB, one machine and a replica
16.7M polls a secondOpen streams with small hintsOnly a few thousand are online
7.8 Tbps of imagesCDN; at 90% hits the origin serves 0.8 TbpsText only

Latency budget (200 ms p99 at our servers)

StepBudget
Gateway: sign-in and limits10 ms
Ready feed and follow list15 ms
Merge big accounts, light pass5 ms
Headers for 100 candidates20 ms
Features and model60 ms
Hydrate 20 posts25 ms
Assemble and send15 ms
Reserve50 ms
Later pages skip the middle steps: the order is already stored.