Build Instagram
Media pipeline plus a social graph. Where read:write asymmetry starts dictating architecture and the feed becomes the hard problem.
#The product
Post a photo. Follow people. See a feed of photos from people you follow. Like and comment.
Instagram is the image host plus a social graph — and the graph is where all the difficulty moves.
#Estimation
500M DAU · 0.2 posts/user/day · 50 feed views/user/day
Writes: 500M × 0.2 / 10⁵ = 1,000 posts/s → 3,000 peak
Reads: 500M × 50 / 10⁵ = 250,000 QPS → 750,000 peak
Ratio: 250:1 ← the number that shapes everything
Media: 1M photos/day × 1.6 MB (incl. derivatives) = 160 TB/day
Metadata: 1M × 500 B = 500 MB/day → tiny
FANOUT: 1,000 posts/s × 200 avg followers = 200,000 feed writes/s
one 100M-follower account = 100M writes from ONE postA 250:1 read ratio means: move work onto the write path. The expensive thing should happen once at write time, not 250 times at read time. That single conclusion drives the feed design.
#Phase 1 — Posts without a feed
POST /posts → presigned upload → object storage
→ metadata row → Postgres
→ async workers → derivatives
GET /users/{id}/posts → query by user, newest firstProfile pages work. No feed yet. This ships in a week and is genuinely useful.
Schema, sharded on user_id:
posts (user_id, post_id, s3_key, caption, created_at)
follows (follower_id, followee_id, created_at)
likes (post_id, user_id, created_at)Shard posts on user_id, for three reasons. It's high-cardinality — there are hundreds of millions of distinct values, so data spreads evenly across shards. The profile query already filters by it, so the router knows which shard to ask. And all of one user's posts land on the same shard, so a profile page is a single-shard read. See Sharding.
Index follows in both directions. You need "who do I follow" for reads and "who follows me" for fanout. Two indexes, or two tables — either way, both directions.
#Phase 2 — The feed, naively
The obvious implementation: at read time, fetch recent posts from everyone you follow and merge.
GET /feed
→ SELECT followee_id FROM follows WHERE follower_id = me (200 rows)
→ SELECT * FROM posts WHERE user_id IN (...) ORDER BY created_at LIMIT 20Why this dies: the 200 accounts you follow live on many different shards, so one feed load sends a query to each of those shards and waits for all of them to answer (a scatter-gather). That happens 750,000 times per second at peak. Two things go wrong:
- You wait for the slowest shard. If each shard is slow only 1% of the time, a request that touches many shards hits at least one slow one most of the time (tail amplification).
- Cost grows with how many people you follow, not with what you show. You only display 20 posts, but you paid to query 200 accounts to find them.
You've put the expensive work on the frequent operation. Exactly backwards for a 250:1 ratio.
#Phase 3 — Precompute the feed (fanout on write)
Flip it. When someone posts, immediately write the post ID into every follower's precomputed feed.
Alice posts
→ publish event to Kafka
→ fanout worker: look up Alice's followers
→ for each: ZADD feed:{follower_id} {timestamp} {post_id}
GET /feed → ZREVRANGE feed:{me} 0 19 ← one Redis call, ~1msReads become a single sorted-set read. The 250:1 ratio now works for you.
Trim aggressively. Nobody scrolls past a few hundred items:
Keep 800 post IDs per feed.
Active users only (opened app in 30 days): say 100M
100M × 800 × 8 B = 640 GB → ~20 Redis nodes
Materializing feeds for all 500M users would be 3.2 TB
for feeds most of which are never read.Fanout is async via Kafka. The user's post request returns in ~50 ms; fanning out to 200 followers takes longer and they don't need it to have finished.
#Phase 4 — The celebrity problem
The trigger: an account with 100M followers posts. That's 100M Redis writes from a single action — minutes of work, and it starves every other fanout.
ON POST:
followers < 10,000 → PUSH (fan out)
followers ≥ 10,000 → don't fan out; mark as celebrity
ON READ:
1. read precomputed feed (push portion)
2. fetch recent posts from the few celebrities you follow
3. merge by timestamp, return top 20This works because follower counts follow a power law — a tiny number of accounts have enormous followings, and almost everyone else has very few. The overwhelming majority of accounts are far below any reasonable threshold, so nearly every post is pushed and reads stay cheap. And you follow only a handful of celebrities, so the pull portion is small and bounded.
Two refinements worth naming:
- The threshold must be evaluated at post time against current follower count, and it needs hysteresis — different thresholds for going up and coming down. Promote an account to celebrity at 10K followers, but only demote it back below 8K. With a single number, an account going 9,999 → 10,001 → 9,998 would switch strategies on every post, and its posts would end up partly pushed and partly pulled.
- Celebrity posts are read by everyone, so they cache at close to a 100% hit rate. Millions of readers pull the same few posts, so after the first read each one is served straight from cache. The pull path is cheap precisely because the demand is concentrated on so few posts.
#Phase 5 — Hydration and counts
The feed cache stores only a list of post IDs — cheap to build and cheap to store. But a screen needs the photo URL, the author's name and avatar, the like and comment counts, and whether you liked it. Turning that list of IDs into fully-formed posts is called hydration, and it happens on every feed load.
20 post IDs → batched multi-get across:
· post metadata service
· user/profile service (cached hard — profiles change rarely)
· counts service
· "did I like this" — per-viewer, can't be sharedBatch everything. Twenty sequential lookups would be 20 round trips; one batched call is one.
Counts must never be a synchronous row update. A viral post would make one row take 50,000 writes/s. Instead: publish like events to Kafka, and have a stream processor add them up in batches (say, "+347 likes for post X in the last 5 seconds"). It writes those running totals (rollups) to storage once per batch, and you serve the slightly-behind approximate count. Users cannot tell the difference between 1,203,847 and "1.2M" — and the product should display the rounded version anyway, which makes the approximation invisible by design.
#The decision log
| Decision | Rejected | Because |
|---|---|---|
| Hybrid fanout (push + pull) | Pure push | One celebrity post = 100M writes |
| Pure pull | 200-way scatter-gather on the most frequent operation | |
| Materialize feeds for active users only | All users | 3.2 TB of feeds mostly never read |
| Trim feeds to ~800 items | Unbounded | Nobody scrolls that far; memory is the constraint |
| Async fanout via Kafka | Synchronous | Post must return in ~50ms; fanout takes longer |
| Counts via stream aggregation | UPDATE ... SET count = count + 1 | Viral post = one hot row at 50K writes/s |
Shard posts on user_id | Shard on post_id | Profile page becomes a single-shard read |
| Filter deletes at read time | Scrub every feed cache | Removing one post from 100M cached feeds is the fanout problem again; instead, hydration skips post IDs that no longer exist |
#What you'd cut for v1
Stories, reels, DMs, explore, search, ads. Ship: upload, follow, chronological feed, likes. And ship pull-only fanout first — it's simpler, has no celebrity problem, and lets you measure the real follower distribution before committing to a threshold you'd otherwise be guessing at.
#Explaining the design
"Read:write is about 250:1, so I want the expensive work on the write path. I'd precompute feeds — on post, fan out the post ID into each follower's Redis sorted set, trimmed to 800 and only for users active in the last 30 days. That makes a feed read one cache call.
The obvious problem is celebrity fanout: a 100M-follower account would generate 100M writes from one post. So it's a hybrid — push below about 10,000 followers, pull above, merged at read time. Since follower counts are power-law distributed, almost everything is pushed and reads stay cheap.
I'd ship pull-only first and add push once we've measured the actual distribution."
#Next
Build Twitter — same fanout problem, but with real-time delivery and a much harder ranking layer.
#Check yourself
What number shapes Instagram's architecture?Show answer
What ships in Phase 1, and how is it sharded?Show answer
Why does the naive pull feed die?Show answer
How does fanout-on-write make reads cheap?Show answer
What refinements does the celebrity threshold need?Show answer
What is hydration, and how is it kept fast?Show answer
Why ship pull-only first?Show answer
#What to carry forward
- Let the read:write ratio decide where work happens.
- Shard posts by user; index follows both ways.
- Precompute feeds for active users, trimmed; fan out asynchronously.
- Hybrid push/pull with a hysteresis threshold handles celebrities.
- Batch hydration and serve counts from stream aggregation.