Design Instagram (Photo Sharing & Activity Feed)
Design Instagram (Photo Sharing & Activity Feed)
Instagram handles billions of uploaded photos, real-time activity notifications, and personalized timeline generation for over 1 billion monthly active users.
1. System Requirements
Functional Requirements
- Post Photos & Videos: Users can upload high-resolution photos and video clips with captions and tags.
- Follow Social Graph: Users can follow/unfollow other accounts.
- News Feed Generation: Users can view an infinite scroll timeline of recent posts from accounts they follow.
- Like & Comment: Real-time interactions on posts with instantaneous count updates.
- Explore & Search: Discover trending posts based on user engagement signals.
Non-Functional Requirements
- Low Feed Latency: Newsfeed page loads in $< 200\text{ms}$.
- High Availability: 99.99% availability; read traffic vastly outnumbers writes (100:1 ratio).
- Media Durability: Zero data loss for uploaded images and videos.
- Eventual Consistency: A new post does not need to show up on all followers' screens simultaneously, but should appear within seconds.
2. Capacity & Scale Estimation
- Daily Active Users (DAU): $500\text{ Million}$
- New Posts Uploaded per Day: $100\text{ Million}$
- Average Photo Size: 2 MB (compressed to multiple WebP resolutions).
- Daily Media Storage: $100\text{M} \times 2\text{ MB} = 200\text{ TB/day} \rightarrow 73\text{ PB/year}$.
- Average Read QPS: $500\text{M} \times 20\text{ feed views / 86400} \approx 115,000\text{ QPS}$.
3. High-Level Architecture Blueprint
+-----------------------+
| Mobile Client |
+-----------+-----------+
|
v
+-----------------------+
| Cloudflare Edge CDN |
+-----------+-----------+
|
v
+-----------------------+
| API Gateway |
+-----+-----------+-----+
| |
+--------------------------+ +--------------------------+
| |
v v
+------------------------+ +------------------------+
| Media Upload Svc | | Feed Generation Svc |
| (Presigned URLs to S3) | | (Hybrid Fanout Engine) |
+-----------+------------+ +-----------+------------+
| |
v v
+------------------------+ +------------------------+
| Transcoding Pipeline | | Redis Timeline Cache |
| (FFmpeg Worker Cluster)| | (ZSET of Post IDs) |
+------------------------+ +------------------------+4. Feed Generation: Push vs. Pull vs. Hybrid Fanout
Push Model (Fanout-on-Write)
- When User A posts, the system looks up all followers and inserts the
post_idinto every follower's Redis timeline. - Pros: Feed read is instantaneous ($O(1)$ lookup in Redis).
- Cons (The Celebrity Problem): If Cristiano Ronaldo (600M followers) posts, writing 600,000,000 rows melts the message queue.
Pull Model (Fanout-on-Read)
- When a user opens the app, the system queries their follow list, fetches recent posts from all followed creators, and merges them in memory.
- Pros: No write amplification.
- Cons: Feed generation is slow and expensive at read time.
The Hybrid Solution (Production Standard)
- Regular Users ($< 25,000$ followers): Use Fanout-on-Write. Posts are pushed into followers' Redis caches immediately.
- Celebrities / High-Follower Accounts: Use Fanout-on-Read. Their posts are not fanned out. When a follower loads their feed, the system queries the celebrity's recent posts and merges them into the timeline dynamically.
Key Architecture Trade-Offs & Decision Matrix
| Decision Factor | Choice A | Choice B | Selected Architectural Rationale |
|---|---|---|---|
| Communication Protocol | REST over HTTP/2 | WebSockets / gRPC | Use REST for stateless CRUD APIs; use WebSockets for low-latency bidirectional streaming. |
| State Management | Centralized Distributed Store | Local Memory with Sticky Sessions | Centralized Redis / Database state allows seamless container auto-scaling and zero-downtime rolling deploys. |
| Concurrency Control | Optimistic Locking (version column) | Pessimistic Locking (SELECT FOR UPDATE) | Optimistic locking delivers superior throughput for low-conflict consumer operations. |
Related Topics
Expand your knowledge by learning about adjacent concepts in system design.
Design a Distributed Caching System (Redis Architecture)
Comprehensive architecture guide for designing production-grade Redis distributed caching clusters: master-replica replication, Redis Sentinel, Redis Cluster sharding, and memory eviction.
Design a URL Shortener (TinyURL / Bitly Architecture)
Design a scalable URL shortening service capable of handling billions of redirects: Base62 encoding, distributed ID generation, database indexing, and 301 vs 302 redirect trade-offs.
Scaling Reads (Replication, CQRS & Multi-Level Caching)
Techniques for scaling read-heavy systems: Read Replicas, Multi-Tier Caching (L1/L2), Command Query Responsibility Segregation (CQRS), and Edge CDNs.
Cheat Sheet Utility
View and print a concise system design reference card.