🇮🇳
🇮🇳
Republic Day Special Offer!Get 20% OFF on all courses
Enroll Now
P
Prakalpana
📚Learn
Code Your Future
System Design⏱️ 25 min read📅 Jan 6

HLD: Design Instagram - Photo Sharing at Scale

VP
Vikram PatelStaff Engineer at Meta
📑 Contents (16 sections)

📌Problem Statement

Design a photo-sharing social network like Instagram.

📌Requirements

Functional

  • Upload photos/videos
  • Follow users
  • News feed (posts from followed users)
  • Like and comment
  • Stories (24-hour content)
  • Direct messaging
  • Non-Functional

  • 500M daily active users
  • 100M photos uploaded daily
  • Feed generation < 500ms
  • 99.99% availability
  • Capacity Estimation

  • 500M DAU, 100M uploads/day
  • Average photo size: 500KB
  • Daily storage: 100M x 500KB = 50TB/day
  • 5 years: 90PB storage
  • 📌High-Level Architecture

    System Architecture
    Live
    Mobile/Web App
    Load Balancer
    Nginx
    API Gateway
    User Service
    Post Service
    Feed Service
    User DB
    Postgres
    Post DB + S3
    Feed Cache
    Redis
    CDN
    CloudFront
    ⚡ High Availability🔄 Auto-scaling🛡️ Fault Tolerant

    📌Component Design

    1. Photo Upload Flow

    public class PhotoUploadService {
    public UploadResponse upload(MultipartFile file, String userId) {
    String photoId = generateId();
    String s3Key = userId + "/" + photoId;
    s3Client.putObject(BUCKET, s3Key, file.getInputStream());
    List<String> thumbnails = imageProcessor.generateThumbnails(
    file, Arrays.asList(150, 320, 640, 1080)
    );
    Post post = new Post(photoId, userId, s3Key, thumbnails);
    postRepository.save(post);
    fanoutService.publishToFollowers(userId, post);
    return new UploadResponse(post.getId(), getCdnUrl(s3Key));
    }
    }

    2. News Feed Generation

    Two approaches:

    Push Model (Fan-out on Write)

  • When user posts, push to all followers' feeds
  • Good for users with few followers
  • Problem: Celebrities with millions of followers
  • Pull Model (Fan-out on Read)

  • When user opens app, fetch posts from followed users
  • Good for celebrities
  • Problem: Slow for users following many people
  • Hybrid Approach (Best)

  • Push for normal users
  • Pull for celebrities (>10K followers)
  • public class FeedService {
    private static final int CELEBRITY_THRESHOLD = 10000;
    public List<Post> getFeed(String userId, int limit) {
    List<Post> feed = new ArrayList<>();
    feed.addAll(feedCache.get(userId));
    List<String> celebrities = followService.getCelebritiesFollowed(userId);
    for (String celeb : celebrities) {
    feed.addAll(postService.getRecentPosts(celeb, 10));
    }
    feed.sort(Comparator.comparing(Post::getCreatedAt).reversed());
    return feed.stream().limit(limit).collect(Collectors.toList());
    }
    public void fanout(String userId, Post post) {
    int followerCount = followService.getFollowerCount(userId);
    if (followerCount < CELEBRITY_THRESHOLD) {
    List<String> followers = followService.getFollowers(userId);
    for (String followerId : followers) {
    feedCache.prepend(followerId, post);
    }
    }
    }
    }

    3. Database Design

    Users Table

    CREATE TABLE users (
    user_id BIGINT PRIMARY KEY,
    username VARCHAR(50) UNIQUE,
    email VARCHAR(100),
    profile_pic_url TEXT,
    bio TEXT,
    created_at TIMESTAMP
    );

    Posts Table (Cassandra for scale)

    CREATE TABLE posts (
    post_id UUID,
    user_id BIGINT,
    media_url TEXT,
    caption TEXT,
    location TEXT,
    created_at TIMESTAMP,
    PRIMARY KEY (user_id, created_at)
    ) WITH CLUSTERING ORDER BY (created_at DESC);

    Follows Table

    CREATE TABLE follows (
    follower_id BIGINT,
    followee_id BIGINT,
    created_at TIMESTAMP,
    PRIMARY KEY (follower_id, followee_id)
    );

    4. CDN and Image Serving

  • Store originals in S3
  • Generate thumbnails on upload
  • Serve via CloudFront CDN
  • Cache headers: max-age=31536000
  • 📌Scaling Strategies

    Database Sharding

  • Shard posts by user_id
  • Shard feeds by user_id
  • Consistent hashing for distribution
  • Caching Layers

  • 1CDN: Static content (images)
  • 2Redis: User feeds, session
  • 3Memcached: Hot posts, user profiles
  • Message Queue

  • Kafka for async processing
  • Fanout jobs
  • Notification delivery
  • Analytics events
  • 📌Interview Questions

    Q: How to handle celebrity posts? A: Pull model for celebrities, don't fan out

    Q: How to rank feed? A: ML model considering recency, engagement, user interests

    Q: How to handle stories? A: Separate service with TTL, store in Redis with 24h expiry

    This is asked at Facebook, Google, and Instagram interviews.

    VP

    Written by

    Vikram Patel

    Staff Engineer at Meta

    🚀 Master System Design

    Join 500+ developers

    Explore Courses →
    Chat on WhatsApp