Instagram Architecture

A photo and video feed that stays fast as the user base grows by orders of magnitude.

On the roadmap:Software Architect · step 5: Learn from the giants

Overview

Instagram is a good example of scaling a monolith before splitting it: keep the application stateless, shard the database, cache aggressively, and push media to a CDN. This summary is written from public engineering talks and blog posts; see the linked resources for the originals.

Architecture highlights

  • Started as a small Django and PostgreSQL service and scaled by adding caching and sharding
  • Media is stored in object storage and served through a CDN
  • Feeds mix precomputed timelines with ranking at read time
  • Heavy use of Memcached-style caching in front of the database

Architecture diagram

Key components

  • Application servers

    Stateless web and API tier that scales horizontally behind load balancers

  • Sharded relational database

    Stores users, relationships and media metadata, split across many shards

  • Cache tier

    Absorbs most reads for profiles, counts and feed fragments

  • Media storage and CDN

    Holds originals and resized variants and delivers them near the viewer

Challenges and trade-offs

  • Generating unique ids across shards

    Ids must be unique and roughly time-ordered without a single bottleneck.

  • Feed fan-out

    Accounts with millions of followers cannot write to every follower's timeline, so they are handled at read time.

  • Hot keys and cache stampedes

    Viral posts concentrate traffic on a few cache keys, which needs request coalescing and short-lived local caches.

Study: Instagram

Learn how Instagram is built from the people who explain it. Watch first for the picture, then read for the detail.