Full answer
Requirements
Functional:
- Upload a photo with a caption; view a profile grid.
- Follow other users; scroll a home feed of recent posts from people you follow.
- Like and comment.
Non-functional:
- Feed loads in under about 300 ms; images load quickly worldwide.
- Uploads are never lost; feeds can be a few seconds behind.
- Read-heavy: many more views than uploads.
Capacity estimates
Assumptions: 500M daily active users, 100M uploads per day, 3 MB stored per photo (2 MB original plus 1 MB of resized versions), 20 feed loads per user per day.
- Uploads: 100M / 86,400 = about 1,160 per second.
- Storage: 100M x 3 MB = 300 TB per day, about 109.5 PB per year before replication.
- Feed loads: 500M x 20 / 86,400 = about 116,000 per second; each loads metadata for about 10 posts and their image URLs.
- Image bandwidth is served by the CDN, not by origin servers.
API
POST /v1/uploads -> {"upload_url": "https://storage/...signed", "media_id": "m_91"}
PUT {upload_url} (client uploads bytes directly)
POST /v1/posts {"media_id": "m_91", "caption": "Campus sunrise"}
GET /v1/feed?cursor=.. -> [{"post_id", "author", "image_urls": {"640": ..., "1080": ...}, "likes"}]
POST /v1/posts/{id}/likes
POST /v1/users/{id}/follow
Data model
users: user_id, name, avatar.posts: post_id (time-sortable ID), author_id, media_id, caption, created_at. Shard by author_id so a profile grid is one shard read.follows: (follower_id, followee_id) with an index in both directions.likes: (post_id, user_id); a separate counter per post for display.- Images: object storage keys like
media/m_91/1080.jpg. - Feed cache: per-user list of the latest few hundred post IDs in Redis.
High-level design
flowchart LR
A[App] --> API[API servers]
A -- "upload bytes" --> S3[(Object storage)]
S3 -- "upload event" --> IMG[Resize workers]
IMG --> S3
API --> DB[(Posts and follows DB)]
API --> FC[(Feed cache)]
DB -- "new post event" --> FO[Fan-out workers]
FO --> FC
A -- "images" --> CDN[CDN]
CDN --> S3Upload and feed flows
Upload: the app asks for a pre-signed URL, uploads directly to object storage (API servers never carry the bytes), then creates the post. A storage event triggers workers that make 3 to 4 sizes and strip location metadata.
Feed: when a user posts, fan-out workers push the post ID to the feed cache of each follower (fan-out on write). Reading a feed is then one cache read plus a batch fetch of post metadata. For accounts with millions of followers, skip fan-out and merge their recent posts at read time instead (a hybrid), because writing to 50M feed lists for one post is too slow.
Bottlenecks and trade-offs
- Storage cost dominates. Move old originals to colder storage classes; keep popular sizes hot in the CDN.
- Celebrity fan-out is handled by the hybrid model.
- Like counts on viral posts: increment in a cache and flush to the database in batches, accepting a few seconds of lag.
- Fan-out on write makes reads fast but uses more storage and work for inactive users; only fan out to users active in the last few weeks.
- Chronological versus ranked feeds: ranking adds a scoring service on the read path, which costs latency but improves engagement.
By level
Fresher: Explain that photos go to object storage and a CDN while post details go to a database, and sketch the upload and feed APIs. A fresher should describe a simple way to build the feed from followed users.
Mid: Do the capacity math, design fan-out on write with a feed cache, and add background resizing. Explain the celebrity problem and the hybrid fix.
Senior: Discuss storage tiering and cost, ranking, counter scaling, deletion and privacy across caches, and fan-out only to active users. Expect numbers to guide each choice.
Key points
- Upload directly to object storage with pre-signed URLs
- Resize images asynchronously and serve through a CDN
- Shard posts by author; keep a per-user feed cache
- Fan-out on write, with read-time merge for celebrities
- Storage, about 300 TB per day, is the largest cost
Common mistakes
- Sending image bytes through API servers
- Storing images in the database
- Fanning out a celebrity's post to every follower
- Updating like counts with a database write per like on viral posts
Follow-ups
- How would you add Stories that expire after 24 hours?
- How would you rank the feed?
- How would you delete a user's photos everywhere, including the CDN?
Tips
- Separate media from metadata at the start
- Do the storage math; it is striking
- Explain the hybrid fan-out
Evaluation rubric
- Correct media path with object storage and CDN
- Capacity math for uploads, storage and reads
- Feed design with fan-out trade-offs
- Handles celebrities and counters