Skip to content
Home Interview Design Instagram: System Design Interview Deep Dive (2024)

Design Instagram: System Design Interview Deep Dive (2024)

Where developers are forged. · Structured learning · Free forever.
📍 Part of: System Design Interview → Topic 4 of 7
Design Instagram end-to-end for your system design interview.
🔥 Advanced — solid Interview foundation required
In this tutorial, you'll learn
Design Instagram end-to-end for your system design interview.
  • Separate Read and Write paths to handle lopsided traffic patterns (1:100 write-to-read ratio).
  • Use Consistent Hashing and Sharding by User_ID to manage data growth across multiple servers.
  • Implement a Hybrid Feed Model (Push for normal users, Pull for celebrities) to avoid the 'Thundering Herd' problem.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
Quick Answer

Imagine a giant post office where a billion people each send photo postcards every day. The post office has to instantly sort every postcard, deliver it only to the people who care about that sender, store the original photo safely forever, and let anyone pull up an old postcard in under a second — even at 3 AM during a major event. Instagram is exactly that post office, and designing it means figuring out every room, shelf, conveyor belt, and delivery truck needed to make it all work without ever losing a single photo.

Instagram serves over two billion monthly active users, processes roughly 100 million photo and video uploads every day, and is expected to return a personalised feed in under 300 milliseconds. When an interviewer asks you to design it, they're not looking for a diagram of boxes connected by arrows — they're watching whether you can reason about trade-offs at scale, make deliberate architectural decisions, and defend them under pressure. This is one of the most common system design questions in FAANG-level loops, and candidates who haven't internalised the nuances consistently get stuck on the feed generation problem or grossly underestimate storage requirements.

The core challenge Instagram solves is deceptively simple on the surface: store media, show it to followers, let people discover new content. Underneath, it's a collision of three genuinely hard distributed-systems problems — write-heavy media ingestion, read-heavy personalised feed delivery, and near-real-time social graph queries — all happening concurrently at planetary scale. Each of those problems demands different storage engines, caching strategies, and consistency models, and they have to coexist inside one coherent product.

By the end of this article you'll have a complete, defensible Instagram design you can present in a 45-minute interview. You'll know the exact numbers to anchor your estimates, the right database choices for each data type, how to generate feeds without melting your servers, where CDNs fit, how to shard your data, and — critically — which trade-offs to call out proactively so the interviewer knows you're thinking like an engineer who has shipped things to production, not just read about it.

Core Component Architecture: Handling 100M Uploads Daily

Designing Instagram isn't just about 'uploading a file.' It's about a decoupled architecture where the Write Path (Uploading) and the Read Path (Feed Generation) are optimized independently. On the write side, we use a Load Balancer to distribute traffic to an API Gateway, which handles authentication and rate limiting. The media itself (Photos/Videos) never touches our relational database; instead, it's streamed to an Object Store like AWS S3 or Google Cloud Storage.

We store the metadata (User ID, Photo URL, Timestamp, Location) in a distributed NoSQL database like Cassandra or a sharded PostgreSQL cluster. This separation ensures that even if our metadata database is busy, our media storage remains performant and durable. To make the images load instantly worldwide, we push them to Edge Locations via a Content Delivery Network (CDN).

io.thecodeforge.instagram.MediaService.java · JAVA
123456789101112131415161718192021222324252627282930313233
package io.thecodeforge.instagram;

import java.util.UUID;
import java.time.Instant;

/**
 * Represents the Metadata record for a high-scale media upload.
 * In production, this would be persisted to a sharded DB cluster.
 */
public class PhotoMetadata {
    private final String photoId;
    private final String userId;
    private final String s3Url;
    private final long timestamp;

    public PhotoMetadata(String userId, String s3Url) {
        this.photoId = UUID.randomUUID().toString();
        this.userId = userId;
        this.s3Url = s3Url;
        this.timestamp = Instant.now().getEpochSecond();
    }

    public void saveToDatabase() {
        // High-level logic for sharded database insertion
        System.out.println("Persisting metadata to shard based on userId: " + userId);
        System.out.println("Photo ID: " + photoId + " | Storage Path: " + s3Url);
    }

