Design Instagram (and how TikTok differs)
Upload photos and videos, follow people and scroll a feed that loads instantly. The classic problem is building the feed: compute it when people open the app, or when someone posts? Plus how TikTok's interest-based feed changes the design.
Builds on these concepts
In one sentence
Instagram's feed is mostly precomputed on write (fan-out to followers), with celebrities merged on read; TikTok's For You feed is recommendation-first: candidates come from what you might like, not from who you follow.
A reference design: it covers what an app like this needs and follows how these companies have described their systems publicly, simplified to be learnable.
Requirements
Functional
- Post photos and videos
- Follow accounts
- Home feed, likes, comments
- Explore / Reels recommendations
Non-functional
- Feed loads in ~200 ms
- Read-heavy: feed reads ≫ posts
- Media is most of the storage and traffic
- Eventual consistency is fine (a post can appear a few seconds later)
Back-of-the-envelope estimates
| Metric | Estimate |
|---|---|
| Daily users | 500M, opening the feed ~10 times → ~60K feed reads/s |
| New posts | ~100M / day ≈ 1,200 posts/s |
| Fan-out | avg 200 followers → 240K feed inserts/s |
| Media | 100M × ~2 MB (all sizes) ≈ 200 TB / day |
API and data model
POST /posts { caption, mediaCount } → { postId, uploadUrls }
GET /feed?cursor=… → [ post ]
POST /posts/:id/like
POST /users/:id/follow
users(id, …) posts(id, author_id, caption, media, created_at)
follows(follower_id, followee_id) -- both directions indexed
feed:{user_id} → [post_id, …] -- Redis list, precomputedArchitecture
Step by step · Post a photo
- 01Shares a photo
- 02POST /posts
- 03Creates the post and an upload URL
- 04Original uploaded
- 05Resized to 3 sizes + thumbnail
- 06Post row saved
- 07Post id pushed to each follower's feed
- 08LPUSH feed:{follower} postId (trimmed to 500)
At this scale · 1M users
Precompute feeds (fan-out on write)
- ▸When someone posts, workers push the post id into each follower's feed list
- ▸Opening the feed is reading one list from Redis
- ▸An ML model re-ranks the feed by interest, not only by time
Tap any box to see what it does.
Deep dive: fan-out on write vs on read
| Fan-out on read (pull) | Fan-out on write (push) | |
|---|---|---|
| When the work happens | When you open the feed | When someone posts |
| Feed read cost | Query all followees' recent posts and merge: slow | Read one precomputed list: fast |
| Post cost | Cheap | One write per follower: 300M for a celebrity |
| Good for | Small apps, celebrities | Normal accounts in a read-heavy app |
Real systems are hybrid: fan-out on write for normal users, and for accounts with millions of followers, followers pull those posts at read time and merge them into the precomputed list. The merged result goes through a ranking model.
How TikTok differs
| Instagram home feed | TikTok For You | |
|---|---|---|
| Where posts come from | Accounts you follow (social graph) | Any creator: what the model thinks you'll watch (interest graph) |
| Main mechanism | Fan-out + ranking | Candidate retrieval + ranking, retrained continuously on your watch time |
| Key signal | Who you interact with | Watch time, rewatches, skips in the first seconds |
| New users | Empty until they follow people | Useful from the first swipe (cold start with popular content) |
| Media | Photos and videos | Short videos only: aggressive prefetching of the next ones |
In a TikTok-style design there's no fan-out: a candidate generation step pulls a few thousand videos from many sources (similar to what you watched, trending in your region, new creators), a ranking model scores them, and the app prefetches the next videos so swiping is instant. Instagram's Reels and Explore work the same way.
How it changes with scale
Step by step · Open the feed
- 01Opens the app
- 02SELECT posts of people I follow ORDER BY time LIMIT 20
- 03Join of follows and posts (slow with many follows)
- 04Feed in ~200 ms
At this scale · 100 users
Feed = a SQL query
- ▸Users, posts and follows in PostgreSQL; photos in S3
- ▸Feed built on read: posts from people you follow, newest first
Tap any box to see what it does.
| Users | What you run | Why it changes |
|---|---|---|
| 100 | Monolith + PostgreSQL + S3, feed by SQL query | The join is instant with few follows |
| 10K | CDN, image resizing worker, Redis cache | Media traffic and repeated reads |
| 1M | Fan-out on write into Redis feeds, ranking, services | Feed reads dominate and the query gets slow |
| 100M+ | Hybrid fan-out, sharded PostgreSQL, recommendation feeds | Celebrities, billions of rows, interest-based discovery |
Key takeaways
- 01The feed is the core: decide when to do the work, on write (push) or on read (pull).
- 02Use a hybrid: push for normal accounts, pull for celebrities, then rank.
- 03Media goes to object storage and a CDN, in several sizes.
- 04TikTok-style feeds replace the follow graph with candidate retrieval + ranking on watch behaviour.
Check your understanding
1 of 3
In a social feed like Instagram's, what does fan-out on write mean?