Skip to content

How to design Instagram

The first sections are free. The deep dives, level expectations and quiz are part of Pro.

On this page
Asked at
Practice this question

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

AskWhy it mattersA 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.

The requirements menu

RequirementScopeWhy

Upload a photo with a caption

Core

The write path. It decides where photo bytes travel and when the post counts as published.

Follow other users

Core

The follow graph is what the feed is built from, and it is read in both directions: whom I follow, and who follows me.

See a feed of photos from followed users, newest first

Core

The read path, and the one that carries most of the traffic. Its latency budget drives the design.

Open a photo at full size

Core

It fixes how photos are served to viewers: from a CDN, at more than one size, through links the app hands out.

Unfollow, and have that account's photos leave your feed

Optional

Worth a sentence: deleting the follow row is easy, but post ids already in the stored feed stay there until they age out or you filter them at read time. Take it if the interviewer asks what happens after an unfollow.

Delete your own post after it is shared

Optional

Its id may already sit in millions of stored feeds. Filtering out deleted posts when the feed is read is the approach we'd take, and it also covers a photo that failed moderation. Leave it unless asked; nothing else depends on it.

Several photos in one shared item (a carousel)

Optional

Changes the post row from one media key to a list, and the upload from one presigned URL to several. Mention that the design extends to it and move on.

Private accounts that approve followers

Optional

Tempting to skip, but it matters to the media path: photo links must expire and be checked, so a link copied out of the app stops working once its expiry passes. Our design serves photos through signed links anyway, so say so.

Notifications when someone you follow posts

Out of scope

Pushing each post live to open apps is a separate delivery system, and the 1 minute budget doesn't need it. Name it and park it.

Likes and comments

Out of scope

Counters and comment threads are their own storage and write path. Say they'd hang off the post id and leave them.

Stories and live video

Out of scope

Expiring media and live streams need a different media pipeline. Out for this round.

The Explore page and ranking

Out of scope

Ranking changes what a feed stores and when it's computed; a chronological feed keeps the round tractable.

Direct messages

Out of scope

A chat system with its own delivery and storage. Nothing in the feed design depends on it.

Non-functional requirements

RequirementWhat 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

QuantityWorkingResultWhat 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

EntityWhat 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

EndpointNotes

POST /posts { caption, media_type } → { post_id, upload_url }

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.

PUT {upload_url} (photo bytes, straight to blob storage)

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 POST /posts/{id}/complete call from the client is a fallback if the event path is unavailable.

PUT /users/{id}/follow

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.

GET /feed?cursor={timestamp}&page_size={n} → { items, next_cursor }

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

createbytesfeedpostsClientBlob storePost APIPost rowsFollowsFeed API

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.

POST /postsPUT bytesupload eventpending, readyold pendingfan-out jobfollowersprepend idfailed jobsoriginphotosGET /feedfollowingpage of idsbig creatorsClientPost serviceBlob storagePost metadataSweeperFan-out queueFeed workersFollow tableFeed storeDead lettersCDNViewerFeed service
The whole design

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?

A concept that helps here: Blob storage and presigned uploadsA concept that helps here: CDNs and edge cachingA concept that helps here: Idempotency keys

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?

A concept that helps here: Fan-out on write and on readA concept that helps here: Delivery semantics: at most, at least, exactly onceA concept that helps here: Hot keys and hot partitions

Concepts this guide uses

Sources

  1. Amazon S3 User Guide: Uploading objects with presigned URLs
  2. Amazon S3 User Guide: Event notification types and destinations
  3. Amazon S3 User Guide: Configuring a bucket lifecycle configuration to delete incomplete multipart uploads
  4. Amazon CloudFront Developer Guide: Serve private content with signed URLs and signed cookies
  5. Amazon CloudFront Developer Guide: Use signed URLs
  6. Amazon CloudFront Developer Guide: What is Amazon CloudFront?
  7. Amazon SQS Developer Guide: Amazon SQS queue types
  8. Arkadiusz Chmura: How Twitter handled personalized timelines for their users (on Designing Data-Intensive Applications)
Sign in to report a mistake