Home›Design›Instagram, one tap, every layer
Design · System design X-ray

Instagram: one tap, every layer

Open your feed and about a dozen systems take part before the first photo appears. Switch between 2012, when three engineers ran it, and the multi-data-center system that followed. Tap any box for what it does, why it is there, and what Instagram actually ran, with the source.

The story, live

Follow one tap through Instagram

Step through the chapters, or explore on your own: switch the year, replay a request, tap any box. Every fact comes from Instagram's own engineering posts, listed at the end.

The story

How three engineers became three data centers

2012: keep it boring, keep it stateless

Fourteen million people, three engineers, one Amazon region. The stack is plain on purpose: Django on stateless web servers behind load balancers, PostgreSQL for the data, Redis and Memcached in front of it, a task queue for work that can wait, and photos on S3 behind a CDN. When traffic grows, they add web servers. Nothing on those servers needs to be moved.

The database is the first thing that cannot just be copied

Web servers you can clone. Rows you cannot. So they split users and their media across thousands of logical shards, each one a PostgreSQL schema, mapped onto a dozen physical servers. A logical shard can move to a new server later without rewriting its data. And every ID is minted on its own shard, with the time, the shard number and a counter packed into 64 bits, so no central ID service can become the bottleneck.

Do the expensive part later

Posting a photo stores the file, writes one row, and answers. Putting that photo into every follower's feed is queued, and about 200 workers do it in the background. Reading a feed is then cheap: the list is already built.

2013 to 2015: one data center becomes a single point of failure

Instagram moves from AWS into Facebook's data centers, then spreads across three. Because the web and async tiers hold no state, each data center gets its own. PostgreSQL keeps a primary per shard and read replicas in every region, so reads stay local and writes travel. Cassandra holds data that must exist everywhere. Caches stay local, and invalidations ride the database's replication stream so each region clears its own. Facebook later shuts down a whole data center at peak as a test, and users do not notice.

2018: the long tail

At this size the average is fine and the slowest reads are the problem. Garbage collection pauses in Cassandra show up as tail latency. Instagram replaces Cassandra's storage engine with RocksDB and publishes it as Rocksandra: on one cluster, P99 reads drop from 60 ms to 20 ms.

Decisions you can reuse

What to take from it

Keep the app tier stateless

Any server can answer any request, so adding servers, losing one, or adding a data center is routine.

Put files on the edge

Photos never pass through the web servers. Object storage plus a CDN carries most of the bytes.

Shard early, by the key you read by

Logical shards on few physical servers can be moved later without rewriting data. See System design.

Do slow work later

Fan-out to followers runs on a queue, so posting stays fast. The feed is built on write, not on read.

Cache, then plan for stale caches

Caches carry most reads. With several regions, invalidation becomes its own system.

Pick the store per data shape

Relational rows in PostgreSQL, huge append-only feeds in Cassandra, search in a search engine. See Database families.

The design docs behind it

Each part of this system has its own design doc: Social feed · Search and AI retrieval · Product catalog. The whole story starts at System design 1: one server.

Sources

Where these facts come from

Part 3 of 4 · Design · 28 pagesAll pages

Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.