Sub topic 1
Clarifying Requirements and Estimating Load
Prerequisites
Assume you understand client-server architecture, HTTP APIs, relational databases, key-value stores, caching, and message queues. You should also be comfortable reading short Python and SQL examples.
Clarifying Requirements and Estimating Load
A news feed shows posts from accounts a user follows. Before choosing a design, clarify:
- How many users and posts exist?
- How often does a user open the feed?
- How many posts should one request return?
- How fresh must new posts be?
- Is chronological order enough, or is ranking required?
For example, assume 10 million daily active users. If each reads the feed 12 times per day, the average read rate is:
10,000,000 × 12 ÷ 86,400 ≈ 1,389 reads/secondDesign for peak traffic, not only the average. A 5× peak means about 7,000 reads per second. If 1% of users publish once per day, writes average about 1,200 posts per second during the day, with higher bursts.
Modeling Users, Posts and Follow Edges
Keep the core entities simple. A follow edge is a directed relationship from one user to another.
| Entity | Important fields | Useful access pattern |
|---|---|---|
| User | `user_id`, profile data | Find a user |
| Post | `post_id`, `author_id`, `created_at`, content | Recent posts by author |
| Follow edge | `follower_id`, `followee_id` | Followees for a user |
Index posts by (author_id, created_at, post_id). Store follow edges with indexes in both directions when the product needs both “who I follow” and “who follows me.”
Building the Feed at Read Time
Fan-out on read, also called the pull model, builds a feed when requested:
- Load the user’s followees.
- Read recent posts for those accounts.
- Merge the results by timestamp.
- Return a limited page.
This approach makes publishing cheap and keeps storage compact. Its read cost grows with the number of followees and the number of posts examined. A user following thousands of accounts may cause slow queries, so use indexed queries, bounded lookback windows, caching, or a later hybrid design.
SELECT post_id, author_id, created_at
FROM posts
WHERE author_id IN (:followed_author_ids)
ORDER BY created_at DESC, post_id DESC
LIMIT 20;Exercise
Why can fan-out on read make publishing inexpensive but reading expensive?
Show answer
Publishing stores one post, while reading must find and merge posts from every followed account.