TOUGH TONGUE AI
  • Home
    LogoTOUGH TONGUE AI

    RESOURCES

    Loading...

    HomeLibrarySD Mock · Design Instagram

    SD Mock · Design Instagram

    Overview

    Design a photo-sharing service like Instagram on a live architecture board — drag components, wire them up, and defend your design to an expert interviewer who watches every move.

    Your Instructions

    Design Instagram

    A 20-minute mock system design interview. You drive the pace. Jump to the board when you are ready; skip capacity math if you want. The interviewer grades your thinking, not a template match.

    The trap is "photos in S3, rows in MySQL, done." That sentence says nothing about who serves reads while uploads hog connections, how 3.7 TB of metadata is partitioned, or how "newest photos from the 500 people I follow" becomes one fast query. The design's substance is exactly what the quip skips.

    Two shared surfaces (only one is on screen at a time; use the panel arrow to switch):

    • Notepad. Shared notes. Requirements, capacity math, schema, and end-of-session feedback live here. You can type in it too.
    • Architecture board. Your design canvas. Drag typed components, wire them, label edges, mark scaled tiers as distributed. If it is not on the board, it is not in your design.

    Build incrementally while you talk. Trace one upload end to end through your diagram, then one feed load. Argue the tradeoff, not "industry standard."

    Design choices interviewers probe

    Read this before you go in. These are the decisions, and why they matter.

    Read fleet vs write fleet

    Uploads are large, slow, disk-bound writes. Views are small, cache-served reads. Put both on one fleet and the slow path eats the fast one. A web server holds about 500 concurrent connections. An upload holds one for seconds; a cached read holds one for milliseconds. A modest number of uploaders occupies every slot and reads starve.

    The write rate is only ~23 photos/sec, so this is a connection-occupancy problem, not a throughput problem. Say that distinction out loud. The fix is dedicated upload servers and dedicated read servers behind their own load balancers, scaled and tuned independently: long timeouts and large body limits on the write side, keep-alive and cache clients on the read side.

    Pre-signed direct-to-blob uploads that shrink the write fleet to a metadata-only service are equally defensible if you can say why.

    Two stores, not one

    Ten years of photo bytes is ~1,425 TB. Ten years of metadata is ~3.7 TB. A 400× gap means two different stores: bytes in a Blob Store (HDFS or S3) with multiple copies on different servers; rows in a sharded, replicated metadata store. Blobs inside the database make the metadata tier 400× larger than it needs to be and ruin every index.

    The second surprise is that the follow graph (1.82 TB) is almost as large as the photo table (1.88 TB). At social scale, relationships weigh as much as content, and feed generation reads the follow graph for every user.

    Shard key: UserID or PhotoID

    • Shard by UserID. All of a user's photos sit on one shard, and IDs come from that shard's auto-increment with the shard number appended. Nice locality. Then it breaks four ways: a celebrity's shard absorbs every view of every upload; prolific uploaders skew shard size; a user whose photos exceed one shard must be split anyway, so you pay cross-shard latency for exactly the users you tried to keep together; and one shard down means one user's entire history is gone.
    • Shard by PhotoID. Generate a globally unique PhotoID first, then PhotoID % 10. A user's photos scatter evenly. No user can make a shard hot or large, and a shard outage costs everyone about a tenth rather than someone everything. "All of a user's photos" becomes a scatter query. That is acceptable because it is not the hot path; the feed is.

    The catch: you need the PhotoID before you know the shard, so per-shard auto-increment no longer works. That is the chicken-and-egg problem the next decision solves.

    Where the PhotoID comes from

    One dedicated key-generation database with a single 64-bit auto-increment column works, and it is a single point of failure. If it dies, nobody can upload anywhere.

    Run two. One issues odd IDs (auto-increment-increment = 2, offset = 1), the other even (offset = 2), behind a load balancer. They drift apart harmlessly; uniqueness only requires that they never issue the same number.

    Then put time in the ID. The feed must find the newest photos on every shard. If the PhotoID starts with creation time, the primary index on PhotoID is a time index, and "newest N" on each shard is a short index scan.

    PhotoID = [ epoch seconds : 31 bits ] [ per-second sequence : 9 bits ]  
    => 40 bits = 5 bytes
    

    31 bits covers 50 years (1.6B seconds against 2.1B). 9 bits gives 512/sec, which is 22× the expected 23/sec and lands the ID on a byte boundary. The sequence resets every second; the epoch prefix keeps IDs unique across seconds. Shard is still PhotoID % 10, and consecutive sequence numbers walk through the shards in turn, so load spreads by construction.

    Snowflake (41 bits of milliseconds, 10 bits of machine identity, 12 bits of sequence, minted on each app server) is equally defensible. Instagram's own scheme generates a 64-bit ID inside each Postgres shard. The one idea that matters: time in the high bits so the ID sorts by creation order.

    Feed: pre-generate, then deliver

    • On demand. Fetch the followee list, pull each followee's latest 100 rows across 10 shards, rank, return the top 100. For 500 followees that is up to 50,000 rows and a sort at request time. The 200 ms budget forbids it.
    • Pre-generated. Dedicated feed-builder workers continuously build each user's UserNewsFeed row. Serving is one query. A builder resumes from the user's last-generated timestamp and only processes newer PhotoIDs, which is cheap precisely because IDs are time-ordered.
    • Delivery. Pull is stale and most polls return empty. Push over long-poll explodes for a celebrity with 50M followers and hammers anyone who follows thousands. Hybrid: push to small accounts, let high-follower accounts be pulled, or cap push frequency. The full fan-out theory belongs to the Newsfeed question; here you need the compact version and the reason the hybrid exists.

    Eventual for visibility, absolute for durability

    These are not the same guarantee. A photo appearing in a feed a few seconds late is fine; asynchronous builders and caches without invalidation storms are how you buy availability. A lost photo is not fine: acknowledge an upload only after multiple copies exist on different storage servers, replicate every metadata shard, and keep a hot standby for any single-instance service. Durability is built at write time, not restored afterwards.

    Growing the shard fleet

    Changing the modulus from 10 to 20 moves almost every row. Instead, create many logical partitions up front and map several onto each physical server through a config map. When a server fills, copy some partitions to a new server, replay writes until it catches up, flip the map, drain the old copy. No PhotoID changes and no application change. Consistent hashing is the alternative when you cannot pre-decide the partition count.

    Reference solution

    One complete design you can study, then rebuild in your own words. Do not recite it. In the interview, draw your graph and defend the choices.

    Requirements (lock these or take the defaults)

    • Upload, download, and view photos and videos; treat both as bytes
    • Search on titles only
    • Follow users; a News Feed of the top ~100 photos from followed users
    • 500M users, 1M DAU, 2M new photos/day, average 200 KB, ~500 followees per user
    • Feed generation under 200 ms; highly available; feed freshness can lag; no uploaded photo is ever lost; read-heavy
    • Out of scope unless asked: tags, comments, user tagging, recommendations

    Capacity

    • 2M photos/day ÷ 86,400 ≈ 23 uploads/sec. Tiny. The write path is a connection problem, not a throughput problem.
    • 2M × 200 KB = 400 GB/day → × 3,650 days ≈ 1,425 TB of bytes in 10 years
    • Metadata with 4-byte ints: User 68 B × 500M ≈ 32 GB; Photo 284 B × 2M/day ≈ 0.5 GB/day → ≈ 1.88 TB in 10 years; UserFollow 8 B × 500M × 500 ≈ 1.82 TB. Total ≈ 3.7 TB
    • 3.7 TB ÷ 1 TB per shard = 4 shards minimum; plan 10 for headroom (370 GB each)
    • Bytes : metadata ≈ 400 : 1. UserFollow ≈ Photo. Index Photo on (PhotoID, CreationDate) because recent photos are fetched first. Say this while drawing the schema, before anyone asks about the feed.

    Rough numbers are fine; the method is what gets graded.

    Components to put on the board

    How the graph works: two entry paths that never share a server. Uploads go through the upload load balancer to the write fleet, which mints a PhotoID, writes bytes to the blob store, waits for replicas, then inserts the row into shard PhotoID % 10. Feed loads go through the read load balancer to the read fleet, which answers from Memcached and the CDN and only falls through to the shards and blob store on a miss. Feed builders run off to the side, continuously, and never sit on either request path.

    Mobile Client, Web Client
        → Load Balancer (upload) → Service: Upload Service (mark distributed)
              → Load Balancer (key-gen) → MySQL (key-gen, odd) | MySQL (key-gen, even)
              → Blob Store (photo bytes, multiple copies)
              → MySQL (metadata shards, PhotoID % 10, mark distributed)
        → Load Balancer (read) → Service: Read Service (mark distributed)
              → Memcached (hot Photo rows, UserNewsFeed rows)
              → MySQL (metadata shards)
              → CDN → Blob Store (miss only)
    Worker: Feed Builder (mark distributed)
        → MySQL (metadata shards): newest PhotoIDs since last build
        → UserNewsFeed row (metadata store, cached in Memcached)
    

    Components:

    • Clients (Mobile, Web). Upload bytes with a title and visibility; load the feed; tap through to full size. The feed response carries CDN URLs, never bytes.
    • Load Balancer (upload) and Load Balancer (read). Two of them, one per path. The split is the whole point: an upload burst cannot occupy a read server's connection slots. Health checks and failover live here.
    • Upload Service (mark distributed). The slow, disk-bound path. Obtains a PhotoID, writes bytes, waits for replicas, inserts metadata, acks. Large body limits and long timeouts. Without a separate write fleet, uploaders starve readers.
    • Load Balancer (key-gen) + MySQL pair. Two databases whose only job is to hand out IDs, one odd and one even. The LB round-robins and routes around a dead one. Without the pair, one key-gen DB is a single point of failure for every upload.
    • Blob Store. HDFS or S3. The ~1,425 TB. Every object is stored as multiple copies on different storage servers, and the upload is acknowledged only after the copies exist. This box is where "never lose a photo" is enforced.
    • MySQL metadata shards (mark distributed). The ~3.7 TB in 10 shards keyed by PhotoID % 10, each replicated. Holds User, Photo, UserFollow, and UserNewsFeed. The Photo primary index on PhotoID is a time index because the ID is epoch-prefixed. A distributed KV (Cassandra, DynamoDB) with PhotoID as partition key is the book's other recommendation; it keeps replicas by default and defers deletes for undelete.
    • Read Service (mark distributed). Views, searches, feed loads. Checks Memcached before any shard and returns CDN URLs. Short timeouts, keep-alive. Sized from the 1M DAU and a peak factor, not from the write rate.
    • Memcached. Hot Photo rows and UserNewsFeed rows in front of the shards. LRU eviction. Sized by the 80/20 rule: 20% of a day's Photo rows is only ~114 MB, so in practice you cache far more than a day.
    • CDN. The delivery plane for bytes. Geo-distributed edges and photo caches serve thumbnails and full-size images; only a cold photo reaches the Blob Store. Read-side only.
    • Feed Builder (Worker, mark distributed). Continuously builds UserNewsFeed rows. For each user it reads the last-generated timestamp, scans each shard's PhotoID index for followees' photos newer than that, ranks, writes the top 100 IDs. Without it, the feed is a 50,000-row sort at request time.

    Walk one upload: Bo posts "Sunset", then Ana loads her feed

    Ana (UserID 4471) follows Bo (8812) and Cy (9903). Bo uploads three photos at 10:00:01, 10:00:02, 10:00:02; Cy uploads one at 10:00:03.

    1. Bo's phone sends POST /v1/photos with the bytes and title. The upload Load Balancer routes it to an Upload Service instance. Read servers never see it.
    2. Upload Service asks the key-gen Load Balancer for a sequence number. Round-robin lands on the odd or even MySQL; say it returns 43.
    3. It composes PhotoID = (epoch seconds << 9) | 43, i.e. 1789580402·043.
    4. It writes the bytes to the Blob Store and waits until the store confirms copies on multiple servers. "Never lose a photo" is enforced here, before the ack.
    5. Shard = PhotoID % 10 = 3. It inserts the Photo row (PhotoPath, lat/long, CreationDate) into shard 3; the shard's replicas copy it.
    6. Bo gets 200 with the PhotoID. From his side the upload is done.
    7. Asynchronously, a Feed Builder revisiting Ana reads her last-generated timestamp (10:00:00), scans each shard's PhotoID index for Bo's and Cy's photos newer than that, finds four, ranks them, and writes the IDs to Ana's UserNewsFeed row. Ana sees "Sunset" a few seconds later. The requirements allow that.

    Then Ana opens the app:

    1. GET /v1/feed hits the read Load Balancer and a Read Service instance.
    2. One query: Ana's UserNewsFeed row, most recent 100 PhotoIDs. Memcached first; on a miss, the shard, then populate.
    3. For each PhotoID it needs the row (title, path). Hot rows come from Memcached; misses go to shard PhotoID % 10, fast because the primary index is already in time order.
    4. The response carries CDN URLs, not bytes. The client pulls thumbnails from the nearest CDN edge; only a cold photo reaches the Blob Store.
    5. One feed-table read plus cached row lookups: comfortably inside 200 ms.

    Celebrity upload: an account with 50M followers must not trigger 50M feed-row pushes. Hybrid delivery: their followers pull that account's recent PhotoIDs and merge at read time; small accounts are pushed into UserNewsFeed rows as above.

    Storage server dies holding 100K photos: nothing is lost, because a single copy never existed. The upload was acknowledged only after multiple copies on different servers; reads fall to another copy. Metadata shards have replicas; single-instance services have a hot standby that takes no traffic until failover.

    Shard at 95% disk: logical partitions over physical servers. Copy some partitions to a new server, replay writes until caught up, flip the config map, drain the old copy. Zero downtime, no ID change.

    User deletes a photo: mark it deleted in metadata immediately so the Read Service and Feed Builders skip it. After the undelete window, purge bytes from the Blob Store and CDN and scrub it from UserNewsFeed rows. Takedowns use the same path with a zero window. Edits create a new PhotoID rather than mutating bytes, which keeps long CDN TTLs safe.

    Schema

    • User (68 B): UserID, Name, Email, DateOfBirth, CreationDate, LastLogin

    • Photo (284 B): PhotoID, UserID, PhotoPath, PhotoLat, PhotoLong, UserLat, UserLong, CreationDate

    • UserFollow (8 B): UserID, FolloweeID

    • UserNewsFeed: UserID, PhotoIDs[], LastGeneratedAt — one row per user

    • KeyGen: ID auto-increment — two MySQL, odd / even

    • Photo rows sharded by PhotoID % 10; index on (PhotoID, CreationDate) so "newest first" is an index scan on every shard

    • UserFollow keyed by UserID so a follower's list is one read; UserNewsFeed is one row per user

    • Every shard replicated; a KV variant keeps replicas by default and defers deletes for undelete

    • Bytes never live in a row; PhotoPath points at the Blob Store and is served through the CDN

    • The 4-byte PhotoID in the row sizing is smaller than the 5-byte ID you design; noticing that mismatch out loud earns a point

    Scaling and wrap-up

    • Upload and Read fleets scale horizontally behind their own load balancers; the read fleet is sized from DAU, the write fleet from connection occupancy
    • CDN and Memcached absorb reads; the Blob Store absorbs bytes; logical partitions absorb metadata growth
    • Memcached sized by the 80/20 rule with LRU eviction; long TTLs on photo bytes because photos are immutable
    • First metrics: upload-to-visible lag, feed p99, CDN hit rate, shard replica lag, objects below target copy count
    • If time: thumbnails and a media-processing queue, private-account enforcement at build time and again at read time, deferred delete and purge, likes counters, multi-region

    Equally defensible variants

    Cassandra or DynamoDB with PhotoID as the partition key instead of MySQL shards. Snowflake IDs minted on each app server instead of the key-gen pair. Pre-signed direct-to-S3 uploads that leave the write fleet metadata-only. Redis for UserNewsFeed rows instead of a table behind Memcached. Consistent hashing instead of a logical-partition map. What matters is that you can argue the tradeoff.

    More in System Design Interview Prep: AI Coach Edition

    • SD Coach · Real-Time Chat and Collaboration
    • SD Mock · Design Facebook Messenger
    • SD Mock · Design Uber Backend
    • SD Mock · Design Yelp / Nearby Friends
    • SD Coach · Media Streaming
    • SD Mock · Design YouTube / Netflix
    • SD Coach · Agentic AI
    • SD Coach · Agentic AI Architectures (Slides)
    • SD Mock · Design ChatGPT
    • SD Mock · Design Claude Code
    • SD Mock · Design an AI Customer Support System
    • SD Mock · Design a Real-Time Voice AI
    View the full course (43 sessions) →