Build a Short-Post Social Feed Platform at Scale
System DesignModel answerLast updated
Design a backend for a social platform where users publish brief posts, follow each other, and read a recent-posts home feed, covering feed delivery, storage, caching, and abuse controls.
You are asked to design the backend for a social platform in which people publish short text posts and read a personalized feed of what the accounts they follow have published recently.
Required capabilities
- A user can publish a short post.
- A user can follow and unfollow other accounts.
- A user can open a home feed showing recent posts from the accounts they follow.
- The system must handle both a heavy read load and a heavy write load.
What your answer should cover
- Your assumptions about scope and scale, with rough back-of-envelope numbers.
- The main APIs and the data model.
- How the home feed is produced. Compare computing it when a post is written with computing it when the feed is read, and say whether you would combine them.
- Storage technology choices, caching layers, pagination, and how posts are ordered or ranked.
- Consistency expectations, for example how soon a new post should show up for followers and for its author.
- Operational concerns: rate limiting, spam and abuse handling, and monitoring.
You may answer verbally, supported by structured notes or a rough diagram. State your trade-offs explicitly.
Hints
Hint 1
Do the arithmetic early: posts per second, feed reads per second, and especially the number of feed insertions per second if every post is copied to every follower. That last figure should drive your core design choice.
Hint 2
Think about accounts with an enormous number of followers and accounts that rarely log in. A single strategy for all users is likely to waste work or hurt latency for one of these groups.
Hint 3
Use time-sortable IDs and cursor-based pagination, store only post IDs in cached feeds, and fill in post contents at read time. This also makes deletes and unfollows easier to handle.
Hint 4
For operations, think about what signal tells you the feed is stale (for example, queue consumer lag), and which limits and checks belong synchronously on the write path versus asynchronously afterwards.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Scope and assumptions
In scope: publish a post (up to about 280 characters, optional media reference), follow/unfollow, home feed, and a per-author post list. Out of scope (mention as extensions): search, direct messages, trending topics, notifications, replies and reposts.
Non-functional targets
- Availability matters more than strict consistency for feeds, so eventual consistency is fine.
- A new post should reach followers' feeds within roughly 5 seconds at p99. The author should see their own post immediately (read-your-writes).
- Feed read latency: p99 under 200 ms.
Back-of-envelope (assume 300M daily active users)
- 2 posts per user per day gives 600M posts/day, about 7K writes/s on average and about 20K/s at peak.
- 20 feed opens per user per day gives 6B reads/day, about 70K reads/s on average and about 200K/s at peak. The system is read-heavy, roughly 10:1.
- With 200 followees on average, copying each post into each follower's feed means about 120B inserts/day, around 1.4M/s. This is the number that shapes the design.
- Storage: about 300 bytes per post gives about 180 GB/day, roughly 65 TB/year. Media goes to blob storage behind a CDN.
2. APIs
POST /v1/posts {text, media_ids?, idempotency_key} -> {post_id, created_at}
DELETE /v1/posts/{id}
PUT /v1/users/{id}/follow -> 204
DELETE /v1/users/{id}/follow -> 204
GET /v1/feed/home?cursor=&limit=20
GET /v1/users/{id}/posts?cursor=&limit=20
Use cursor pagination: the cursor holds the last seen post ID (or score plus ID if ranked). Offsets break when new items arrive at the head, causing duplicates or gaps. The idempotency key makes client retries safe.
3. Data model
Use 64-bit Snowflake-style IDs (timestamp, worker, sequence), which sort by time without an extra index.
| Store | Key / schema | Notes |
|---|---|---|
| Posts (wide-column store or sharded SQL) | post_id -> author, text, media, created_at, deleted | Shard by post_id |
| Author timeline | (author_id, post_id DESC) | One partition per author |
| Social graph | followers(user, follower) and following(user, followee) | Both directions, sharded by first key |
| Users | profile, counters | Relational plus cache |
| Home feed cache (Redis) | feed:{user_id} -> about 800 post IDs | IDs only, not bodies |
4. Feed generation
Fan-out on read: at open time, fetch recent posts for each of about 200 followees and merge them. Writes are cheap, but each read triggers hundreds of lookups at 200K QPS, so latency suffers.
Fan-out on write: on publish, push the ID into every follower's cached feed. Reads become a single list fetch. The downside is accounts with tens of millions of followers: one post causes huge write bursts, delays delivery, and wastes work on inactive followers.
Hybrid (recommended):
- Regular accounts (below roughly 10K followers): push. A fan-out worker consumes a
PostCreatedevent from Kafka, walks the follower list, and does push plus trim-to-800 on each follower's Redis list in pipelined batches. - Very large accounts: do not push. At read time, merge the user's precomputed feed with recent posts pulled from the few large accounts they follow. Those authors' timelines are hot in cache.
- Inactive users (for example 30+ days away): skip fan-out and let their cache expire. Rebuild by pull on return.
5. Architecture and request paths
Client -> CDN / API gateway (auth, rate limits) -> Post service, Graph service, Feed service. The post service writes to the DB and emits events to Kafka; fan-out workers, the spam pipeline, search indexers and notifiers consume them.
Write path: persist durably, return success, publish the event, fan out asynchronously.
Read path: fetch IDs from Redis, merge in large-account posts, batch-hydrate bodies from a post cache, filter deleted/blocked/muted content, rank, return with a cursor.
6. Deep dives
Ranking. Start reverse-chronological: simple and debuggable. Later add two stages: candidate generation (about 800 IDs plus pulled posts) and a light scorer using recency decay, engagement, and author affinity from a feature store. On ranker timeout, fall back to chronological. For stable pages, snapshot the ranked list per session keyed by the cursor.
Consistency.
- The post is durable before the client sees success.
- Feeds converge within seconds.
- Read-your-writes: the client inserts its own post locally, or the feed service merges the author's latest posts at read time.
- Unfollow: filter against the current followee set at read time and clean cached entries lazily.
- Delete: write a tombstone and filter during hydration rather than editing millions of feeds.
Redis sizing. 300M users x 800 IDs x 8 bytes is about 2 TB, sharded by user ID and replicated. It is a cache, so a lost shard can be rebuilt from the graph and author timelines.
Fan-out throughput. Partition Kafka by author, scale workers horizontally, batch writes, and process active users first. Consumer lag is the key freshness metric.
Hot keys. Viral posts get in-process caching and request coalescing to protect the cache tier.
7. Operations
- Rate limiting: token bucket per user and per IP at the gateway with Redis counters (for example, caps on posts per few hours and follows per day, plus read QPS limits). Return
429withRetry-After. Be stricter with new accounts. - Abuse and spam: cheap synchronous checks on write (URL blocklist, duplicate-content hash, posting velocity), then asynchronous ML classification from the event stream. Flagged content is down-ranked or removed and affects an account reputation score. Detect mass follow/unfollow churn and add CAPTCHA or verification challenges on suspicious behavior.
- Observability: track fan-out lag, feed p50/p99 latency, cache hit rate, error rates, and queue depth. Alert on freshness SLO breaches. Use distributed tracing across the read path.
Common mistakes
- Choosing pure push or pure pull without computing the fan-out numbers.
- Ignoring very large accounts and inactive users.
- Using offset pagination on a feed that changes constantly.
- Storing full post bodies in every cached feed, which makes deletes and edits expensive.
- Treating the cache as the source of truth.
- Promising strong consistency for feeds when eventual consistency is sufficient and far cheaper.