Katkat

Design Instagram (Feed, Photos, and Friend Recommendations)

AnthropicSoftware Engineer · Onsite

Last updated

Design a scalable social media platform supporting photo uploads, timeline feed generation, follow graphs, and friend recommendations. This problem tests distributed storage, feed fan-out strategies under skewed follower distributions, and graph-based recommendation systems.

Scenario

Design the core backend architecture for a photo-sharing platform similar to Instagram. Users should be able to upload photos, follow other users, view a personalized home feed of recent posts from people they follow, and discover new connections via a friend/account recommendation feature.

Requirements

Functional Requirements

  • Post Creation: Users can upload photos with captions.
  • Social Graph: Users can follow and unfollow other users.
  • Home Feed: Users can view a reverse-chronological or ranked feed containing posts from followed users.
  • Recommendations: Users receive suggestions for accounts to follow ("People you may know").

Non-Functional Requirements

  • High Availability: Feed generation and photo viewing should prioritize availability over strict consistency (eventual consistency is acceptable).
  • Low Latency: Home feed retrieval p99 under 200 ms; photo uploads should feel responsive.
  • Scalability: System must handle celebrity accounts with tens of millions of followers without cascading delays.

Scale & Traffic Assumptions

  • Daily Active Users (DAU): 500 million.
  • Posts per Day: 100 million photos uploaded daily (~1,150 uploads/sec average, ~3,000 peak).
  • Feed Reads: 5 billion feed requests daily (~58,000 reads/sec average, ~150,000 peak).
  • Average Media Size: 2 MB per photo (with thumbnails and compressed variants).

What Your Design Should Cover

  1. API Design: Core endpoints for uploading media, following accounts, fetching the feed, and getting recommendations.
  2. Data Model & Storage: Relational vs. NoSQL choices for post metadata, social graph representation, and media object storage (e.g., S3 + CDN).
  3. Feed Architecture: Detailed comparison and implementation of Fan-out-on-Write (push) vs. Fan-out-on-Read (pull), including a hybrid approach to mitigate the "celebrity problem".
  4. Recommendation Engine: Offline and nearline architectures for generating friend recommendations based on mutual connections and engagement graphs.
  5. Caching & Scalability: Cache hierarchy (CDN, Redis for timelines), partitioning strategies, and handling read-heavy skew.

Hints

Hint 1

Consider a hybrid fan-out model where normal user posts are pushed to follower inboxes, but posts from users with high follower counts are fetched on read.

Hint 2

Store the immutable media blobs in an object store behind an edge CDN, keeping only media metadata and URLs in your transactional database.

Hint 3

For friend recommendations, precompute graph-based metrics like friends-of-friends or mutual followers via periodic offline batch jobs rather than running expensive graph traversals on live requests.

Follow-up questions

  • How would your feed ingestion and caching strategy adapt if the feed changes from pure reverse-chronological order to an ML-ranked relevance score?
  • How do you handle privacy changes, such as a user switching their account from public to private, or deleting a post with millions of cached impressions?
  • How would you optimize storage costs for media as posts age and their access frequency drops exponentially after 48 hours?