System Design · Practice problem · Hard
Post to a Feed With a Celebrity
Fan out on read builds each feed when it is read. Fan out on write copies each post to every follower when it is written, which makes reads cheap but makes a post's cost grow with its followers.
Hard means correctness and scale at the same time. You solve it on a canvas that runs your design. The canvas needs JavaScript; everything on this page works without it.
What is the situation?
You run a recipe app. Cooks post recipes, and each person's home page shows the newest posts from the accounts they follow. A reader follows 300 accounts: 290 ordinary cooks, 9 food writers and 1 TV chef. An ordinary cook has 200 followers, a food writer has 300,000, and the TV chef has 5 million. Your app knows which is which, so a food writer's post and the chef's post each arrive as their own call, and you can send them down a different path. At the evening rush there are 2,000 page loads and 40 posts a second, and at Sunday lunch people post 180 times a second. Food writers post about once every 5 seconds between them. Design the path from POST /posts to GET /feed. The Follow graph is already on your canvas, and it is the only place that knows who follows whom. Three facts about your hardware. A Fan-out Worker writes 10,000 timeline entries a second. A page that reads many accounts makes its lookups side by side, so it waits for the slowest one and holds one of the store's slots until then. And a page has about 2 seconds before it gives up. By the time a test is judged, your Caches have had time to fill.
What must your design do?
- 95% of pages load within 150 ms, and 99.9% are served. A page that leaves out a saved post counts as failed.
- 99.9% of posts are accepted, and 99% are answered within 200 ms. A post is saved somewhere durable.
- A post reaches its followers' pages within 5 seconds, 99% of the time, whoever wrote it. One account's post must not hold up anyone else's.
- Your design asks the Follow graph who follows whom.
- If every Fan-out Worker crashes in the middle of a copy, no post is skipped, no copy is written twice, and the oldest unfinished post is done within 30 seconds.
- If a Cache restarts empty, pages are back to normal within 60 seconds, and posting carries on as usual.
- The system costs $850 a month or less.
What does Run test?
Run works without an account. It checks your design against 2 tests you can read before you start:
- A quiet evening. A quiet evening: 150 page loads and 5 posts a second.
- A popular cook posts. The same quiet evening, and at second 8 a popular cook with 20,000 followers posted.
What does Submit add?
Submit needs an account. It runs 6 more tests you cannot read in advance, and stops at the first one your design fails, showing what happened and why. Passing every test, or giving up, unlocks the written solution.
Which parts can you use?
Already on your canvas: Follow graph.
- Load Balancer: about 50,003 a second, $18 a month each
- App Server: about 2,000 a second, $45 a month each
- Database: about 1,000 a second, $120 a month each
- Read Replica: about 1,000 a second, $90 a month each
- Cache: about 100,003 a second, $30 a month each
- Queue: effectively unlimited, $25 a month each
- Outbox Relay: about 5,000 a second, $40 a month each
- Fan-out Worker: about 841 a second, $45 a month each
Every connection takes 1 ms, and the budget is $850 a month.
What does this problem leave out?
- What a page's lookups really cost. A page that reads 300 accounts here pays for its slowest lookup and one slot, while real lookups are each a piece of work. So a design that keeps every account's list in memory looks cheaper here than it is.
- One account's list as a hot key. A page reaches a Cache server by its reader, never by the accounts it reads, so we cannot see every page landing on the chef's key.
- What a rebuilt timeline holds. We measure that pages come back with the accounts' posts and that posting carries on, and we do not check that a rebuilt timeline has every post it had.
- How much memory timelines take. A Cache here has no size limit.
- Which followers are active. Every follower counts, so real copying would cost less, and reading one huge follower list costs one read here.
- A Queue going down, or a server crashing between two writes. So sending the event from the server and using an Outbox Relay read the same here.
- Moving an account across the line, ranking, page two, unfollows, deletes and privacy.
Where does this problem fit?
Its category is Feeds & fan-out: One write lands in millions of feeds. It practises caching, replication, queues & async, fan-out and reliability & ops.
For the whole system, read the interview breakdown Design a News Feed (Twitter, Facebook).
By GOAT Engineer. Explore System Design · Privacy Policy · Contact.