How to design a system like Instagram
Photos are the easy part — the feed is where the real system design lives.
Instagram splits into two problems: storing media (object store plus CDN, with metadata in a database) and generating each user's feed. The feed is the hard part — the fan-out-on-write vs fan-out-on-read trade-off, precomputed timeline caches, sharding the data, and traversing the followers graph are what make it scale to billions of reads.
Upload the photo
When a user posts, the app server streams the image bytes into object storage rather than the database, because databases are slow and costly for large blobs. The store returns a URL, and only lightweight metadata — the poster, caption, timestamp, and that URL — is written to the database. Separating the heavy media from the light metadata is the first key decision.
Serve media through a CDN
The photo in object storage is distributed to a CDN so it can be delivered from an edge location physically near each viewer. This makes images load fast worldwide and keeps the enormous volume of media traffic off your own servers. Since photos are immutable once posted, they cache perfectly at the edge.
Store metadata in a sharded DB
Posts, users, likes, and comments are structured data that live in a database. At Instagram's scale one database can't hold it all, so the data is sharded — partitioned across many nodes by a key like user ID — so each shard owns a slice of the load. This spreads both storage and query traffic across the fleet.
Model the followers graph
Who follows whom is a graph, and it's the input to every feed. To build your timeline the system must know exactly whose posts you should see, so the follow relationships are stored and queried heavily. This graph is what turns a pile of posts into a personalized experience.
Fan-out on write
The pivotal design choice is when to assemble the feed. Fan-out on write does the work at post time: the moment you publish, the system looks up your followers and pushes the post's ID into each of their precomputed timelines. Reads then become trivial lookups, at the cost of a burst of writes — which is why it needs a special case for accounts with millions of followers.
Read the cached timeline
Because each user's timeline was assembled in advance and kept in an in-memory cache like Redis, opening the app is just fetching a ready-made list of post IDs and hydrating them with media URLs from the CDN. This is what makes the feed feel instant even though it draws from thousands of accounts. The precomputation moved the cost off the read path entirely.
Two problems hiding in one app
"Design Instagram" sounds like it's about photos, and interviewers let you believe that for the first five minutes. But the photo-storage problem is largely solved by off-the-shelf infrastructure, while the genuinely hard, interesting problem is the feed: how do you show hundreds of millions of people a fast, personalized, time-ordered stream of posts from everyone they follow, when the whole system serves reads at a scale of many billions per day?
The right way to approach the design is to split it cleanly. First, the write and storage path — someone uploads a photo, and it has to be stored durably and served quickly. Second, the read path — someone opens the app and expects an instant feed. These two paths have completely different characteristics (writes are rare and heavy, reads are constant and must be cheap), and almost every important decision in the design comes from optimizing them separately. Keeping them distinct in your head is what stops the problem from becoming an intractable blob.
The upload path — media, metadata, CDN
When a user posts a photo, the first decision is *where the bytes go*. A photo is a large binary object, and databases are expensive, slow, and wasteful when used to store big blobs. So the image is streamed into object storage — a service like Amazon S3 that is cheap, effectively infinite, and built exactly for this. The store returns a URL.
Only then do you touch the database, and you write very little: the poster's ID, the caption, a timestamp, and the URL pointing at the object store. This separation — heavy media in object storage, light metadata in the database — keeps your database rows tiny and your queries fast.
Finally, because millions of people may view a popular photo and it never changes once posted, the media is distributed through a CDN. Edge servers around the world cache the image so each viewer downloads it from a nearby location rather than from your origin. Immutable content like photos caches essentially perfectly, so the CDN absorbs the overwhelming majority of media bandwidth. The upload path, then, is: client → app server → object storage (+ metadata to the DB) → CDN for delivery.
The feed problem — fan-out on write vs read
Now the hard part. Your feed is the merged, reverse-chronological list of posts from every account you follow. There are two fundamental ways to build it, and choosing between them is the heart of the design.
- Fan-out on read builds the feed at query time. When you open the app, the system looks up everyone you follow, fetches their recent posts, merges and sorts them, and returns the result. Writes are trivial (posting just saves one row), but *every single read* does expensive graph traversal and merging — and reads vastly outnumber writes, so this becomes the bottleneck.
- Fan-out on write does the work up front. The moment you publish a post, a fan-out service looks up all your followers and pushes the post's ID into each of their precomputed timelines, typically stored in a fast in-memory cache like Redis. Reads then become a trivial lookup of a ready-made list. The cost moves to write time: one post can trigger thousands of timeline insertions.
Because Instagram is read-dominated, fan-out on write is the default — you pay the write cost once so that the billions of reads are cheap. But it has a famous failure mode.
The celebrity problem and the hybrid fix
Pure fan-out on write quietly assumes users have a manageable number of followers. That assumption shatters for celebrities. If an account with 300 million followers posts, fan-out on write must perform 300 million timeline insertions for a single post — a write storm that's slow, expensive, and bursty. Worse, many of those followers may not open the app before the next post lands.
The standard solution is a hybrid. For ordinary users, fan-out on write is used as normal — their posts are pushed into followers' timelines eagerly. But posts from a small set of very-high-follower accounts are *not* fanned out. Instead, when you load your feed, the system takes your precomputed timeline (from the people you follow normally) and, at read time, *merges in* the latest posts from the handful of celebrities you follow. This is fan-out on read applied only to the expensive minority. You get the best of both: cheap reads for the common case, and no write explosions for celebrities. This blend of strategies, chosen per-account based on follower count, is exactly the kind of pragmatic trade-off that separates a textbook answer from a real one.
Scaling the data — sharding, caching, the graph
Underneath both paths sit three scaling concerns that make the whole thing hold together at billions of users.
Sharding. No single database can store all the users, posts, likes, and comments, so the data is partitioned across many nodes — sharded by a key such as user ID. Each shard owns a slice of the data and traffic, which spreads load horizontally. The cost is that queries spanning many shards (and keeping IDs globally unique) become harder, so shard keys are chosen carefully to keep the common queries local to one shard.
Caching. The timelines themselves live in an in-memory cache like Redis, and so do hot objects — popular posts, user profiles, follower counts. A cache hit answers in microseconds and never touches the database, and since a tiny fraction of content drives most of the traffic, caching the hot minority removes the vast majority of read load.
The followers graph. Every feed depends on knowing who follows whom, so the follow relationships are stored as a graph and queried constantly — both to fan out a new post to followers and to know whose celebrity posts to merge at read time. This graph is the join between the two halves of the system.
Put together, the design is a study in moving work to where it's cheapest: heavy media offloaded to object storage and a CDN, feeds precomputed at write time so reads are instant, data sharded so no node is a bottleneck, hot data cached in memory, and the followers graph tying the read and write paths into one personalized product.
Media (the photo bytes) goes to object storage and is served through a CDN; only small metadata lives in the database.
The core challenge is the feed: assembling a personalized, time-ordered list from everyone a user follows.
Fan-out on write pushes each new post into followers' precomputed timelines — fast reads, expensive writes.
Fan-out on read builds the feed at query time by pulling from followed users — cheap writes, slow reads.
Hot timelines are cached in memory; the database is sharded so no single node holds all the data.
Celebrities with millions of followers break pure fan-out-on-write, so a hybrid approach is used.
Quick quiz
Where should the actual photo bytes be stored?
What is the main downside of fan-out on write?
Where do we cache — and how it goes wrong
Caching lives at every layer. So do the ways it can bite you.
A crash course on scalability
How one box on a laptop becomes a system that serves millions.
How Amazon S3 works
An effectively infinite hard drive on the internet, with eleven nines of durability.
How Apache Kafka works
A distributed commit log that decouples who produces data from who consumes it.