Design Instagram
Design a photo-sharing social network like Instagram with features like posting photos, following users, and a personalized feed.
Use CDN for media storage, fan-out on write for feed generation, and separate read/write paths for scalability.
Think about the celebrity/follower problem
Consider eventual consistency for feeds
How would you handle photo processing?
functional
- •Upload/view photos
- •Follow users
- •News feed
- •Like/comment
- •Search users/tags
non functional
- •High availability
- •Low latency feed
- •Consistency can be eventual
- •Scale to 500M users
storage
2M × 2MB average = 4TB/day = 1.4PB/year
photos per day
2M new photos
daily active users
200M DAU
storage
feeds
Redis for timeline cache
photos
Object Storage (S3) + CDN
search
Elasticsearch
metadata
Sharded MySQL/PostgreSQL
components
- •CDN
- •Load Balancer
- •Photo Upload Service
- •Feed Generation Service
- •User Service
- •Notification Service
feed ranking
ML model considering recency, engagement, relationship strength
photo upload
Client uploads to S3 directly with pre-signed URL, triggers async processing
celebrity problem
Lazy loading for users with millions of followers
hybrid
Fan-out on write for regular users, on-read for celebrities
fan out on read
cons
- •Slow reads
- •Complex read path
pros
- •Fast writes
- •No wasted computation
description
Compute feed at read time
fan out on write
cons
- •Slow writes for celebrities
- •Storage overhead
pros
- •Fast feed reads
- •Simple read path
description
Pre-compute feeds when user posts