MEDIUMSocial & Mediasenior engineer

Design Instagram

Design a photo-sharing social network like Instagram with features like posting photos, following users, and a personalized feed.

Estimated Time: 45 minutes
GoogleMetaTwitter/X
#social-network#feed#photo-sharing#cdn#fan-out

Solution Overview

Use CDN for media storage, fan-out on write for feed generation, and separate read/write paths for scalability.

Solution Overview

Use CDN for media storage, fan-out on write for feed generation, and separate read/write paths for scalability.

Hints to Get Started
1

Think about the celebrity/follower problem

2

Consider eventual consistency for feeds

3

How would you handle photo processing?

Requirements

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
Capacity Estimation

storage

2M × 2MB average = 4TB/day = 1.4PB/year

photos per day

2M new photos

daily active users

200M DAU

High-Level Architecture

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
Deep Dives

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

Feed Generation

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