Back to blog

The database is full: buy a bigger one, or split the data?

Thắng Đoàn
Thắng Đoàn

FotoFlow has 50,000 users and the photo metadata database has grown past 20 terabytes, taking tens of thousands of writes a second. The team is already on the biggest machine money buys, and every backup or new replica takes the better part of a day to copy. Replication cannot help here: replication copies all the data everywhere, so to scale writes and storage, the data itself has to be split.

Split the data

Sharding splits one big database into several smaller ones: shards, each holding part of the data and taking part of the writes. A shard key decides where each row lives. The app or a router looks at the key and sends each query to the right shard. Sharding splits one big database into several smaller ones, each holding part of the data and taking part of the writes. Each shard is usually replicated too: shards scale writes, followers within a shard scale reads.

Ranges create hot spots

Shard by user id ranges and every new signup lands in the last range, which is exactly where the most active users live: shard D melts while shard A idles. The same happens with dates or alphabetical names, anything that arrives in order. Range-partitioned databases like Bigtable and CockroachDB split a busy range in two automatically, but writes that always land at the end of the key space simply follow the last range. A shard key must spread both the data and the traffic.

Hash the key, then meet the celebrity

Instead of ranges, run the key through a hash function: shard = hash(user_id) % 4. Consecutive ids scatter to unrelated shards, so new users and their traffic spread evenly. The price: range queries like "all users with ids 1 to 1000" now touch every shard. Usually a fine trade.

Hashing spreads keys, not traffic. A pop star with 50 million followers lives on one shard like anyone else, and every view of her profile lands there too: a hot key, the celebrity problem. No shard key fixes a single key that is more popular than one machine can handle. For read-hot keys, cache in front of the shard so most views never reach it. For write-hot keys like her like counter, split it: write to likes:42#0 through likes:42#7 at random and add them up when reading.

What sharding costs

A lookup by user id is fast: hash it, ask one shard. But "top 10 users by followers" is not keyed by user, so the app asks every shard and merges the answers. These scatter-gather queries are as slow as the slowest shard, and joins across shards are hard. Pick a shard key that matches your most important queries: ideally they touch exactly one shard.

Then shards fill up, and you add a fifth. But hash % 4 becomes hash % 5, and for about 80 percent of users the remainder changes. With hash modulo N, changing N reshuffles nearly every key. Terabytes move between shards while the system keeps serving traffic.

What this does not solve

Sharding spreads data and writes, but growing the cluster still means moving most of the data. A placement scheme that moves almost nothing when the cluster changes is the next post.

If you sharded your biggest table tomorrow, which column would you pick, and which query would it break?
Who is your celebrity key: which one row gets more traffic than a whole shard of the rest?

One database was one problem. Four shards are four.

Next in the series: A cache node dies: why does half the cache vanish?

Sources

Instagram Engineering: Sharding IDs at Instagram

Designing Data-Intensive Applications

System Design Primer

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.