    public static void main(String[] args) {
        PhotoMetadata upload = new PhotoMetadata("user_8821", "https://s3.thecodeforge.io/bucket/img_99.jpg");
        upload.saveToDatabase();
    }
}
▶ Output
Persisting metadata to shard based on userId: user_8821
Photo ID: 7c9e... | Storage Path: https://s3.thecodeforge.io/bucket/img_99.jpg
🔥Forge Tip: The 'Pull' vs 'Push' Feed Model
For regular users, 'Push' their posts to followers' pre-computed feeds (Fan-out). For celebrities with millions of followers, 'Pull' their content only when a follower refreshes their feed. This hybrid approach prevents 'Celebrity Fan-out' from crashing your message queues.

Database Sharding & Scalability Strategies

A single database instance will fail at Instagram's scale. We must shard our data. The best strategy is to shard by User_ID. This ensures that all photos from a single user live on the same shard, making the 'View Profile' query extremely fast. However, for the 'Global Feed', we might need a secondary index or a specialized search service like Elasticsearch.

We also implement a multi-layered caching strategy: Redis for the 'Latest Feed' (Pre-computed), and an LRU cache at the application level for frequently accessed user profiles. This reduces the DB read pressure by over 80%.

SchemaDesign.sql · SQL
12345678910111213141516171819202122
-- io.thecodeforge.instagram - Database Schema Concepts
-- Sharding Key: user_id

CREATE TABLE io_thecodeforge.users (
    user_id BIGINT PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    email VARCHAR(100) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE io_thecodeforge.photos (
    photo_id BIGINT PRIMARY KEY,
    user_id BIGINT REFERENCES io_thecodeforge.users(user_id),
    image_path VARCHAR(255) NOT NULL,
    caption TEXT,
    latitude DECIMAL(9,6),
    longitude DECIMAL(9,6),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Index for fast Feed Generation (Sorted by Time)
CREATE INDEX idx_user_photos_time ON io_thecodeforge.photos(user_id, created_at DESC);
▶ Output
Schema created. Sharding logic should be handled by the application layer or middleware like Vitess.
⚠ Production Reality:
Don't use auto-incrementing IDs in a distributed system. Use a 'Snowflake' ID generator (like Twitter's) to create unique, time-sortable 64-bit IDs across multiple shards.
FeatureRelational (PostgreSQL)NoSQL (Cassandra/HBase)
Data ModelStructured, ACID compliantColumn-family, High availability
Best Use CaseUser Profiles, Auth, FinancialsFeed Data, Likes, Comments, Activity Logs
ScalabilityVertical (Scaling up) or ShardingHorizontal (Linear scaling out)
ConsistencyStrong ConsistencyEventual Consistency (Tunable)

🎯 Key Takeaways

  • Separate Read and Write paths to handle lopsided traffic patterns (1:100 write-to-read ratio).
  • Use Consistent Hashing and Sharding by User_ID to manage data growth across multiple servers.
  • Implement a Hybrid Feed Model (Push for normal users, Pull for celebrities) to avoid the 'Thundering Herd' problem.
  • Leverage CDNs to minimize 'Time to First Byte' (TTFB) for global users.

⚠ Common Mistakes to Avoid

    Storing Images directly in the Database: This is a rookie mistake. Databases are for metadata; Object Stores (S3) are for files.
    Ignoring the CDN: Without a CDN, users in Europe will have massive latency fetching photos stored in a US-West data center.
    Over-estimating the need for Strong Consistency: Does a 'Like' count need to be perfect across the globe in 1ms? No. Eventual consistency is fine for social metrics.
    Under-calculating Storage: 100M photos/day * 2MB/photo = 200TB of NEW data every single day. You must discuss data retention and cold storage.

Frequently Asked Questions

How do you handle the 'Celebrity' problem in Instagram's feed?

We use a hybrid approach. For regular users, we 'fan-out' (push) their posts to followers' feeds. For celebrities like Cristiano Ronaldo, we don't push to 600M+ people. Instead, we store their post in a 'Hot Table' and pull it into a follower's feed only at request time.

Which database is better for Instagram: SQL or NoSQL?

Both. SQL (sharded) is better for relational data like User Profiles and Followers where referential integrity matters. NoSQL (like Cassandra) is better for the massive, write-heavy stream of Likes, Comments, and Activity Feeds.

How do you ensure 'High Availability' for image viewing?

We use Object Storage with cross-region replication and a global CDN. If one region goes down, the CDN automatically routes requests to the next nearest healthy edge location or origin server.

🔥
Naren Founder & Author

Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.

← PreviousDesign TinyURL — InterviewNext →Design a Caching System
Forged with 🔥 at TheCodeForge.io — Where Developers Are Forged