How to design Instagram
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
The problem
Instagram keeps the photos people share and shows each person a feed of recent photos from the accounts they follow. Two facts shape the answer. Photo bytes are large next to the row that describes them, so they should travel from the phone into object storage and out through a CDN without passing through the application servers. And feeds are read far more often than photos are posted, so most feeds are assembled ahead of time at publish time, while the few accounts followed by tens of millions are handled differently so one photo doesn't turn into a flood of writes.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
How many users are we designing for, and how do feed reads compare with uploads? | The read-to-write ratio decides whether feeds are built at publish time or when the feed is opened, and the user count sizes the stored feeds. | About 2 billion users. Feeds are read far more often than photos are posted, and the biggest accounts have about 90 million followers. |
How quickly must a new post show up in followers' feeds, and how fast should the feed open? | A delivery budget measured in seconds would need a very different pipeline from one measured in a minute. The open-time budget rules out building a feed by scanning everyone a user follows. | Up to 1 minute for a new photo to reach followers' feeds. Opening the feed should take under 500 ms. |
Photos only, or video as well? | Video adds transcoding and streaming, which is a design of its own. Knowing it's out keeps the media path to a single object per post. | Photos for this round. Expect a follow-up asking how you'd add short and long videos. |
Is the feed in time order, or ranked? | A ranked feed changes what gets stored per user and when it's computed. A chronological feed is a sorted list of post ids. | Newest first. Ranking and the Explore page are out of scope. |
Are all accounts public, or can an account be private? | If a photo may be seen only by approved followers, its URL can't be a public link that anyone could pass around. | Assume public accounts for the core design, but say how you'd handle private ones: the design we recommend already serves photos through links that expire. |
Do we need likes, comments, stories or messages? | Each is its own write path and its own storage. Naming them as out of scope lets you spend the time on uploads and the feed. | No. Likes and comments, stories and live video, the Explore page and ranking, and direct messages are all out of scope. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
Read-heavy: far more feed views than uploads | Spend the design effort on the read path: a feed is a lookup of ids prepared earlier plus one batch fetch of post rows, and photos come from a CDN. The write side can afford to do more work per post, including fanning it out to followers ahead of time. |
Feed loads in under 500 ms | No scan over everyone a user follows at read time. The feed service reads a short stored list of post ids, merges in the recent posts of the few huge accounts the user follows, fetches the rows in one batch and returns links. Photo bytes load from an edge location near the viewer rather than from the storage region. |
An uploaded photo is never lost | Bytes go into replicated object storage, and the post row moves to ready after storage reports the object exists. Metadata lives in a database with a standby. The failure to design for is the half-finished upload: a pending row with no object behind it. |
A new post may take up to 1 minute to reach followers’ feeds | Delivery to feeds can be asynchronous: the post service hands a job to a queue and returns, and workers write ids into followers' feeds behind it. The queue absorbs bursts and short worker outages, and nothing has to push posts live into open apps. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Storage for a prepared feed for each user | 200 post ids × 10 bytes = 2 KB for one user's list; 2 KB multiplied by 2 billion users = 4 TB | About 4 TB across all users | The feed store. Keeping a ready-made feed for each user is affordable spread over a cluster of memory or SSD nodes, so the design builds feeds ahead of time rather than at read time. |
Write rate to deliver one photo from the biggest creator inside the budget | 90 million followers = 90 million feed writes per post; 1 minute = 60 seconds; 90 million ÷ 60 seconds = 1.5 million writes per second | About 1.5 million feed writes a second for a single post | Why a loop on one host fails for a huge account, and why the design either spreads the job across many workers or skips writing that post into feeds at all. |
Share of all feeds one photo from the biggest creator touches | 90 million ÷ 2 billion = 0.045 = 4.5% | About 4.5% of all stored feeds, from a single post | The case for treating huge accounts differently: a single account can touch a sizable share of all feeds with one photo, so a special path for the few accounts like it removes the largest individual fan-out jobs. |
Core entities
| Entity | What it holds |
|---|---|
User | An account that posts, follows and reads a feed. |
Follow | One row per follower and followed pair, indexed both ways because the feed reads it in both directions. |
Post | The metadata for one shared photo; the bytes live elsewhere under the media key. |
Media object | The photo bytes in blob storage, written by the phone and read by the CDN. |
Precomputed feed | A short list of post ids per user, newest first, kept in a key-value store. |
The API
| Endpoint | Notes |
|---|---|
| Checks the caption and media type, writes a pending row, and returns a presigned upload URL tied to the post's object key with a short expiry. Nothing is fanned out yet. Accept a client-supplied idempotency key so a retried tap doesn't create two posts. |
| The phone sends the bytes to object storage, not to our servers. The signature fixes the key and the window in which the upload is accepted. Completion reaches the post service as a storage event; a |
| Idempotent: inserting the same follower and followed pair twice is a no-op, so a retry is safe. Optionally enqueue a small backfill that pulls the followed account's latest posts into the follower's feed. |
| Reads ids from the caller's stored feed older than the cursor, merges in recent posts from any huge accounts they follow, fetches the post rows in one batch, and returns items carrying signed CDN links at the sizes the app needs. next_cursor carries the last item's timestamp with its post id encoded alongside as a tie-breaker, so two posts that share a timestamp paginate cleanly; an empty cursor means the top of the feed. |
The high-level design
Step 1 of 4
Post, follow, and build the feed when it's opened
Start with the two paths. The post service takes POST /posts, writes the photo into blob storage and inserts the post row. A feed service answers GET /feed by reading whom the caller follows, pulling each of those accounts' recent posts from the post table and sorting them by time. A follow table holds the graph. This is a complete, correct system and the right first picture to draw, and it has two weaknesses the next steps answer: photo bytes pass through the post service, and each feed read does work proportional to the number of accounts followed.
What breaks first
The deep dives
Getting photos in and out. Someone taps share on a photo. Where do the bytes go, when does the post count as published, and how does a follower on another continent load the picture quickly?
Building the feed. An account with about 90 million followers posts a photo. How does it reach those followers' feeds within the minute you promised, and what do feeds show when the delivery path stalls?
Concepts this guide uses
Sources
- Amazon S3 User Guide: Uploading objects with presigned URLs
- Amazon S3 User Guide: Event notification types and destinations
- Amazon S3 User Guide: Configuring a bucket lifecycle configuration to delete incomplete multipart uploads
- Amazon CloudFront Developer Guide: Serve private content with signed URLs and signed cookies
- Amazon CloudFront Developer Guide: Use signed URLs
- Amazon CloudFront Developer Guide: What is Amazon CloudFront?
- Amazon SQS Developer Guide: Amazon SQS queue types
- Arkadiusz Chmura: How Twitter handled personalized timelines for their users (on Designing Data-Intensive Applications)