Design a Social Media Activity Feed at Scale
System DesignModel answerLast updated
Design the core feed delivery system for a social platform, focusing on how to balance write amplification against read latency for users with vastly different follower counts.
Design a Social Media Activity Feed at Scale
You are building the activity feed for a social platform. When someone posts content, their followers should see it in their personalized feed. The core challenge is doing this efficiently when some users have 200 followers and others have 10 million.
What the system needs to do
- Allow users to publish posts (text and attached media).
- Allow users to follow and unfollow other accounts.
- Serve each user a personalized, reverse-chronological feed of posts from accounts they follow.
Scale and quality targets
- Active users: 100 million per day.
- Feed load time: under 200ms at the 99th percentile.
- Consistency: feeds do not need to update in real time — a delay of a few seconds is fine.
What your answer should cover
- API design — endpoints for publishing a post, following/unfollowing, and retrieving a feed with pagination.
- Data model — what you store, where you store it (SQL vs. NoSQL and why), and how you structure it.
- Feed generation architecture — how a new post makes its way into followers' feeds; specifically, how your approach differs for a regular user versus an account with millions of followers.
- Caching strategy — what lives in cache, how it is keyed, and how you handle consistency between cache and storage.
- Pagination — how a client requests successive pages of the feed without missing posts or seeing duplicates.
Ground your design in rough numbers. Interviewers want to see estimates drive decisions, not decorate them.
Hints
Hint 1
Start with a back-of-envelope calculation. Posts per day, feed reads per day, and average follower count each have a direct implication for the design — write them down before drawing any boxes.
Hint 2
Consider what happens when a single write must propagate to 10 million followers. That edge case is the crux of the problem, and the answer is not to treat every user the same way.
Hint 3
Your feed does not need to store full post content — storing only post IDs and resolving them at read time keeps the cache smaller and makes edits and deletes much cheaper.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Size the System First
Estimates should drive every decision that follows.
| Signal | Rough value |
|---|---|
| DAU | 100M |
| Posts per user per day | ~0.5 → 50M posts/day, ~600 writes/s, peak ~3k/s |
| Feed loads per user per day | ~10 → 1B reads/day, ~12k QPS, peak ~50k QPS |
| Avg. followees per user | ~200 |
| Max followers (celebrity) | 10M+ |
Key conclusions:
- The system is read-heavy (~20:1). Feeds must be served from memory — disk latency will not meet 200ms.
- Naïve push is expensive but finite for regular users. 50M posts × 200 followers = 10B feed inserts/day (~115k/s). Feasible with enough workers.
- Celebrities break naïve push entirely. One post → 10M cache writes synchronously is not acceptable.
- Feed cache is large but survivable. 500 post IDs × 16 bytes × 100M users ≈ 800GB. A Redis cluster can hold this, and it can always be rebuilt from storage if lost.
2. API Design
POST /v1/posts
body: { text, media_ids[] }
header: Idempotency-Key
→ 201 { post_id, created_at }
POST /v1/users/{user_id}/follow → 204
DELETE /v1/users/{user_id}/follow → 204
GET /v1/feed?limit=20&cursor={opaque_cursor}
→ { items: [{ post_id, author_id, text, media_urls, created_at }],
next_cursor }
A few design choices worth calling out explicitly:
- The authenticated user is read from the auth token, never from the URL. This prevents one user from writing to another's feed.
- The
Idempotency-Keyheader on post creation lets clients safely retry without creating duplicate posts. - Pagination uses an opaque cursor rather than offset. Offset breaks when new posts arrive: items shift and users see duplicates or gaps. A cursor encodes the last-seen position and is stable regardless of new writes.
3. Data Model
Users — relational DB (Postgres/MySQL), sharded by user_id. The data is small, relational, and benefits from transactional consistency. Add an is_celebrity boolean flag and a last_active_at timestamp here — both matter for feed generation logic.
Posts — wide-column store (Cassandra or DynamoDB). Partition by author_id, cluster by post_id descending. This makes "recent posts by author X" a cheap single-partition range scan, which the pull path needs. Use Snowflake-style IDs (timestamp + machine ID + sequence): globally unique, roughly time-sortable, and usable directly as pagination cursors.
Follow graph — two denormalized wide-column tables:
followers(user_id → follower_id)— read during fan-out to find who to notify.following(user_id → followee_id)— read at feed time to know who a user follows.
Denormalization avoids expensive joins at high QPS. This is purely key-based access with no need for relational guarantees.
Feed cache — one Redis sorted set per user, keyed feed:{user_id}. Members are post_ids, scored by timestamp. Cap each set at ~500 entries with ZREMRANGEBYRANK.
4. Feed Generation: The Hybrid Model
Three approaches exist; the right answer combines two of them.
Push (fan-out on write): when a post is created, immediately write its ID into every follower's feed cache. Reads are O(1). Works well for regular users. Catastrophic for celebrities (10M writes per post) and wasteful for inactive followers.
Pull (fan-out on read): at request time, fetch recent posts from every followee and merge them. Writes are cheap. Reads become O(followees), which at 50k QPS makes 200ms impossible for any user with a large following list.
Hybrid — what interviewers want to see:
- Regular users (≤ ~10k followers): use push. A
PostCreatedevent goes onto a Kafka topic. Async fan-out workers read the author's followers in batches andZADDthe post ID into each active follower's feed cache, skipping users inactive for ~30 days. Trim each feed to 500 entries. - Celebrity accounts (
is_celebrity = true): skip fan-out entirely. Their recent post IDs live in a dedicated per-author cache,author_posts:{author_id}, holding the last ~100 IDs. - Inactive users: their feeds are not maintained. On login, rebuild the feed via pull from the posts DB, then switch to push going forward.
Read path for GET /feed:
- Fetch the precomputed feed from
ZREVRANGEBYSCORE feed:{uid}starting at the cursor. - Look up which celebrity accounts this user follows (small list, cached separately).
- Fetch each celebrity's recent post IDs from
author_posts:*. - K-way merge all results by post ID (time-ordered). Take
limititems. - Hydrate: multi-get post bodies from post cache, author profiles from user cache.
- Lazily filter deleted posts, blocked users, and any unfollowed accounts. Filtering at read time is far cheaper than scrubbing millions of cached feeds.
- Return items and
next_cursor.
Every step is an in-memory operation or a small bounded fan-out. P99 latency stays under 200ms.
Edge cases to address:
- New follow: asynchronously backfill the followee's recent posts into the follower's feed.
- Unfollow: handle via read-time filter; no need to scrub the cache.
- User crosses the celebrity threshold: flip the flag. Use hysteresis (e.g., 10k to enter, 8k to exit) to avoid oscillation.
- Author sees their own post immediately: inject it directly into their own feed on write, bypassing async fan-out. This satisfies read-your-writes for the one case users notice most.
5. Caching Layers
| Cache | Key | Contents |
|---|---|---|
| Feed cache | feed:{uid} | Sorted set of post IDs |
| Post body cache | post:{post_id} | Full post payload |
| Celebrity timelines | author_posts:{id} | Recent post IDs, hot key |
| Social graph | following:{uid} | Followee list + celebrity subset |
| User profiles | user:{uid} | Name, avatar, long TTL |
Store IDs and bodies separately. A delete or edit touches one post:{post_id} entry rather than every feed that contains it.
Hot celebrity keys get replicated across Redis nodes and cached in-process on feed servers with a short TTL (a few seconds) to avoid hot-key saturation.
6. Pagination
Offset is wrong here — new posts shift positions, causing duplicates or skips on the next page.
Use a cursor that encodes the post_id/score of the last item returned. The next query becomes "items with score strictly less than cursor." For the hybrid merge, the same cursor value bounds both the precomputed feed and the celebrity timelines simultaneously. Requests that scroll past the ~500 cached entries fall back to a pull query against the posts DB; very few users scroll that deep.
7. Trade-offs to Articulate
- Write amplification vs. read latency: push optimizes reads at the cost of write volume; pull inverts that. The hybrid bounds both by routing based on follower count.
- Memory vs. deep scroll: capping feeds at 500 entries keeps the 800GB Redis footprint manageable, at the cost of a slower DB fallback for deep scrolling.
- Eventual consistency vs. correctness: fan-out lag of a few seconds is fine for followers but not for the author themselves — handle the self-feed case synchronously.
Common Mistakes
- Proposing pure push without addressing celebrities — this is the most frequent gap.
- Using offset-based pagination — breaks on a live feed.
- Storing full post bodies in the feed cache — makes deletions and edits expensive.
- Not separating ID storage from body hydration — the same mistake at a lower level.
- Forgetting inactive users — blindly fanning out to 100M users when only 10M are active wastes most of the write budget.