90% Full, 300GB a Week: How Instagram Sliced One Database Into a Thousand Without Losing a Single Photo
🏗️System DesignNovember 1, 2026 at 3:18 AM·8 min read

90% Full, 300GB a Week: How Instagram Sliced One Database Into a Thousand Without Losing a Single Photo

In 2012, Instagram's entire app ran on one Postgres instance filling up like a bathtub with the drain closed. They had weeks to live. The fix — sharding — would become the most consequential architectural decision in distributed systems history, and almost nobody gets it right the first time.

System DesignDatabase ShardingInstagram EngineeringDistributed SystemsSharding50-Terabyte

The Bathtub Is Overflowing

It was early 2012, and Mike Krieger was staring at a Munin graph that looked like a death sentence. Instagram — thirteen engineers, 30 million users, zero revenue — was running almost entirely on a single PostgreSQL instance. That one database held every photo's metadata, every like, every follow, every comment. And it was 90% full.

Worse: it was growing by 300 gigabytes a week.

This wasn't a slow leak. This was a countdown. Disk I/O was already degrading under the write load. Vacuum processes were taking longer. Query latency was creeping up in a way that, left unchecked, would turn the app's signature instant-upload experience into a spinner of doom. Krieger and co-founder Kevin Systrom did the math: they had weeks, not months, before the database physically could not keep up.

The team's solution — a scrappy, pragmatic sharding scheme built in a matter of weeks — would later be documented in a blog post ('Sharding & IDs at Instagram') that became required reading for an entire generation of backend engineers. It's still cited today, a decade later, because it captures a truth most teams learn the hard way: sharding isn't a feature you add. It's a surgery you perform on a patient who's still awake.

Three Ways to Cut the Pie

When you shard a database, you're answering one brutal question: given a piece of data, which machine owns it? There are three classic answers, and each one has a body count.

Key-based (hash) sharding takes something like a user_id, runs it through a hash function, and takes the result modulo N — the number of shards. It's beautifully even. No single shard gets hammered because some celebrity account happens to live there. But it has a landmine buried inside: the moment you add a shard, N changes, and hash(id) % N returns a completely different answer for nearly every key in your system. Before roughly 2010, this was simply accepted as normal pain — you'd schedule a migration weekend, rehash everything, and pray. At Instagram's growth rate, a 'migration weekend' every few months wasn't a plan. It was a slow-motion outage.

Range-based sharding is the one that feels intuitive on a whiteboard: user_ids 1 through 1,000,000 go on shard one, 1,000,001 through 2,000,000 go on shard two, and so on. Range queries — 'give me all users created in March' — become trivial. But every social network that has ever existed has hit the same wall: your earliest users are disproportionately your power users. The first million Instagram accounts belonged to early adopters who uploaded more, followed more, and commented more than the average user who joined three years later. Range sharding doesn't distribute data evenly — it distributes it chronologically, which almost never correlates with load.

Directory-based sharding solves the rebalancing problem with a lookup table: a separate service maps user_id → shard_id, and you can move any individual user anywhere, anytime, with total flexibility. The catch is you've just invented a new single point of failure and a new network hop on every single request. If the directory service goes down, your entire database — correctly sharded or not — is unreachable.

The Math That Saved Everyone: Consistent Hashing

The real answer to key-based sharding's resharding nightmare was sitting in an academic paper from 1997, authored by David Karger and a team at MIT, originally built for web caching. It's called consistent hashing, and it is one of those ideas so elegant it feels like cheating once you understand it.

Instead of mapping keys directly to shard numbers, consistent hashing maps both the keys and the shards onto the same conceptual ring — imagine a clock face with points from 0 to 2^32. A key belongs to whichever shard's position is the next one clockwise from it. Add a new shard? It slots into one spot on the ring, and only the keys between it and its counter-clockwise neighbor need to move. Instead of reshuffling 100% of your data when you scale from 4 nodes to 5, you move roughly 1/5th. Remove a node, same deal in reverse.

