Build a Social App for Sharing Pictures and Short Clips
System DesignModel answerLast updated
Design a social platform where people post photos and short videos, follow each other, and browse a personalized home feed, with attention to media delivery, feed fan-out and trade-offs.
Design the backend for a social app centered on visual content. People publish photos and short videos with a caption, follow other accounts, and open a home feed that lists recent posts from the accounts they follow. Posts can be liked and commented on, and every account has a profile page listing its own posts.
Be prepared to defend each choice. Expect questions such as: why this data store and not another, what fails first as traffic grows, how latency, cost and consistency pull against each other, and what you would change if usage patterns shifted. Budget your time so the core design fits comfortably in less than a full session.
Assumptions and rules
- The workload is read-heavy: feeds are opened far more often than posts are created.
- No traffic numbers are provided. State your own estimates and use them to drive decisions.
- Ephemeral stories, private messaging, search and advertising are out of scope.
Questions you may want to settle up front
- Is the feed ordered by time or ranked?
- Are videos included, and how long may they be?
- How soon after posting should followers see the new item?
- What is the maximum follower count for one account?
- Is the audience global, requiring multiple regions?
What to cover
- Scope, requirements and back-of-envelope estimates.
- The upload path and how media is stored and delivered.
- Feed generation for ordinary accounts and for accounts with huge audiences.
- Data model and storage choices, including the follow graph.
- Caching and consistency choices per feature.
- Failure handling, scaling to several regions, and the main trade-offs.
Hints
Hint 1
Start from estimates: posts per day, feed opens per day, and average media size. The ratio of reads to writes should justify most of your later choices.
Hint 2
Keep large files away from your API servers: think about direct-to-object-storage uploads with signed URLs, asynchronous transcoding, and CDN delivery.
Hint 3
Compare fan-out on write with fan-out on read, then ask what happens when one account has tens of millions of followers. A hybrid approach is the usual answer.
Hint 4
Decide consistency per feature: an author should see their own post immediately, while follower feeds and like counts can lag by seconds.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Requirements and estimates
Functional: upload photo/video with caption, follow/unfollow, home feed, like, comment, profile grid.
Non-functional: feed load p99 under ~300 ms (metadata only; media comes from a CDN), high availability for reads, new posts visible to followers within a few seconds, durable media (never lose an upload).
Assumed scale (state these out loud):
- 500M monthly users, 100M daily active.
- 10M new posts/day, which is about 120 writes/s average and roughly 500/s at peak.
- Each daily user opens the feed ~10 times, giving ~1B feed requests/day, about 12K/s average and ~40K/s at peak.
- Average stored media after processing is ~2 MB per photo, ~10 MB per short video across renditions. At roughly 10M posts/day this is about 20-40 TB/day, so several PB per year. Object storage and a CDN are mandatory; the database holds only metadata.
The read:write ratio is around 100:1, so we optimize for reads and accept extra work at write time.
2. Media path
- Client asks the API for an upload slot. The API returns a pre-signed URL to object storage, so bytes never pass through application servers.
- Client uploads (multipart/resumable for video). A storage event triggers an asynchronous pipeline via a queue.
- Workers validate, strip metadata, generate thumbnails and several resolutions for photos, and transcode video into adaptive-bitrate segments (HLS/DASH).
- When processing completes, the post row is marked
readyand the fan-out job is enqueued. - Delivery is through a CDN with long cache TTLs and immutable, content-addressed URLs. Hot media gets a very high hit rate; origin shielding protects object storage.
Trade-off: asynchronous processing delays visibility by seconds, but it keeps the upload API fast and lets us retry failures independently.
3. Data model and stores
- Users, posts, comments: sharded relational or wide-column store keyed by
user_id(posts keyed by(author_id, created_at/post_id)so profile pages are a single range scan). Post IDs are time-sortable (Snowflake-style) so ordering needs no extra index. - Follow graph: two access patterns: "who does X follow" and "who follows X". Store both directions, sharded by the first key (
follows_by_follower,follows_by_followee). A wide-column store or sharded SQL works; a graph database is unnecessary because we only need one-hop lookups. - Likes: write-heavy. Record
(post_id, user_id)in a key-value store for "did I like this" checks, and keep an aggregated counter updated asynchronously (batched increments in Redis, periodically flushed). - Feed cache: Redis sorted sets, one per user, holding recent post IDs (say the latest 500-1000), scored by post ID/time.
- Media: object storage; the DB stores keys and metadata only.
4. Feed generation
Fan-out on write (push): when a post becomes ready, a worker reads the author's followers and inserts the post ID into each follower's feed list. Reads are then one cache fetch plus hydration of post objects. Cost: write amplification equal to follower count.
Fan-out on read (pull): at read time, fetch the latest posts from each followed account and merge. Cheap writes, but expensive and slow reads, especially for users who follow many accounts.
Hybrid (recommended):
- Push for ordinary accounts (say under ~100K followers).
- For celebrity accounts, skip push. At read time, merge the user's precomputed feed with recent posts from the few celebrities they follow (kept in a hot cache).
- Skip pushing to inactive users; rebuild their feed lazily on next login.
A 10M-follower account posting with pure push would mean 10M writes per post, causing lag and hot shards. The hybrid caps that.
Feed read flow: fetch the ID list from Redis, merge in celebrity posts, batch-get post/author metadata (cached), attach like counts, and return a page with a cursor (last post ID, not offset) for stable pagination. If ranking is added, a lightweight ranker reorders the top few hundred candidates; start chronological to keep things simple.
5. Consistency choices
- Author sees own post immediately: write it into their profile and feed cache synchronously (read-your-writes), even before follower fan-out finishes.
- Follower feeds: eventual consistency, seconds of lag is acceptable.
- Counts (likes, comments): approximate and eventually consistent; the exact set of likers is the source of truth.
- Follow/unfollow: apply immediately to the graph; stale feed entries from an unfollowed account are filtered at read time.
- Deletes: mark the post deleted and filter at hydration so cached IDs do not leak content; clean caches lazily.
6. Scaling, failure and trade-offs
- Caching: feed lists, post metadata and user profiles in Redis/Memcached; a CDN for media. Expect the cache tier to absorb most of the 40K/s peak reads.
- Sharding: by user ID for feeds and graph. Hot keys (celebrity profiles) get replicated read caches.
- Queues: fan-out via Kafka-style partitioned queues; failures retry with idempotent inserts (a post ID already present is a no-op). Dead-letter queues catch poison messages.
- Multi-region: media replicated or cached at regional CDNs; metadata written to a home region per user with asynchronous replication, reads served locally. Accept that a follower in another region may see a post a little later.
- What breaks first: usually the fan-out workers and hot cache shards for popular accounts, then the like-counter write path. Mitigate with the hybrid model, batching and sharded counters.
- If usage shifts: if users follow more accounts or become less active, lean more toward pull and lazy materialization. If ranking becomes central, move to candidate generation plus a ranker and store features separately.
Common mistakes
- Pushing uploads through app servers instead of signed URLs.
- Pure push with no plan for celebrities, or pure pull with no plan for latency.
- Using offset pagination on a feed that is constantly changing.
- Treating every counter as strongly consistent.
- Choosing a graph database or a single giant SQL table without tying the choice to access patterns.
- Skipping estimates, so no decision can be justified with numbers.