Design Instagram - authors upload photos, people follow authors, and a reader scrolls a home feed of recent photos. The photo bytes never pass the API servers - the phone sends them straight to object storage with a pre-signed URL, background workers make three small copies, and a CDN serves them. The senior-level signal is a media pipeline that survives a crash halfway, a feed that splits the work by author size, and a cursor that stays correct while the feed changes.
Written by a Staff+ Engineer and an Engineering Manager
12+ years of experience in distributed systems and real-time messaging. Tech lead for large-scale chat applications serving billions of users. Over 12 years of industry experience with 6 years in engineering leadership, building and scaling tech infrastructure teams that deliver end-to-end large-scale distributed systems.
Finished this lesson?
Sign in to save your progress across devices.
Tip: use ← and → to move between lessons.
Was this lesson helpful?
Rehearse this exact question with the author
45-minute mock with a Staff+ engineer who interviews at your target company. Premium members save $20.
What to Design?
Could you answer these today? Tick what you already know.
The happy path is quick to draw: the phone sends a photo, a server saves it, the feed is a query over the people you follow. The interview is decided by four questions about what happens when the files are large, a worker dies, an author is huge, or the feed moves under the reader. Each one is a deep dive later on this page, with three designs side by side: the one that sounds right, a better one, and the one that holds.
Swipe to see all columns
| The four questions, and the answer that sounds right | |||
|---|---|---|---|
| # | The interviewer asks | The answer that sounds right | What it misses |
| DD1 | 5,000 photos of 4 MB arrive every second. What do your API servers do with them? | They receive each photo and save it to storage | That is 20 GB a second and 40,000 open uploads on servers that should answer in 300 ms. The phone can send the photo straight to storage with a signed URL |
| DD2 | The worker that resizes a photo dies halfway. What does the feed show, and what is left behind? | The job runs again from the queue | |
The same three habits answer all four:
Do not spend your questions on filters and stickers. Spend them on what the system promises.
Swipe to see all columns
| Scope Decisions | ||
|---|---|---|
| Question | Agreed answer | Why it matters |
| Photos only, or video too? | Photos only | Photos need simple resizing. Video needs transcoding and many variants: a different, heavier pipeline |
| When does the author get the answer? | When the post record is stored, before the photo is processed | The upload splits into a fast record and slow background work (Phase 1, DD1) |
| How fresh must the home feed be? | A few seconds behind is fine, also for follow changes | |
Out of scope (say this out loud): video, Stories, Reels and live streams; ranking and recommendations; comments, likes and direct messages; search by hashtag or place; ads; private accounts; several regions that all accept writes.
FR1 — Upload a photo. An author shares a photo with a caption. The post is accepted once its record is stored.
FR2 — Make it renderable. Every photo is turned into three smaller copies: a thumbnail, a standard and a full size. A resized copy is called a rendition. Copies are served from a CDN, a network of servers near the readers that keep files ready.
FR3 — Follow and unfollow. A follow decides whose posts appear in a reader's home feed.
FR4 — Read the home feed. A reader gets recent posts of the authors they follow, newest first, also when one of those authors has 10 million followers.
FR5 — Scroll. Older posts come page by page, with a cursor.
The key principle behind all five
Swipe to see all columns
| Five facts and the conclusions they do not supportRead each row as: the fact on the left does not, by itself, imply the claim in the middle. | ||
|---|---|---|
| Fact | Does not imply | Why the gap exists |
| The author got 201 | The photo is in storage | 201 means the record is stored. The 4 MB are still on the phone |
NFR1 — Scale. 50 million daily users. At peak 5,000 uploads and 300,000 feed reads a second. More than 15 billion follows.
NFR2 — Speed. 95% of upload answers and of first feed pages arrive within 300 ms.
NFR3 — Nothing lost. An accepted post, a follow and an original photo survive any single failure. Copies and feed rows can be rebuilt.
NFR4 — Availability. 99.9% for upload and for the home feed. The feed may be a few seconds behind.
NFR5 — No work per follower on the hot path. A post by a very large author must not cause one write per follower.
| Capacity Thought Experiment | |
|---|---|
| Parameter | Value |
| Users | 50 million active a day |
| Uploads at peak | 5,000 photos a second |
| One photo | 2 to 5 MB. We use 4 MB |
| Photo bytes at peak | 5,000 x 4 MB = 20 GB a second |
| One post record | About 2 KB: 10 MB a second for all records |
| Uploads in flight | |
The number that identifies the real system is the gap between the record and the photo:
post records 5,000 x 2 KB = 10 MB a second through our API servers
photo bytes 5,000 x 4 MB = 20 GB a second must NOT go through them
feed images 300,000 x 1.6 MB = 480 GB a second served by the CDN
The records are a small web service. The bytes are two thousand times more, and that is why they take another road. The upload time, the copy sizes and the worker speeds on this page are illustrative assumptions.
Interviewer signal: candidates who draw "phone, API server, database" and store the photo "in S3" from the server have not looked at the 20 GB. Candidates who say "the record goes through us, the bytes go around us, and the copies are made after the answer" have found the system.
Three ideas shape everything:
The record is small and must be exact, so it goes to our database through our API. The photo is large and needs no logic, so the phone sends it straight to object storage, a service that stores files under a key (for example S3). Our server only signs a permission slip for that one upload: a pre-signed URL.
The post database holds posts and follows. Object storage holds the original photo. These two are the truth. Everything else is derived: the three renditions, each reader's feed rows, the files in the CDN. If a derived thing is lost or wrong, it is made again from the truth.
Resizing takes about a second, and telling followers can mean many writes. Neither happens before the author's answer. Both run from queues, and both are built so that a step that runs twice gives the same result.
The core recognition
This is two small pipelines behind one fast write. The write stores a record. One pipeline makes the photo renderable. The other puts the post into feeds. The read path only collects what the pipelines prepared.
| Design Decisions | |
|---|---|
| Design choice | Reason |
| Record first, then a pre-signed URL | The answer takes 50 ms and no photo byte touches an API server |
| Followers hear of a post when the photo has arrived | An upload that is abandoned never shows up in anybody's feed |
Two things carry the design: the
postsrow with itsstatus, and the feed row that points to it.
One row per post in the post database. Written by the upload, moved forward by the pipeline.
Swipe to see all columns
| posts | ||
|---|---|---|
| Field | Type | Comment |
post_id | STRING | Key, for example post_501. Never changes |
author_id | STRING | Who shared it. Indexed with created_at for the profile grid |
One row per reader and post in the feed store. It holds IDs only and can be rebuilt.
Swipe to see all columns
| home_feed_by_user | ||
|---|---|---|
| Field | Type | Comment |
reader_id | STRING | The partition: all rows of one reader lie together |
created_at | TIMESTAMP | Sort key, newest first |
post_id | ||
Swipe to see all columns
| Supporting records | ||
|---|---|---|
| Record | What it holds | Why it exists |
| Follow | follower_id, followee_id, unique together | Who follows whom. The truth for every feed |
| Rendition | post_id, type thumb, standard or full, object_key | |
An outbox is a table of events that is written in the same transaction as the change it reports. A small relay loop reads it and puts each event on the queues, again and again until it succeeds. A stored change can therefore never lose its event.
Swipe to see all columns
| Storage | ||
|---|---|---|
| Store | Data | Consistency |
| Post DB (PostgreSQL) | Posts, follows, renditions, outbox | The source of truth. One transaction per change |
| Object storage (for example S3) | The original photo and its three copies | The original is the truth and is never rewritten. Copies have fixed names and are never overwritten |
| Feed store (a wide-column store, for example Cassandra) | Feed rows per reader, follower lists, the hot list | Derived. A few seconds behind. Can be rebuilt from posts and follows |
Each store fits one way of reading. Photo bytes do not belong in SQL, and a feed is not a file.
The user always comes from the login token. A client never sends its own user ID.
| The calls | |
|---|---|
| Call | What it does |
POST /v1/posts | Creates the post record: caption, media_content_type, header Idempotency-Key. Answers 201 with post_id, status and upload_url. No photo bytes in this call |
PUT upload_url | The phone sends the photo to object storage. The URL is valid for 15 minutes, for one key, one content type and at most 20 MB |
Three promises of this API are worth saying out loud. The write answer means "the record is stored", not "the photo is processed" and not "your followers have it". The feed may be a few seconds behind, and an item whose status is not READY comes without URLs. The cursor is a token the client cannot read; it only hands it back.
Present the design as four short stories, each in its own colour on one whiteboard: Phase 1 upload (black), Phase 2 make it renderable (blue), Phase 3 tell the followers (green), Phase 4 read the feed (purple). Phases 1 to 3 follow one post. The number on an arrow is the same number in the animation and in the step table.
Swipe to see all columns
| The four phases | |||
|---|---|---|---|
| Phase | Colour | Story | What it leaves behind |
| Phase 1: Upload a photo (FR1) | Black | leo's phone stores a record, gets an upload URL and sends the photo straight to object storage | One post record, one original photo, one outbox row |
| Phase 2: Make it renderable (FR2) | Blue | A media worker reads the original and writes three smaller copies | Three files under fixed names. The post is |
How to read the picture
sunset.jpg, 4 MB, at 12:00:00. It becomes post_501. alice follows leo and also maya, who has 10,000,000 followers.Each phase below is an interactive walkthrough of the same picture. It plays on its own. You can pause it, step with the arrow keys, and click any component to read its role.
Goal: answer the author in 50 ms, and get 4 MB into storage without touching our servers.
/v1/postsPhone app (leo) → Post serviceNothing is written in this step.
Swipe to see all columns
| Phase 1 step by step (black arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | leo taps Share. The phone calls the Post service with the caption "Sunset at the pier", the content type image/jpeg and the idempotency key k_91. About 2 KB, no photo bytes. | Nothing yet |
| 2 | The Post service inserts the post record. The photo will lie at originals/post_501. | posts row post_501: AWAITING_UPLOAD |
Why it is built this way
AWAITING_UPLOAD after one hour.It can, as a shortcut, but it cannot be the only signal. A phone can lose its connection right after the last byte, and then a finished upload would never be announced. The event from object storage comes from the side that holds the file, so it is true whenever the file exists. Handling it twice is harmless: the change from AWAITING_UPLOAD to PROCESSING happens once, and the second attempt finds nothing to change.
Goal: turn one 4 MB original into three small files that a phone can show, without ever risking the original.
outbox relay: sendPost DB → Media queue{ post_501 }new rowSwipe to see all columns
| Phase 2 step by step (blue arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | The outbox relay puts a media job for post_501 on the Media queue. | One queue message |
| 2 | A media worker takes the job. | The message is hidden for 60 s |
| 3 | The worker reads the original from object storage: 4 MB. | Nothing |
Why it is built this way
Resizing on request is a real design, and it saves work for photos nobody looks at. But the first reader of every size waits a second instead of milliseconds, and when a large author posts, thousands of first readers ask in the same moment. Here almost every photo is looked at within seconds, in a known set of three sizes. Making them once, right after the upload, is simpler and keeps the read path free of any computation.
Goal: make the post appear in feeds without doing work for every follower of a very large author.
outbox relay: sendPost DB → Fanout queue{ post_501, leo, hot false }new rowSwipe to see all columns
| Phase 3 step by step (green arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | The outbox relay puts a fanout job on the Fanout queue: post_501, author leo, not hot. This happens at 12:00:08, at the same moment as step 1 of Phase 2. | One queue message |
| 2 | A fanout worker takes the job. leo has 300 followers. The limit for a hot author is 100,000. | The message is hidden for 60 s |
| 3 | The worker reads leo's follower list from the feed store: 300 followers. alice is one of them. | |
Fanout means turning one post into one feed row for each follower.
Why it is built this way
READY one second later, no feed row has to change.From measurement, not from theory. Below the limit, a post is written into every follower's rows: more write work, cheaper reads. Above it, the post is added at read time: one write, a little more work on every read of a follower. Typical limits lie between 10,000 and 500,000 followers. Start with one value, watch how long the fanout of the largest normal authors takes, and move the limit when that delay grows. What matters is that the rule is simple and can be changed, not the exact number.
Goal: build a page in well under 300 ms from prepared rows, and let the CDN carry every photo byte.
/v1/feed/home?limit=20Phone app (alice) → Feed serviceNothing is written in this step.
Swipe to see all columns
| Phase 4 step by step (purple arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | At 12:00:30 alice opens the app. It asks the Feed service for the home feed, limit 20. | Nothing |
| 2 | The Feed service scans alice's own feed rows: the newest 20. post_501 is among them. | Nothing |
| 3 | alice follows 3 hot authors. The Feed service reads their newest posts from the hot list, post_777 and others, merges everything by time and post ID, and keeps the newest 20. | Nothing |
Why it is built this way
READY, a placeholder otherwise. It learns that a photo is ready on its next refresh. No extra push channel is needed.The hot list keeps only maya's newest 20 posts, which covers the first pages of almost every reader. For a reader who scrolls further back, the Feed service reads maya's older posts from the Post DB with the index on author and time. That is the same read the profile grid uses. It is slower than the hot list, but only a few readers scroll that far, and the cursor works the same way.
The deep dives are the four questions from the top of the page. Each one compares three designs for the same scenario: a Bad one, which is usually the answer that sounds right; a Good one, which still leaves a gap; and a Great one, which holds. The answers start covered. Read the scenario, decide what you would build, then click each card.
Swipe to see all columns
| Deep Dive Map | |||
|---|---|---|---|
| Deep Dive | Topic | Scenario | NFR |
Scenario: at peak 5,000 photos are shared every second, 4 MB each. A phone needs about 8 seconds to send one. So 40,000 uploads are in flight at any moment, and 20 GB arrive every second. leo taps Share. What do our servers do with his photo, and when does he get his answer?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. One request carries the caption and the photo. The Post service reads the whole file, writes it to object storage, stores the record and answers.
Where it breaks. The server holds each photo in memory while it arrives: 40,000 uploads x 4 MB = 160 GB across the app servers. All 20 GB a second pass through them. And leo's answer comes only after the last byte, more than 8 seconds after his tap. The target is 300 ms.
The idea. The Post service does not keep the file. It passes the bytes on to object storage in chunks of 64 KB while they arrive.
What it fixes. Memory. 40,000 uploads x 64 KB is 2.6 GB instead of 160 GB.
What it still misses. Every byte still crosses the app servers, so the fleet is sized for 20 GB a second and 40,000 connections that stay open for 8 seconds each. The answer still waits for the last byte. And each restart of an app server, for example during a deploy, breaks every upload that passes through it.
The key idea. Our servers handle what needs logic: the record. Object storage is built to receive files, so the phone sends the file there itself.
How it works
AWAITING_UPLOAD.PROCESSING, and the outbox row is written in the same transaction.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
/v1/posts (multipart)Phone app (leo) → Post serviceNothing is written in this step.
Swipe to see all columns
| DD1 side by side | |||
|---|---|---|---|
| Bad: server receives | Good: server streams | Great: pre-signed URL | |
| Bytes through our app servers | 20 GB a second | 20 GB a second | 10 MB a second of records |
| Memory held for uploads | 160 GB | 2.6 GB | None |
Check it against the scenario
| leo taps Share | |
|---|---|
| Situation | Outcome with the Great option |
| Normal upload | 201 after 50 ms. The photo is in storage 8 seconds later, and the post becomes PROCESSING |
| The phone never saw the 201 and asks again | The same idempotency key finds post_501. The answer carries a fresh URL for the same key. Still one post |
| leo closes the app and never uploads | The row stays AWAITING_UPLOAD. The sweeper deletes it after one hour |
| Someone tries to use the URL for a 2 GB file | The URL allows at most 20 MB and only an image type. Storage refuses |
The trade-off to name. The client has two steps instead of one, and for a few seconds a post exists without a photo. The design accepts both: the post has a status, and nobody but the author sees it before the photo has arrived.
Yes, within narrow limits, and that is the point. The URL is a permission for one action: put one object at one key, with one content type and a size limit, for 15 minutes. It gives no right to read, list or delete. It is sent only to the logged-in author over an encrypted connection. The worst a thief can do is upload one wrong image for one post that is not visible yet. What the server must still do after the upload is check the file: the media worker reads it as an image, and a file that is not one ends as FAILED.
"I split the upload in two. The phone first sends only the metadata. I store the post row with a status of awaiting upload and return a pre-signed URL, so the acknowledgement depends on one small insert: about 50 milliseconds, not on a 4 megabyte transfer. Then the phone uploads the bytes directly to object storage. At 5,000 uploads a second that keeps 20 gigabytes a second and 40,000 open connections away from my API servers. When storage tells me the object exists, I move the post to processing and write an outbox event in the same transaction. That event starts the resizing and the fanout."
Follow-up: "What if the upload never happens?" — Then no event arrives, no follower was told, and a sweeper removes the row after an hour.
Scenario: post_501 is PROCESSING. Media worker W1 takes the job, writes the thumbnail and the standard copy, and dies before the full copy and before it recorded anything. A second job, post_666, is a broken file that no worker can ever resize. What does alice see for each post, and what is left behind in storage?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. When storage reports the arrival, the Post service makes the three copies itself, right in the handler of that event.
Where it breaks. The server writes two copies and dies. The event was handled once and is gone, so nobody tries again: post_501 stays PROCESSING, and alice sees a grey box for ever. And at peak 15,000 copies a second are made on the same servers that answer the API. Resizing eats their CPU, and the 300 ms answer is lost for everybody.
The idea. A queue holds one job per photo. Workers take a job, make the copies, record them and delete the job. A job that is not deleted within 60 seconds is handed out again.
What it fixes. The crash. When W1 dies, the job returns and W2 does it. alice sees the photo about a minute late.
What it still misses. Each worker names its files itself, for example a81f.webp. W2 writes three new files, and the two files of W1 stay in storage for ever: nothing points to them and nothing deletes them. And post_666 fails on every worker, returns after every try, and never stops. Each round takes a worker away from real work.
The key idea. Make a second run of the job produce exactly what the first run would have produced. Then running it twice is not a problem to prevent, only a little wasted work.
How it works
renditions/post_501/thumb.webp. W1 writes two of them.READY, in one transaction. Files first, record second. Then it deletes the job.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure. A dashed box belongs to one design only.
object created: originals/post_501Object storage → Post serviceNothing is written in this step.
Swipe to see all columns
| DD2 side by side | |||
|---|---|---|---|
| Bad: inside the request | Good: queue, random names | Great: queue, fixed names | |
| A worker dies halfway | Nobody tries again | The job runs again | The job runs again |
| Files nobody uses | 2 | 2 per crash, for ever | None: the same names |
Check it against the scenario
| The pipeline under failure | |
|---|---|
| Situation | Outcome with the Great option |
| W1 dies after two copies | W2 finds two files, writes the third, records all three. post_501 is READY about 61 seconds late |
| W1 was only slow, and W2 starts the same job | Both write the same names with the same content. Both record the same three rows: the unique key on post and type keeps one of each |
| post_666 can never be resized | Five tries, then the dead-letter queue and FAILED. The author is told. The feed lists the post without URLs, or leaves it out |
| The whole media fleet is down for ten minutes | |
The trade-off to name. One extra question to storage per retry ("which files exist?"), and a job that fails is not seen at once: it takes five tries before a person hears of it. In return, a crash at any point costs time only. A post lives in three places at once: the record in the database, the files in storage, the rows in feeds. The rule that keeps them together is the order: the original first, the copies second, the record last.
Because then the database can name a file that does not exist. If the worker dies after the record and before the file, the post says READY, the feed hands out a URL, and the reader gets "not found". The CDN may even keep that answer for a while. With files first, the only possible leftover is a file that no record names yet, and the next run of the job turns it into a finished copy.
"Resizing is asynchronous: a job per photo on a queue with a visibility timeout, so a crashed worker's job is redelivered. To make redelivery safe I use deterministic object keys, renditions slash post ID slash size. A worker writes the files first, then updates the database in one transaction, then acks. On a retry it checks which keys exist and only fills the gaps. Jobs that fail five times go to a dead-letter queue and the post is marked failed. The original is immutable and stays in storage, so any copy can be made again, today or with a better format next year."
Follow-up: "What does the reader see meanwhile?" — The post with status processing and no URLs. The app draws a placeholder and gets the URLs on its next refresh.
Scenario: at 12:00:00 maya, with 10,000,000 followers, and leo, with 300, publish in the same second. alice follows 200 authors. There are 300 fanout workers, and together they write 1,500,000 feed rows a second. At peak that is exactly the normal load: 5,000 posts a second for 300 followers each. And 300,000 feeds are read every second. Where is the work done: when a post is written, or when a feed is read?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. For every post a background job writes one feed row for each follower. Reading a feed is then one scan of the reader's own rows.
Where it breaks. leo's post is 300 rows. maya's post is 10,000,000 rows: 6.7 seconds of the whole fleet, and the fleet was already full. Every other post waits behind it. When ten large authors post within a minute, that is 67 seconds of the whole fleet, and leo's followers see his sunset more than a minute late.
The idea. A post writes nothing extra. When alice opens her feed, the Feed service looks up the authors she follows, reads the newest posts of each and merges them.
What it fixes. The write side. maya's post costs one row, like everybody's.
What it still misses. The cost moved to the side that is 60 times busier. alice follows 200 authors, so one feed read is 200 lookups. 300,000 reads a second x 200 = 60,000,000 lookups a second on the post store. A reader who follows 2,000 authors waits for 2,000 lookups, and the 300 ms are gone.
The key idea. Most authors are small, and a reader follows only a few large ones. So write for the small authors and read for the large ones.
How it works
The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure. A dashed box belongs to one design only.
INSERT home_feed_by_user ×300Fanout workers → Feed storealice, 12:00:00, post_501new rowSwipe to see all columns
| DD3 side by side | |||
|---|---|---|---|
| Bad: write for everyone | Good: read for everyone | Great: split by author size | |
| maya's post costs | 10,000,000 writes | 1 write | 1 write |
| leo's post costs | 300 writes | 1 write | 300 writes |
Check it against the scenario
| 12:00:00, two posts in the same second | |
|---|---|
| Situation | Outcome with the Great option |
| leo's post | 300 feed rows, written in a fraction of a second. Nothing stands in front of it |
| maya's post | One row in the hot list. Her 10,000,000 followers find it on their next feed read |
| Ten hot authors post within a minute | Ten rows. The fanout fleet does not notice |
| A reader follows 40 hot authors | One scan and 40 small reads, done in parallel. This is the case the limit protects: if too many authors are hot, raise it |
| An author grows past 100,000 followers | |
The trade-off to name. Two code paths instead of one, and a limit that has to be watched. The read is no longer one scan: it grows with the number of hot authors a reader follows. That number is small for almost everybody, and it can be capped.
This is the short version. The News Feed page takes the same question apart in full: push against pull, the limit, and how feeds are repaired after a follow or an unfollow.
Because the hot list is read on every feed read of every follower, so it must stay tiny. Twenty posts cover the first pages for almost all readers. A fixed size also caps the storage and the write rate per author, whatever they do. Older posts are not gone: they are in the post database and are read from there by author and time when someone scrolls that far.
"Neither pure model works, because the load is skewed. Fan-out on write gives cheap reads, but one post from an author with 10 million followers is 10 million writes. Fan-out on read gives cheap writes, but every feed load becomes one lookup per followed account, at 300,000 loads a second. So I split by follower count. Below a threshold, say 100,000, fanout workers write feed rows per follower. Above it, the post goes only into a small recent-posts list for that author, and the feed service merges those lists at read time for the few hot authors a reader follows. The threshold is a tunable number, driven by the fanout delay I observe."
Follow-up: "What is in a feed row?" — Reader, time, post ID, author ID. No caption and no URLs, so a post can change state without touching any feed.
Scenario: at 12:00:30 alice gets page 1: 20 posts. Slot 7 belongs to nina's post_502, whose copies are not ready yet. The last post of the page is post_431 from 11:58:07. The next older one, post_430, was created in the same second. In the next 30 seconds 5 new posts land at the top of her feed, and post_502 becomes READY. Then she scrolls down for page 2. What does she get?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Page 2 is "skip 20, take 20". To keep the feed pretty, the server leaves out posts whose photo is not ready.
Where it breaks. The server counts from the top of the list as it is now. Since page 1 the list grew by 6 above the place where alice stopped: 5 new posts, and post_502, which now counts. So page 2 starts with 6 posts she has just seen. It also fails in the other direction: if 3 rows above were removed after an unfollow, page 2 starts 3 posts too late, and alice never sees those three. An offset assumes that the list stands still. A feed never does.
The idea. Page 1 returns the time of its last post, 11:58:07. Page 2 asks for posts older than that.
What it fixes. The shifting. New posts on top and removed rows above change nothing below 11:58:07. No post is shown twice.
What it still misses. post_430 has the same time as post_431. "Older than 11:58:07" leaves it out, so alice never sees it. "Older or equal" shows post_431 twice. At 5,000 posts a second, two posts with the same timestamp are normal. And the server still hides posts that are not ready: post_502 becomes ready above her position, where she no longer looks.
The key idea. Order the feed by something that is unique and never changes, and let nothing else decide a post's place. "Is the photo ready?" changes what a slot shows, never where the slot is.
How it works
PROCESSING, without URLs: the app draws a placeholder in slot 7. The page ends with a cursor: an opaque token for the pair 11:58:07 and post_431.READY. Its place in the order does not move.post_id.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
/v1/feed/home?offset=0&limit=20Phone app (alice) → Feed servicethe first 20 ready postsNothing is written in this step.
Swipe to see all columns
| DD4 side by side | |||
|---|---|---|---|
| Bad: offset | Good: a time | Great: time and post ID | |
| New posts arrive on top | Page 2 repeats 5 posts | No effect | No effect |
| A photo becomes ready between pages | Shifts the list by one more | Appears where she no longer looks | The placeholder turns into the photo |
Check it against the scenario
| alice scrolls at 12:01:00 | |
|---|---|
| Situation | Outcome with the Great option |
| 5 new posts since page 1 | Page 2 is not affected. She sees them when she refreshes |
| post_502 became ready | Slot 7 keeps its place. The next refresh fills it with the photo |
| post_430 and post_431 share 11:58:07 | The pair of time and post ID orders them. Page 1 ends with post_431, page 2 starts with post_430 |
| A late fanout inserts a row between posts she has already passed | It lies above the cursor. Page 2 does not change. A refresh shows it |
The trade-off to name. No "jump to page 7", and the client has to remove duplicates by post ID after a refresh. A feed that is a few seconds behind can still show something new only on the next refresh. What it never does is repeat or lose posts while the reader scrolls.
Yes, because every source is ordered by the same pair. alice's own rows and each hot list are sorted by time and post ID. For page 2 the Feed service asks each source for items after the cursor, merges them and keeps 20. The new cursor is again the pair of the last item returned. No source needs to know about the others.
"Offset pagination assumes a stable list, and a feed is not one: fanout completes out of order, new posts land on top, repairs insert and remove rows. So I page with a cursor anchored to created-at and post ID, which is a total order even when two posts share a timestamp. The server returns items strictly after the cursor. The token is opaque so I can change its format. One thing is special for a photo feed: readiness. I never filter or sort by whether the renditions are ready. An entry keeps its position and carries a status, and the client swaps the placeholder for the image on a later refresh. Clients dedupe by post ID."
Follow-up: "Why not hide posts that are not ready?" — Then a state change would move items in and out of the list between two pages, and the cursor could no longer promise anything.
| If the interviewer keeps going | |
|---|---|
| Question | Short answer |
| maya's photo becomes ready and thousands ask for it in the same second | The first request in a region makes the CDN fetch the file once. The others wait for that fetch or are served from it. For the largest authors, the busiest regions can be warmed right after the post turns READY |
| A better image format next year | Process the originals again and write the copies under new names, with a version in the key. Update the rendition rows. The old files expire by themselves. Nothing in the CDN has to be invalidated |
| The copies of a post never succeed | The post ends as FAILED and the author is told. As a softer choice, the feed can hand out the original until an engineer has repaired the job |
For the patterns behind this page, see Handling Large Blobs and Fan-Out and Feed Generation, and the building blocks Blob Storage and CDN. The News Feed question is the same feed without the photos, in depth.
20 GB a second and 40,000 open connections land on servers that should answer in 300 ms. Sign a URL and let the phone upload straight to object storage.
The answer then depends on the phone's connection and on the resize queue. Store the record, answer, and do the rest in the background.
A crash in the middle leaves a post that is never finished, because nobody tries again. A queue with a visibility timeout hands the job to the next worker.
Every retry writes new files and leaves the old ones behind. Fix the name by post and size, so a second run writes the same files.
The original is the only thing that cannot be made again. Keep it unchanged, and every copy can be rebuilt at any time.
Writing to every follower breaks on an author with 10 million followers. Reading from every followed author breaks on every feed read. Split by author size.
Feed rows are copies that can be late, double or lost. Posts and follows in the database are the truth, and feeds are repaired from them.
Both let the list move between two pages, so posts repeat or vanish. Page with a cursor of time and post ID, and return every post with its state.
The shape of a strong answer: store a small record and answer at once. Give the phone a signed URL and let the photo go straight to object storage. When the photo has arrived, write one outbox event. From it, one pipeline makes three copies under fixed names, files first and record second, and another puts the post into feeds: rows for normal authors, a short hot list for large ones. Read a feed as one scan plus a few small reads, return every post with its state, and page with a cursor of time and post ID. Let the CDN carry every photo byte to the reader.
Small things through us, big things around us
The record is 2 KB and goes through the API. The photo is 4 MB and goes around it, in both directions: up with a pre-signed URL, down through the CDN.
One truth, many copies
Posts, follows and originals are the truth. Renditions, feed rows and CDN files are copies. A copy that is late or lost is made again.
Slow work after the answer, safe to run twice
Fixed file names, a unique key per post and type, a feed row keyed by reader, time and post. Any background step may run twice, and the result is the same.
| What the interviewer expects at each level | |
|---|---|
| Level | A strong answer shows |
| Junior | A clear happy path: follow, upload, scroll. Photos go to some file storage and a CDN, the feed comes from a database. Knows that serving images from the app servers does not scale |
| Mid-level | Separates the database of record from the derived feed store. Explains the pre-signed URL and why processing is asynchronous. Compares offset and cursor, and removes duplicates by post ID. Knows that very large authors need a different fanout |
| Senior | Drives to the three hard parts unasked: the life of a photo from upload to renderable and what happens on a crash, the split fanout with a tunable limit, and a cursor that survives late rows and photos that become ready. Says what the feed shows before processing ends and how every derived store is rebuilt |
These aren't toy problems — they reflect real infrastructure challenges at companies that serve billions of photos a day. If you want to walk through this design in a mock interview or get feedback on your own approach, book a session with us. We're here to help you sharpen your thinking, build your confidence, and land your next offer.
Pick an answer for each question, and you see at once whether it is right. Show explanation opens the answer, why it is right, and why each of the other options fails.
Question 1 of 10
leo taps Share. When does the Post service answer 201?
Question 2 of 10
Why does the phone upload the photo with a pre-signed URL?
Question 3 of 10
At peak there are 5,000 uploads a second, and every photo gets three copies. How many copies must the pipeline make every second?
Question 4 of 10
A media worker writes two of the three copies and dies before it recorded anything. What happens next?
Question 5 of 10
A photo is broken and no worker can resize it. What should the pipeline do?
Question 6 of 10
alice reads her feed while nina's photo is still being processed. What does the feed return for that post?
Question 7 of 10
Which of these can be lost and rebuilt without losing anything a user created?
Question 8 of 10
maya has 10,000,000 followers and publishes a post. What does the fanout write?
Question 9 of 10
alice follows 200 authors, 3 of them hot. What does one read of her home feed cost in the final design?
Question 10 of 10
Why is "skip 20, take 20" unsafe for page 2 of a home feed, and what replaces it?
| Runs again and writes what, where? Without fixed file names it leaves files behind, and a broken photo is retried for ever |
| DD3 | An author with 10 million followers posts. Who does the work? | A background job adds the post to every follower's feed | 10 million writes for one post stall everybody's fanout. Building feeds at read time costs 200 lookups per read. The answer is both, split by author size |
| DD4 | New posts arrive and a photo becomes ready while a reader scrolls. What is on page 2? | The next 20: offset 20 | The list moved. An offset repeats or skips posts. Use a cursor of time and post ID, and never let "ready" change the order |
| Feeds can be prepared in the background instead of computed on every scroll (Phase 3) |
| How large can an author be? | More than 10 million followers | One post cannot mean 10 million writes on the spot (DD3) |
| May a photo be lost? | Never. A copy may be late and can be made again | The original is kept unchanged. Everything else can be rebuilt from it (DD2) |
| What does the feed show while a photo is processed? | A placeholder | A post has a state, and the feed returns it (Phase 4, DD4) |
| The photo is in storage | Readers can see it | The three copies do not exist yet. Until then the feed shows a placeholder |
| A resize job ran | It ran once | A queue that loses nothing will sometimes hand a job out twice |
| alice follows maya | maya's post is stored in alice's feed | Posts of very large authors are added when the feed is read |
| A feed row was lost | The post is lost | Feed rows are copies. The post is a row in the post database |
| A phone needs about 8 seconds for 4 MB: 5,000 x 8 = 40,000 at any moment |
| Copies to make | 3 per photo: 15,000 a second |
| Feed reads at peak | 300,000 a second |
| One feed page | 20 photos of 80 KB = 1.6 MB. Built from originals it would be 80 MB |
| Largest author | More than 10 million followers |
| An outbox row in the same transaction |
| A post that is marked as arrived cannot lose its resize job or its fanout |
| Fixed file names for the copies | A resize job that runs twice writes the same files, and leaves nothing behind |
| Files first, record second | A copy that the database names always exists |
| Feed rows for normal authors, a hot list for large ones | Reading is one scan, and a post never costs 10 million writes |
| Feed rows hold IDs only | State and URLs are read fresh, so a photo that becomes ready needs no feed update |
| A cursor of time and post ID | Page 2 is right even when the feed changed after page 1 |
caption |
STRING |
| The text under the photo |
original_key | STRING | Where the original lies in object storage: originals/post_501 |
status | ENUM | AWAITING_UPLOAD, then PROCESSING, then READY. FAILED after five failed tries |
created_at | TIMESTAMP | Orders feeds and profiles |
idempotency_key | STRING | Sent by the phone. Unique per author, so a repeated request creates one post |
STRING |
| Second sort key. Breaks ties between posts of the same moment |
author_id | STRING | Who shared it. Used to clean up after an unfollow |
| Which copies exist. Written after the files |
| Outbox | event_id, post_id, author_id, hot, sent | The event "this photo has arrived", stored in the same transaction as the change |
| Follower list | Per author, in buckets: the IDs of the followers | What the fanout reads. Buckets keep the list of a large author in pieces |
| Hot list | Per hot author: the newest 20 posts | What the feed read adds for authors with more than 100,000 followers |
| Queue message | A media job such as post_501, or a fanout job | Work waiting for a worker. Taken, hidden for 60 s, deleted when done |
| Media queue and Fanout queue | One message per job | At least once. A message that is not deleted within 60 s is handed out again |
| CDN | Copies of the photo files near the readers | A cache. A missing file is fetched from object storage once |
GET /v1/feed/home?limit=20&cursor= |
One page of the home feed, newest first. Each item: post_id, author_id, caption, status, rendition_urls, created_at. Plus next_cursor. limit is at most 50 |
GET /v1/users/{author_id}/posts | The profile grid of one author, read from the post database, with a cursor |
POST /v1/users/{author_id}/follow | Follows an author. Answers 204. A second call changes nothing |
DELETE /v1/users/{author_id}/follow | Unfollows. Answers 204 |
READY| Phase 3: Tell the followers (FR3, FR4) | Green | A fanout worker writes one feed row for each of leo's 300 followers. maya's post gets one row in the hot list | 300 feed rows, one hot-list row |
| Phase 4: Read the feed (FR4, FR5) | Purple | alice asks for a page. Her rows and the hot authors are merged, and the photos come from the CDN | Nothing. Reading writes nothing |
| 3 |
| The Post service answers 201, 50 ms after the tap: the post ID, the status and an upload URL that is valid for 15 minutes and for this one key. |
| Nothing |
| 4 | The phone sends sunset.jpg, 4 MB, straight to object storage with the upload URL. It takes about 8 seconds. Our servers are not involved. | Object originals/post_501 |
| 5 | Object storage reports to the Post service: an object was created at originals/post_501. | Nothing |
| 6 | The Post service marks the photo as arrived and writes the outbox row, in one transaction. | post_501: AWAITING_UPLOAD to PROCESSING. One outbox row |
| 4 |
| It writes three copies: renditions/post_501/thumb.webp (150 px), standard.webp (640 px) and full.webp (1080 px). About one second for all three. |
| Three objects, 340 KB together |
| 5 | It records the three copies and sets the post to ready, in one transaction. | Three media_renditions rows. post_501: PROCESSING to READY |
| 6 | It deletes the queue message. | The message is gone |
| Nothing |
| 4 | It writes one feed row for each follower: reader, 12:00:00, post_501, leo. | 300 feed rows. alice's feed has a new first row |
| 5 | It deletes the queue message. | The message is gone |
| 6 | maya's post_777 arrives as a hot job. The worker writes one row into the hot list, which keeps maya's newest 20 posts. No row for any of her 10,000,000 followers. | Hot-list row maya: post_777 |
| 4 | It fetches the details of these 20 posts from the Post DB in one batched read, through a cache: caption, status, copies. | Nothing |
| 5 | It answers with 20 items and a cursor. post_501 is READY and has three CDN URLs. nina's post_502 is still PROCESSING and has none: the app shows a grey placeholder. | Nothing |
| 6 | The app loads the standard copy of post_501 from the CDN: 80 KB. The first request in alice's region makes the CDN fetch the file from object storage once. Everyone after her gets it from the CDN. | The CDN keeps the file |
| DD1 |
| 20 GB of photos a second |
| 5,000 uploads a second, 8 seconds each |
| NFR1, NFR2 — Scale, speed |
| DD2 | A media worker dies halfway | Two of three copies written, and one broken photo | NFR3 — Nothing lost |
| DD3 | An author with 10 million followers posts | A hot post and a small post in the same second | NFR5 — No work per follower |
| DD4 | The feed changes while alice scrolls | 5 new posts and one newly ready photo between two pages | NFR4 — A feed that is behind, but never wrong |
| More than 8 s |
| 8 s |
| 50 ms |
| An app server restarts | Its uploads are lost | Its uploads are lost | No upload passes it |
| What the phone must do | One request | One request | Two steps: ask, then upload |
FAILED| A broken photo |
Stuck in PROCESSING |
| Retried for ever |
FAILED after 5 tries, job kept |
| What alice sees for post_501 | A grey box for ever | The photo, a minute late | The photo, a minute late |
| The original | Kept | Kept | Kept, and never rewritten |
| Uploads are still accepted and posts appear in feeds as placeholders. The queue grows, and is worked off afterwards |
| 1 scan |
| 200 lookups |
| 1 scan and 3 small reads |
| What breaks | The fanout, when a large author posts | The read path, all the time | Nothing. The limit needs tuning |
| How fresh | Seconds, or minutes behind a hot post | Immediate | Seconds for small authors, immediate for hot ones |
| The next post goes to the hot list. Old feed rows of that author stay and age out |
| Two posts in the same second |
| No effect |
| One is left out, or one comes twice |
| Ordered by post ID |
| Rows are removed above | Posts are skipped | No effect | No effect |
| What the client sends | A number | A time | A token it cannot read |
| alice follows a new author |
| The follow is stored at once. A background job adds a bounded window of that author's recent posts to alice's feed rows. For a hot author nothing is needed: the hot list is read anyway |
| alice unfollows an author | The follow row is deleted at once. Her old feed rows are cleaned in the background, and until then the feed read drops posts of authors she no longer follows |
| The fanout workers fall behind, or feed rows are lost | Add workers and replay the outbox from a known point. Feeds are behind for a while but readable. Posts and follows are the truth, so the repair is a rebuild, not a guess |
| A post is deleted | Mark it deleted in the Post DB. The detail lookup of the feed read drops it from every feed at once. Feed rows and files are removed in the background |
| A ranked feed instead of newest first | A scoring step reorders the same candidates before the page is returned. If it fails, the feed falls back to time order. The stores stay as they are |
| Likes and comments | Out of scope here. Counts are a counter service of their own, see [Distributed Counters](/learn/system-design/questions/distributed-counters) |