This single idea is why Amazon's DynamoDB, Apache Cassandra, and Redis Cluster can all add and remove nodes without apocalyptic migrations. They all implement variations of the same ring. And they all add a refinement called virtual nodes: instead of each physical machine owning one point on the ring, it owns hundreds of scattered virtual positions. Without virtual nodes, you can get unlucky and have one physical machine inherit a huge, contiguous arc of the ring. With hundreds of virtual positions per machine, the law of large numbers smooths everything into near-perfectly even distribution.

The Problem Nobody's Architecture Diagram Shows You: JOINs

Here's what the sharding tutorials don't tell you until you're already three months into the migration: a JOIN across shards is, for all practical purposes, impossible at scale. If a user's posts are on shard 7 and their followers are scattered across shards 1, 4, 12, and 19, there is no single SQL query that stitches that together efficiently. You've traded relational convenience for horizontal scale, and there's no refund.

The field has settled on three workarounds, usually used together. Denormalize aggressively — duplicate the data you need for reads directly into the shard that will query it, accepting write amplification as the cost of read simplicity. Build a search index like Elasticsearch alongside your shards specifically for the cross-cutting queries your sharding key can't satisfy — Instagram does exactly this for anything that isn't a direct lookup. And most importantly: pick a shard key that makes 99% of your actual queries shard-local in the first place. If you shard by user_id and almost every query in your app is scoped to a single user — their photos, their feed, their notifications — the cross-shard problem mostly disappears before it starts.

Splitting a Hot Shard Without Taking Down the App

Even a well-chosen shard key eventually produces a hot shard — one that grows faster or gets hit harder than its neighbors. Pinterest faced exactly this and solved it with what engineers internally called the 'Pepsi Project': an online resharding technique using dual-writes (new data goes to both the old and new shard simultaneously), a backfill process that copies historical data in the background, and a final atomic cutover once the two are verified to match. No downtime, no frozen writes, no 3 AM maintenance window. Discord has published similar techniques for splitting shards as their message volume exploded past trillions of rows. Vitess — the MySQL sharding layer originally built at YouTube — formalized this into a primitive called vsplit, turning what used to be a bespoke, terrifying migration into a repeatable operation.

The Tools That Hide the Scalpel

A newer generation of infrastructure tries to give you sharding's scale without forcing you to hand-write the ring logic yourself. Vitess sits in front of MySQL and is what lets YouTube, Slack, and GitHub run sharded MySQL while application code still mostly speaks ordinary SQL. CockroachDB and Google Spanner go further, building automatic range-based sharding directly into a distributed, Postgres-compatible (or SQL-compatible) engine — Spanner famously uses TrueTime, a system of atomic clocks and GPS receivers in Google's data centers, to guarantee global consistency across shards spread around the planet. TiDB offers a similar promise for the MySQL ecosystem. These tools don't eliminate the hard decisions — they just move them earlier, into schema design, instead of later, into a 2 AM incident channel.

The Legacy: The Decision You Can't Undo

Instagram's engineers didn't have Vitess or CockroachDB in 2012 — those tools barely existed. They built their sharding logic by hand, chose logical shards mapped onto physical Postgres instances, and encoded shard information directly into their IDs so that any service could extract the shard location without a directory lookup. It worked, and it scaled Instagram through its acquisition by Facebook and years of explosive growth afterward.

The lesson that survived the decade isn't about Postgres or hash rings or virtual nodes, though all of those matter. It's this: your sharding key is the most consequential architectural decision you will make, and you will almost always make it under pressure, with incomplete information, on a system that's already serving real customers. Pick a key that appears in nearly every query, distributes evenly, and rarely needs to change — for a SaaS product that's usually tenant_id, for a consumer app it's usually user_id, for B2B it's usually account_id. Don't shard until the bathtub is actually overflowing. But when it is, design the key before you pick up the knife — not after.

✍️
Written by Swayam Mohanty
Untold stories behind the tech giants, legendary moments, and the code that changed the world.

Keep Reading