Design Social Media Feed
Design a social media feed like Twitter or Instagram: timeline generation, ranking, and real-time updates.
What is a social media feed?
A social media feed selects, filters, and orders posts for a reader. The system combines durable post data, the social graph, precomputed timeline candidates, fresh activity, and ranking signals while enforcing current visibility rules.
The hardest design tension is asymmetric scale. Most authors have modest audiences and benefit from fan-out on write. A celebrity can have tens of millions of followers, turning one post into a write storm. A production design must protect fast reads without allowing high-follower accounts to overwhelm distribution.
Flow
Trace the critical path
Post event -> hybrid fan-out -> candidate timeline -> policy filter -> ranking -> response.
Correctness
Protect the invariant
Blocks, privacy, deletion, and moderation rules are checked at read time even when cached candidates are stale.
Reliability
Design the degraded mode
Fall back to a recent safe timeline when ranking is unavailable and contain celebrity fan-out with pull-based delivery.
Complete the design challenge before reading the walkthrough. Submit the architecture for grading, then compare its data flow, overload controls, and failure behavior with the reference design.
Start by defining the product boundary. This walkthrough assumes a Twitter-like microblogging service with text posts, optional media, follow relationships, and a personalized home timeline.
Functional requirements
- Create, delete, and read posts.
- Follow or unfollow authors.
- Load a personalized timeline with cursor pagination.
- Like, repost, and reply.
- Deliver new-post indicators to active readers.
- Enforce blocks, private-account rules, deletion, and moderation decisions.
Non-functional goals
<200ms
Timeline latency
p95 at the API boundary
99.9%
Availability
Safe degraded feed allowed
<5s
Freshness
For active ordinary authors
Eventual
Consistency
Except visibility enforcement
Decisions to confirm with the interviewer
- Is ranking required, or is a chronological feed enough for the first release?
- Do private accounts, blocks, and moderation changes need immediate enforcement? Assume yes.
- Are search, trending topics, ads, and recommendations in scope? Treat them as extensions.
- How should celebrity authors be defined: follower count, observed fan-out cost, or both?
- Can the service return a recent unranked feed when the ranking service is unhealthy? Assume yes.
The key invariant is simple: a cached candidate is never proof that the reader may still see the post.