Design a news feed like Facebook's
hard 50 minShow each person the best recent posts from everyone they follow, in a fifth of a second.
API and data
The contract first, then the data that serves it.
Entities
Name the things first
User, Follow (who follows whom, and since when), Post (author, text, media, audience, status), Reaction, and Feed session (one ranked visit). The viewer and author come from the sign-in token, never from the request body. More on that in API authentication.
GET /v1/feed?limit=20GET /v1/feed?cursor=eyJzIjoi...&limit=20→ 200 { "posts": [ { "id": "8841", "author": { "id": "u456", "name": "Asha" }, "text": "Hello world!", "media": [ { "type": "image", "url": "..." } ], "reactions": { "like": 42, "love": 10 }, "comment_count": 5, "created_at": "2026-01-15T10:30:00Z" } ], "next_cursor": "eyJzIjoi...", "has_more": true }POST /v1/media/upload-url{ "content_type": "image/jpeg", "size": 2048000 }→ 200 { "upload_url": "...", "media_id": "m_abc", "expires_in": 900 }POST /v1/posts Idempotency-Key: <client uuid>{ "text": "Hello world!", "media_ids": ["m_abc"], "audience": "friends" }→ 201 { "id": "8841" } or 202 while media is still processingPOST /v1/posts/{id}/reactions { "kind": "like" }DELETE /v1/posts/{id} → 204GET /v1/posts/{id} → 200, 403 or 404GET /v1/feed/stream (server-sent events)event: new_postsdata: { "count": 5 }Status codes
| Code | Meaning here |
|---|---|
| 200 OK | Read worked |
| 201 Created | Post stored |
| 202 Accepted | Media still processing; published when ready |
| 400 Bad Request | Bad field, or a cursor that has expired: start again |
| 401 Unauthorized | Not signed in |
| 403 Forbidden | Signed in, not allowed |
| 404 Not Found | Deleted, or hidden from this viewer |
| 429 Too Many Requests | Over a limit, with Retry-After |
| 500 and 503 | Our fault, with an incident ID |
API decisions
Interviewer
Why a cursor, and what is in it?
You
New posts push everything down, so offsets repeat posts. The cursor is opaque and signed. It holds a feed session and a position.
- The first request ranks once and stores 100 post IDs for ten minutes.
- Later pages read the next 20 IDs of that stored order.
- When it expires, the reply says so and the client starts a new visit.
- Filters still run on every page, so a deleted post never returns.
Interviewer
Posts with photos?
You
The client uploads straight to object storage with a short-lived signed address, then creates the post with media IDs. We check the media belongs to the author and is ready. See file upload APIs.
Interviewer
How do you version this, and limit it?
You
Path versions, additive changes, at least six months of warning before removal, and
410 Goneafterwards. Clients can name features they understand. Limits are sliding windows by user:- Feed reads 100 a minute; background refresh 1 a minute.
- Posts 10 an hour, comments 30 a minute, reactions 60 a minute, uploads by size.
- 429 with
Retry-After; the app keeps showing its saved page. - Very regular request patterns are flagged for review.
Interviewer
REST or GraphQL?
You
Either. A feed page is many shapes of post, so a query language saves fields and round trips on mobile. I would keep the same services behind it. See REST, GraphQL and gRPC.
Database
Data model
What is stored
Why each store
| Data | Store | Why | Alternatives |
|---|---|---|---|
| Posts | Cassandra, or any partitioned store | Lookups by post ID, appends by author, 2.2 PB | Sharded MySQL |
| Follow edges | Partitioned tables plus a cache | Only one step is needed: my follows, my followers | Graph database for friends of friends |
| Ready feeds | Redis sorted sets | Insert by time, read the top, no duplicates | Packed lists in Memcached |
| Post cache | Redis or Memcached | 14M lookups a second at peak | More replicas of the store |
| Accounts, sign-in | PostgreSQL | Transactions, not on the hot path | MySQL |
| Events | Kafka | Keeps events, replays after a fault | Pulsar, Kinesis |
| Media | S3 and a CDN | Large, written once, read often | Any object store |
| Measurements | A column store | Append, then aggregate | Cassandra with batch jobs |
CREATE TABLE posts ( post_id bigint PRIMARY KEY, author_id bigint, kind text, -- text, photo, video, link body text, media list<text>, audience text, -- public, friends, custom, only_me list_id bigint, -- for custom audiences place text, feeling text, tagged set<bigint>, status text, -- active, restricted, review, removed, deleted edited_at timestamp);CREATE TABLE author_posts ( author_id bigint, month text, -- keeps one partition small post_id bigint, PRIMARY KEY ((author_id, month), post_id)) WITH CLUSTERING ORDER BY (post_id DESC);CREATE TABLE followers ( target_id bigint, bucket int, -- 0..n, so 100M followers are not one row group user_id bigint, PRIMARY KEY ((target_id, bucket), user_id));ZADD feed:u123 1768473000000 8841ZREMRANGEBYRANK feed:u123 0 -501 keep the newest 500EXPIRE feed:u123 604800 cold after 7 daysZREVRANGE feed:u123 0 499 candidates, newest firstConsistency
Interviewer
How do you make sure a stored post is always delivered?
You
Writing to the store and then to Kafka is two steps, and a crash between them loses the delivery. I publish from the store's change stream, or an outbox table, so the event exists if the post does. See change data capture. Workers may repeat a task, and adding the same ID twice changes nothing.
Interviewer
Do you need a graph database?
You
Not for the feed: it asks one-step questions. "People you may know" asks for friends of friends, 500 × 500 = 250,000 edges for one person, and that can be a separate offline job or a graph store. See the connection graph problem.