Sub topic 1
Clarifying Requirements and Estimating Load
Prerequisites
This chapter assumes you know the following:
- How clients talk to servers over HTTP APIs.
- The difference between relational databases and key-value stores.
- What a cache and a message queue do.
- How to read short Python snippets.
You do not need to know anything about feeds yet. This chapter is the first step of the design. Every later choice, such as push or pull, cache size, and sharding, depends on the numbers you produce here.
Why start with requirements
"Design a news feed" is vague. A feed for 10,000 users fits on one database server. A feed for 500 million users does not. Before you draw any boxes, ask questions that pin down the scope, then turn the answers into numbers.
Functional requirements
Ask what the system must do. For a feed, a reasonable core set is:
- A user can publish a post.
- A user can follow and unfollow other accounts.
- A user can open a feed. It shows posts from followed accounts, newest first.
- The feed can be scrolled through page by page.
Then ask what is out of scope. Media upload, notifications, search, and ads are separate systems. Say so early. Ranking by relevance is a later chapter, so assume reverse chronological order for now.
Non-functional requirements
These set the engineering targets.
- Read latency. A feed request should usually return in under 200 ms at the 99th percentile (p99, the time that 99% of requests beat).
- Availability. The feed should stay up even if some components fail. A stale feed is better than an error page.
- Freshness. A new post should appear in followers' feeds within a few seconds. It does not need to be instant.
- Consistency. Eventual consistency is acceptable. Two users may briefly see different views of the same timeline.
The freshness and consistency answers matter a lot. If you accept a few seconds of delay, you can precompute feeds in the background.
Estimating load
A back-of-the-envelope estimate uses round numbers and simple arithmetic. The goal is the right order of magnitude, not an exact figure.
Assume these inputs:
| Input | Value |
|---|---|
| Monthly active users | 300 million |
| Daily active users (DAU) | 100 million |
| Feed opens per DAU per day | 10 |
| Posts per DAU per day | 0.5 |
| Average followers per user | 200 |
| Seconds in a day | about 86,400, rounded to 100,000 |
Read and write rates
Reads are feed opens:
- 100 million users × 10 opens = 1 billion reads per day.
- 1 billion ÷ 100,000 seconds = about 10,000 reads per second on average.
- Traffic peaks. Use a peak factor of 3 to 5 times the average. That gives roughly 30,000 to 50,000 reads per second at peak.
Writes are new posts:
- 100 million × 0.5 = 50 million posts per day.
- 50 million ÷ 100,000 = about 500 posts per second on average, and roughly 2,000 at peak.
The read-to-write ratio is about 20 to 1. Feeds are read-heavy, which favors precomputing work at write time.
Fan-out load
Fan-out is the number of feed deliveries one post causes. This is the number that decides the architecture.
- 500 posts per second × 200 followers = 100,000 timeline inserts per second on average.
- At peak this is several hundred thousand inserts per second.
Averages hide the tail. One account with 50 million followers turns a single post into 50 million inserts. That one case is why the later chapters introduce a hybrid approach.
Storage
Keep the stored items small. A timeline entry only needs a reference, not the post body:
- Post ID: 8 bytes.
- Timestamp: 8 bytes.
- Total: about 16 bytes, call it 20 with overhead.
If each active user keeps 800 entries, one timeline costs 800 × 20 = 16 KB. For 100 million active users that is 1.6 TB. This fits in a sharded in-memory cache, and it shows why timelines must be bounded.
Post storage is separate. At 50 million posts per day and about 1 KB of text and metadata per post, you add about 50 GB per day, or about 18 TB per year.
A quick calculator
Scripting the estimate makes it easy to change assumptions and see their effect.
def estimate(dau, opens_per_user, posts_per_user, avg_followers,
feed_len=800, entry_bytes=20, peak_factor=4,
seconds_per_day=100_000):
reads_per_sec = dau * opens_per_user / seconds_per_day
posts_per_sec = dau * posts_per_user / seconds_per_day
return {
"reads_per_sec_avg": reads_per_sec,
"reads_per_sec_peak": reads_per_sec * peak_factor,
"posts_per_sec_avg": posts_per_sec,
"inserts_per_sec_avg": posts_per_sec * avg_followers,
"feed_cache_tb": dau * feed_len * entry_bytes / 1e12,
}
for name, value in estimate(100_000_000, 10, 0.5, 200).items():
print(f"{name}: {value:,.1f}")Running it prints about 10,000 average reads per second, 40,000 at peak, 500 posts per second, 100,000 inserts per second, and 1.6 TB of cache.
How the numbers shape the design
Common errors
- Using averages only. The average follower count is 200, but a few accounts have millions. Always ask about the maximum and the distribution.
- Forgetting peaks. Provision for peak traffic, not the daily average.
- Storing full posts in timelines. This multiplies storage by the fan-out factor. Store IDs and fetch the post bodies separately.
- Skipping the stated assumptions. Write your inputs down. A reviewer can then challenge a number without rejecting the whole design.
Exercise
Using the chapter's assumptions, 100 million DAU, 0.5 posts per user per day, and 200 followers each, what happens to timeline inserts per second if average posts per user per day doubles to 1 and followers stay at 200? Use 100,000 seconds per day.
Show answer
Posts per second rises from 500 to 1,000. Inserts per second equal posts per second × followers, so 1,000 × 200 = 200,000 inserts per second. Inserts double because fan-out load scales linearly with posting rate.
Exercise
Why does a design that only looks at the average of 200 followers per user risk failing in production?
Show answer
Follower counts are heavily skewed. A single account with tens of millions of followers creates tens of millions of timeline inserts from one post. That burst can overwhelm workers and delay everyone else's updates, so the design must handle high-follower accounts separately.