To design Instagram is to build a service where members upload photos and short videos, follow each other, open a feed of recent posts, like, comment and search. A good answer explains how large files get into storage reliably, how one upload becomes the right sizes for the right screens, how a feed is built from the people someone follows, how likes are counted when one post goes viral, how deleted and blocked content stays out of every feed, and how videos are encoded without making everyone wait.
This lesson designs FrameIt, a photo-sharing site for a university photography community. Six runnable programs in Java, with Python and C++ versions, test the decisions interviewers probe most: the upload path, image sizes, feed generation, like counters, feed filtering, and video encoding. All sizes, rates and timings are assumptions chosen to make comparisons clear, not benchmarks of a product.
Key takeaways
Sending photos through our own API servers handled 100,398.0 MB there, against 0.0 MB when the phone uploaded straight to the blob store. Uploading in 0.5 MB chunks and resending only a failed chunk gave up on 3 of 20,000 uploads, against 146 when the whole file was restarted.
Sending originals for every feed and grid needed 3,199.4 GB for 706,000 picture requests. Resized copies needed 116.2 GB, about 27 times less.
Copying every post to every follower wrote 1,734,073 list entries and one post by a large account wrote 17,207. Reading from everyone followed made 3,000,000 reads. A hybrid made 1,652,253 writes, 120,000 reads, and capped the worst post at 2,992.
A counter raised on every tap counted 30,524 repeat taps as likes, and the viral post's row took 200,267 writes. Unique likes with a counter flushed every 5 seconds cut the busiest row to 720 writes.
Showing stored feed ids without a check showed 24,311 posts that should have been hidden. Checking only the first 20 left 14,334 of 20,000 pages short, and reading 10 more until the page was full left none.
Cutting long videos into 60-second segments made long videos 10 times faster (81 seconds instead of 775) and finished the batch at 4,587 seconds instead of 5,688.
1. Clarifying Requirements
Functional requirements
Members upload photos and short videos with a caption and tags.
Members follow and unfollow others. Accounts can be public or private.
A member opens a feed of recent posts from the accounts they follow.
Members like and comment, and see the like count.
Search by username and hashtag.
Members delete their own posts, and block others.
Non-functional requirements
The feed opens in under a second on a normal phone connection, with pictures sized for the screen.
Uploaded media is never lost once the member has seen it posted.
High availability, and a feed that may be a little behind but must never show a deleted post or a post from a blocked account.
Handle accounts with a very large number of followers and a post that suddenly goes viral.
Ask about video length, whether stories and direct messages are in scope, and how fresh the feed has to be. Leave out direct messages, stories, ads and recommendations, and say so.
2. Back-of-the-Envelope Estimation
Uploads: 10 million daily users, 1 in 10 posting a photo, is 1 million photos a day, about 12 uploads a second.
Storage: at 3 MB a photo that is 3 TB a day and 1.1 PB a year of originals, with resized copies adding about 15%.
Views: 100 photo views per user per day is 1 billion a day, 11,574 a second and about 50,000 at the peak.
Ratio: reads outnumber writes about 1,000 to 1, so the design is about bytes served, caching and feed reads.
Uploads are modest. Views and bytes decide the design.
3. Core APIs
POST /v1/uploadsreturns a signed upload address and an upload id. The app sends the file to that address in chunks.POST /v1/postswith{"upload_id": "...", "caption": "...", "tags": ["..."]}creates the post after the file is complete.GET /v1/feed?cursor=...&limit=20returns posts with picture addresses. The cursor names the last post seen, as in Design Slack.PUT /v1/posts/{id}/likeis idempotent: a second call changes nothing.PUT /v1/users/{id}/followandDELETEfor unfollow, andDELETE /v1/posts/{id}for deleting.
4. High-Level Design
Apps talk to an API gateway that authenticates and rate limits. Behind it, a post service owns post metadata, a graph service owns follow edges, a feed service owns the stored feed lists, and a like service owns the counters. Media never travels through these services: files go to a blob store, a queue wakes media workers that resize and encode, and a CDN serves the results from the edge.
Metadata in services, media in a blob store, delivery from a CDN.
An upload has three steps, and the order matters. The app asks for a signed address and sends the file straight to the blob store. Only when the file is complete does it call the post service, which checks that the file exists and creates the post. A queue then starts the media workers, the safety checks and the feed fan-out. A post is never created for a file that did not arrive, and a file with no post is cleaned up later.
The file goes to storage first, the post is created after.
A feed read checks every post before it is shown.
5. Getting the Upload In
A photo of several megabytes travels over a mobile network that sometimes drops. Three designs: send the photo through our API servers in one request, send it in one request straight to the blob store using a signed address, or send it to the blob store in chunks and resend only a failed chunk.
The first program has 20,000 uploads of 1 to 8 MB, made of 0.5 MB chunks. Each chunk fails with 2 chance in 100, and an upload is given up after 3 tries. Modes 0 and 1 use the same failures, so they differ only in where the bytes go.
Output
20000 photo uploads, 1 chunk in 50 fails, 3 tries.
how the photo is uploaded uploads given MB sent by the MB through our MB sent that
up phone API servers was wasted
through our API server, one request 146 100398.0 100398.0 10846.0
straight to blob store, one request 146 100398.0 0.0 10846.0
straight to blob store, in chunks 3 91552.5 0.0 1803.0
Through our API servers, one request: 146 uploads were given up, and 100,398.0 MB passed through the API servers, which then needed bandwidth and workers for media instead of for requests.
Straight to the blob store, one request: the same 146 given up and the same 100,398.0 MB sent by phones, but 0.0 MB through our servers.
Straight to the blob store, in chunks: 3 uploads given up, 91,552.5 MB sent, and 1,803.0 MB wasted instead of 10,846.0 MB.
Upload in chunks, directly to storage.
Chunked uploads also allow resuming after the app is closed: the app asks which chunks the store already has. Check the file at the end (size and a checksum), scan it for safety, and never trust the type the client claims. A cleanup job removes uploads that never became a post.
6. Sizes for Every Screen
A photographer's original is 3 to 6 MB, and a feed on a phone needs about 250 KB. A grid of thumbnails needs 30 KB each. Sending originals everywhere wastes bandwidth and makes the feed slow. The choice is when to make the smaller copies: at upload for every size, or on the first request for each size.
The second program has 10,000 photos with originals of 3,000 to 6,000 KB. Three sizes exist: a 30 KB thumbnail, a 250 KB feed picture and a 1,200 KB full view. 20,000 feed loads show 20 pictures each, 10,000 profile grids show 30 thumbnails, and 6,000 times someone opens a photo in full. 8 in 10 views go to the 1,000 most popular photos.
Output
10000 photos, 706000 picture requests.
what is stored and sent GB sent GB of extra copies resize jobs requests that waited for a resize
always send the original 3199.4 0.0 0 0
make all sizes at upload 116.2 14.8 30000 0
make a size on first request 116.2 5.3 22087 22087
Always send the original: 3,199.4 GB for 706,000 picture requests.
All sizes at upload: 116.2 GB sent, 14.8 GB of extra copies, 30,000 resize jobs, and no request waits for a resize.
A size on first request: the same 116.2 GB sent, only 5.3 GB of extra copies and 22,087 resize jobs, but 22,087 requests waited for a resize, and the first viewer of a new size pays for it.
Resized copies cut the bytes about 27 times.
A sensible mix is to make the feed size and the thumbnail at upload, since almost every post needs them, and to make rare sizes such as the full view on demand behind a CDN. Because 8 in 10 views go to popular photos, the CDN hit rate on the pictures that matter will be high.
7. Building the Feed
A feed is the newest posts of the accounts a member follows. Two simple designs: fan out on write, where each post is copied into the feed list of every follower so that a feed load reads one list, and fan out on read, where a post is stored once and a feed load reads the recent posts of everyone followed and merges them. The first makes writes expensive for accounts with many followers, and the second makes every feed load expensive. A hybrid copies posts of ordinary accounts and reads the large accounts when the feed is opened.
The third program has 20,000 members. 9 in 10 have 10 to 300 followers, 99 in 1,000 have 1,000 to 3,000, and 1 in 1,000 has 15,000 to 19,000, which gives 26 members with 5,000 or more. 5,000 posts are made in a day, and 20,000 feed loads happen. Each member follows 150 accounts, and in the hybrid (an assumption) 1 in 30 of them is a big account.
Output
20000 members, 26 have 5,000 or more followers. 5000 posts and 20000 feed loads in a day.
how the feed is built writes for 5000 lists read for 20000 most writes for
posts feed loads one post
copy to every follower 1734073 20000 17207
read from everyone followed 5000 3000000 1
copy, except big accounts are read 1652253 120000 2992
Copy to every follower: 1,734,073 list writes for 5,000 posts, 20,000 list reads, and 17,207 writes for the largest single post.
Read from everyone followed: only 5,000 writes, but 3,000,000 reads, since every feed load merges 150 sources.
Copy except big accounts, which are read: 1,652,253 writes, 120,000 reads and at most 2,992 writes for one post.
The hybrid mainly caps the worst case.
The hybrid saved only 5% of the writes, because the mid-size accounts with 1,000 to 3,000 followers make most of them. It is the worst case it fixes: a post by a 19,000-follower account no longer creates 17,000 writes at once, and no single feed load needs more than a handful of extra reads. Lower the threshold and more accounts are read at open time, trading write cost for read cost. Also skip fan-out to members who have not opened the app for weeks, and build their feed when they return. See Design a Distributed Cache for where the feed lists live.
8. Counting Likes
A like looks like adding 1 to a number. Two things go wrong. A repeated tap or a resent request must not count twice, and a viral post turns one counter row into a bottleneck, because every update to the same row has to wait for the previous one. Designs: a plain counter, a table of unique likes plus a counter, a counter split into several rows, and a counter that adds likes in memory and writes every few seconds.
The fourth program has 1,000,000 taps in an hour. 2 in 10 go to one viral post, the rest to 5,000 random posts, and 3 in 100 taps are repeats by someone who already liked the post. The flush design writes at most once every 5 seconds, which is 720 times an hour for a post (an upper bound).
Output
1000000 taps in an hour, the viral post gets 200267 of them, 30524 taps are repeats.
how likes are counted writes to the counter writes likes shown reads for count can
busiest row for the hour too high a count be late
one counter, every tap 200267 1000000 30524 1 row 0 s
unique likes, one counter 194240 969476 0 1 row 0 s
unique likes, 16 counter rows 12140 969476 0 16 rows 0 s
unique likes, flush every 5 s 720 775956 0 1 row up to 5 s
One counter, every tap: the viral post's row took 200,267 writes and 30,524 repeat taps were counted as likes.
Unique likes, one counter: 0 likes too high, but the busiest row still took 194,240 writes.
Unique likes, 16 counter rows: 12,140 writes to the busiest row. A count reads 16 rows and adds them.
Unique likes, flush every 5 seconds: 720 writes to the busiest row and 775,956 counter writes in total, with a count that can be up to 5 seconds late.
Count each person once, and spread or batch the hot counter.
Exact counts are rarely needed: a feed can show a rounded count such as 12.4K, while the unique-likes table stays exact for "did I like this". A tap goes first to the unique table with the primary key (member, post), and only a new row raises the counter. The same pattern serves follower counts and comment counts.
9. Keeping the Feed Clean
Feed lists are built when a post is made, and a lot can change before the feed is opened: the post is deleted, the member unfollows the author, the author makes the account private, or one of them blocks the other. A list that holds only ids cannot know. The check has to happen when the feed is read, and it must not leave a short page.
The fifth program has 20,000 feed loads. Each stored list holds 100 ids, and 6 in 100 ids should no longer be shown. It compares showing the first 20 ids with no check, checking the first 20 and dropping failures, and checking and then reading 10 more ids at a time until 20 are left.
Output
20000 feed loads, 6 ids in 100 of each stored list should not be shown.
how the feed page is made posts read posts shown that pages with fewer posts shown
for 20000 pages should be hidden than 20 posts in total
no check 400000 24311 0 400000
check the first 20 400000 0 14334 375689
check, read 10 more until 20 543340 0 0 400000
No check: 24,311 posts were shown that should have been hidden, which for private and blocked accounts is a privacy failure.
Check the first 20: no leaks, but 14,334 of 20,000 pages had fewer than 20 posts (375,689 shown out of 400,000 slots).
Check, then read 10 more until full: no leaks and no short pages, with 543,340 posts read instead of 400,000, which is 36% more reads.
Check on read, and fill the page.
The extra reads are the price of correctness, and they are batched, so they cost little. A deletion or block can also push a removal to the stored lists (a background cleanup), so that fewer ids fail at read time, but the read-time check stays because a cleanup is never instant. For private accounts, store the follower state with the edge (pending or active) so that a pending request does not allow viewing.
10. Encoding Videos
A video must be encoded into several qualities before it can be played, and an encode takes from seconds to many minutes. If long videos and short ones share workers, the question is who waits. Designs: one queue in arrival order, separate workers for short videos, or cutting long videos into short segments that run in parallel and are joined at the end.
The sixth program has 3,000 videos arriving over about 75 minutes, one every 1 to 2 seconds. 9 in 10 take 10 to 60 seconds to encode on one worker, and 1 in 10 take 300 to 1,200 seconds. There are 80 workers. In the segment design a long video is cut into 60-second pieces, and 5 seconds are added to join them.
Output
3000 videos, 80 workers. Times are from upload until the video is ready, in seconds.
how encoding is scheduled short videos short videos long videos last video
average slowest 5% average ready at
one queue, first come first served 37 60 775 5688
separate workers for short videos 35 58 785 5715
long videos cut into 60 s segments 47 85 81 4587
One queue, first come first served: short videos were ready 37 seconds after upload on average, long ones after 775, and the last video was ready at 5,688 seconds.
Separate workers for short videos: 35 and 785 seconds, and the last at 5,715. With 80 workers and a load below capacity, short videos were never blocked in the first place, so the lane barely helped.
Long videos cut into 60-second segments: long videos ready after 81 seconds, 10 times faster, and the last video at 4,587 seconds, 19% earlier. Short videos got a little slower (47 seconds on average, 85 for the slowest 5%, against 60), because they now share workers with many segments.
Segments speed up long videos and finish the batch sooner.
Whether lanes help depends on the load. When workers are scarce and a few long jobs occupy them all, short videos do wait behind them, and a reserved lane or priority queue is the fix. Segmenting needs a joining step and care with keyframes, and it makes the most of idle workers. In all cases show a quick low-quality version first, and make the high qualities later. See Design Message Queue for the queue itself.
11. The Follow Graph and Search
A follow is a directed edge from the follower to the account, and it is read in both directions: "who do I follow" builds the feed, and "who follows me" lists followers and drives fan-out. Store it twice, by follower and by followed, and update both. Private accounts give the edge a state (pending or active). The followers of a very large account are listed in pages with a cursor and never loaded whole, and the counts are kept in separate counters.
Follow edges are stored both ways and have a state.
Usernames and hashtags go into a search index that is updated from the post queue. A deleted post must also be removed from the index, and a private account's posts must not be indexed for public search. Rank hashtag results by recency and engagement. Take care that the hot hashtags of a big event do not overload one index shard.
12. Choosing the Design
Follow a photo from the upload to the screen.
| Concern | Choice | Watch for |
|---|---|---|
| Upload | Chunks straight to the blob store | Large files through API servers |
| Sizes | Common sizes at upload, rare on demand, CDN | Originals in the feed |
| Feed | Copy ordinary posts, read big accounts | Fan-out of one huge account |
| Likes | Unique likes plus a sharded or flushed counter | A single hot counter row |
| Clean feed | Check on read, read more if short | Ids shown without a check |
| Video | Segment long videos, quick first version | Long jobs holding all workers |
| Graph | Edges both ways with a state | Loading all followers at once |
13. Trade-offs to Mention
Direct uploads save API bandwidth and need signed addresses and a way to clean up unused files.
Making sizes early costs storage, and making them late costs the first viewer a wait.
Copying posts into feeds makes reads fast and writes heavy, and a hybrid adds the complexity of two paths.
A flushed counter is cheap and a little late, and a unique table adds one write per like.
Reading more ids to fill a page costs extra reads and avoids short pages.
Segmenting videos speeds long ones and slows short ones a little.
14. Common Mistakes
Sending uploads through the API servers and restarting whole files after a failure.
Serving the original photo in a feed or a grid.
Fanning out every post of a very large account to every follower.
Counting taps instead of unique likes, and keeping one hot counter row.
Trusting stored feed ids without checking deletion, blocking and privacy on read.
Letting a few long encode jobs hold every worker.
Loading the whole follower list of a big account.
15. Interview Questions
How does a photo get into the system? Chunks straight to a blob store through a signed address, then a post is created, and a queue starts the processing.
How do you serve images fast? Resized copies for each screen, from a CDN.
How is the feed built? Copy posts of ordinary accounts into follower lists, read big accounts at open, and check every post on read.
How do you count likes for a viral post? Unique likes, and a counter split into rows or flushed in batches.
How do you keep deleted posts out of feeds? Check on read, and clean the lists in the background.
How do you encode video? Workers pulling from a queue, long videos cut into segments, and a quick first version.
How do you list followers of a huge account? Pages with a cursor, and counts kept separately.
16. Practice Problems
An upload of 8 MB in 0.5 MB chunks fails each chunk 2 times in 100. What is the chance that all 16 chunks pass on the first try? Answer: 0.98 to the power 16 is about 72%, so about 28% of uploads need at least one resend. With chunking that costs one chunk each, not the whole file.
A member with 19,000 followers posts every hour. How many list writes a day with fan-out on write? Answer: 19,000 times 24, which is 456,000, so read this account at open time instead.
Why not delete a post by removing it from every follower list? Answer: that is millions of writes, and it is never complete, so check on read.
A viral post has 200,000 likes in an hour and 16 counter rows. How many writes does each row take on average? Answer: about 12,500, which a row can handle, and a count sums 16 rows.
Summary
Upload in chunks straight to a blob store, and create the post after the file is complete.
Serve resized pictures from a CDN, with common sizes made early.
Copy posts of ordinary accounts into feeds, and read big accounts when the feed opens.
Count unique likes, with sharded or flushed counters for hot posts.
Check deletion, blocking and privacy when the feed is read, and fill short pages.
Encode videos from a queue, cutting long ones into segments.
Conclusion
Instagram looks like a list of photos, and the interesting questions are in the numbers: the upload that must survive a weak network without loading our servers, the 27-fold saving from sending the right size, the account whose post triggers 17,000 writes, the like counter that one viral post can melt, the stored feed that can show a post that was deleted a minute ago, and the video that keeps 80 workers busy for minutes. In an interview, state the numbers (uploads a second, views a second, bytes), then show how each choice follows from them.
Previous topic: Design Slack.
References and further reading
Amazon S3 documentation on presigned URLs and multipart upload, and the tus open protocol for resumable uploads.
Engineering posts from photo-sharing companies on feed ranking, fan-out and counting at scale.
Documentation on image resizing services and CDN caching, such as Cloudflare Images and imgix.
FFmpeg documentation on segmenting and concatenating video, and HTTP Live Streaming (RFC 8216) for segment-based playback.
Design Object Storage, Design a Distributed Cache, Design Message Queue and Design Slack on this site, for the pieces used here.