Design Instagram
DifficultyHard | HelloInterview: problem breakdown
Problem Statement
Design a photo-sharing social network where users can post photos/videos, follow other users, and see a personalized feed of posts from people they follow.
๐Real-world: Instagram is the legendary "tiny team, massive scale" story โ at acquisition by Facebook for $1B in 2012 it had ~30M+ users and just 13 employees, running on AWS with a famously boring, reliable stack: PostgreSQL + Redis + Memcached, sharded across instances, with photos on S3 behind a CDN. They scaled the feed with fan-out-on-write (precompute each user's timeline) but had to special-case celebrities โ fanning a Kardashian post to 300M followers on write is infeasible, so high-follower accounts use fan-out-on-read (the "hybrid" approach this design hinges on). Security angle worth raising: at this scale the social graph and media moderation (CSAM detection, abuse) become core infrastructure, not afterthoughts โ content scanning and trust-and-safety pipelines run on every upload.
Requirements
Functional
- Upload photos/videos
- Follow/unfollow users
- Personalized feed (posts from followed accounts, ranked)
- Like and comment on posts
- Search users and hashtags
- Stories (ephemeral 24h content)
Non-Functional
- 1B users, 500M DAU
- 100M photos uploaded/day
- 500M feed reads/day โ 6k reads/s average; 100k peak
- Photo upload < 5s for standard connection
- Feed load < 500ms P99
Core Design
Instagram is essentially a combination of three problems: media storage (YouTube-lite), social graph (Twitter follower graph), and news feed (Facebook news feed).
Media Pipeline
Upload: chunked โ S3 (raw)
Processing: async queue โ image resize worker (thumbnail, 640px, 1080px) โ S3
CDN: CloudFront/Akamai in front of S3Each photo stored in 3 resolutions + thumbnail. CDN serves most traffic; S3 is origin.
Social Graph
follows(follower_id, followee_id, created_at)
PRIMARY KEY (follower_id, followee_id)
INDEX (followee_id) -- for "who follows me"At scale (Selena Gomez has 400M followers), this table is enormous. Use a graph DB (Neo4j) or a distributed KV store (Cassandra) partitioned by follower_id for "my following list" queries.
Feed โ Hybrid Fan-Out
Same hybrid approach as Facebook news feed:
(< 1M followers): fan-out on write โ write to each follower's feed cache on post
(> 1M followers): fan-out on read โ fetch at read time and merge with pre-computed feed
Feed cache (Redis per user):
feed:{user_id} โ sorted set: score=rank, member=post_id
At feed load:
1. Read pre-computed post IDs from Redis
2. Fetch celebrity posts directly (last 50 from each followed celebrity)
3. Merge + rank (recency + engagement)
4. Hydrate post details (batch fetch from Posts DB)Stories
Ephemeral โ auto-delete after 24h:
stories(story_id, user_id, media_url, created_at, expires_at)
INDEX expires_at -- background job deletes expired stories
Seen tracking: Redis SET seen_stories:{viewer_id} โ {story_id} (TTL 24h)Architecture
Upload Service โ S3 โ Transcoding queue โ Media processor โ S3 (resized) โ CDN
Post Service โ Cassandra (posts) โ Fan-out worker โ Feed cache (Redis)
Feed Service โ Redis + celebrity post fetch โ Rank โ Hydrate โ Response
Social Graph โ Cassandra (follows) + Redis (hot follow counts)
Search โ Elasticsearch (users, hashtags)
Notification โ Push (APNs/FCM) / In-appSecurity Considerations
| Threat | Mitigation |
|---|---|
| CSAM / illegal content | PhotoDNA hash matching on upload; ML classifier |
| Account takeover | 2FA; login anomaly detection; device trust |
| Stalking / location data in EXIF | Strip EXIF metadata on upload before storing |
| Private account scraping | Enforce follow approval; auth check on every post/story fetch |
| DM exfiltration | Rate limit export; require 2FA for account data download |
Interview Tips
Instagram is massive; agree on which features to focus on.
emphasize async transcoding; CDN is the primary cost and scaling lever.
is a quick security mention that resonates โ location metadata leakage from photos is a real privacy issue.
same as news feed, but frame it in Instagram terms (celebrities vs regular users).