# Sharding: Splitting One Database Across Many Servers

> Learn why a growing database gets split into shards, how a shard key decides where each row goes, and what the split costs you.

**In short:** Sharding splits one database across many servers, with a shard key deciding which shard holds each row, so no single machine has to hold everything.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/sharding

## What You Will Learn

Most apps start with one database. It is a program on a computer that keeps data safe and answers questions about it. For a while, one database is enough. Then the data grows, or the number of people using the app grows, and the single computer starts to struggle. Sharding is one of the main ways system designers respond to that problem. In this lesson you will learn what sharding means, how data gets divided, why the choice of divider matters, and what sharding costs you in return.

- **Database:** A system that stores data and answers questions about it, such as 'show me this user's profile'.
- **Server:** A computer that runs a program and answers requests sent over a network.
- **Shard:** One piece of a split database. Each shard holds a portion of the data and usually lives on its own server.
- **Shard key:** The column or field that decides which shard holds each row.
- **Row:** One record in a database table, such as one user or one message.
- **Hash function:** A formula that turns an input, such as a user ID, into a number that looks random but always comes out the same for the same input.
- **Hot spot:** One shard or server that gets far more traffic or data than the others.
- **Horizontal scaling:** Growing a system by adding more machines side by side, rather than making one machine bigger.
- **Rebalancing:** Moving data between shards so the load and storage are shared more evenly again.
- **Cross-shard query:** A question that needs data from more than one shard, so the system must ask several servers.

## Why Is One Database Server Not Enough?

A single database server has three main limits. Each one can become the reason you need to split your data.

- **Storage:** A server has a fixed amount of disk space. When the data outgrows the disk, you cannot simply keep adding rows to the same machine forever.
- **Read and write load:** A server can only handle so many questions and updates per second. When traffic goes past that point, pages load slowly or fail.
- **Memory:** Databases run faster when frequently used data fits in memory. When the working data is larger than the memory, every lookup has to touch the disk, which is slower.

Sharding answers these limits by spreading the data and the traffic across many servers. Each server holds only a slice of the whole, so each one needs less storage, handles fewer requests, and keeps more of its working data in memory. Many sources describe this as horizontal scaling. You add machines next to each other instead of trying to make one machine enormous.

> **Tip:** Do not shard just because the data is big. A well-sized server can hold a surprising amount. Check the real limit first.

Sharding is not the only way to grow a database. Adding copies of data and keeping data in separate tables are older, simpler options. Those come first for most teams. Sharding is the step you take when the simpler options no longer give you enough room.

**Quick check:** A small online shop has one database. It is slow because many staff run long sales reports, but the data is only 20 GB and fits in memory. What is the most sensible first move?

A. Split the database into twelve shards so each server runs fewer reports.
B. Look at the report queries and add simpler speed-ups before sharding, since the data already fits on one server.
C. Shard the orders table by the day of the week the order was placed.
D. Stop sharding forever, because a database that is slow can never be fixed by any method.

**Answer:** B. The data is small and fits in memory, so the limit is not storage. Adding shards would add work without fixing the real problem. Simpler speed-ups come first, and sharding is the last step.

## How Is Data Divided Across Shards?

Sharding needs a rule for deciding where each row goes. The rule is built on a shard key. A shard key is a column, or a small set of columns, that the system reads to choose a shard. If the shard key is the user ID, then any request that includes a user ID can be sent to exactly one shard. The shard key should be a value that does not change after the row is created, because moving rows when the key changes is slow and error-prone.

There are three common strategies for turning a shard key into a shard. Each one balances things differently.

The range strategy splits the key values into continuous ranges. For example, users 1 to 1,000,000 might sit on shard A, and users 1,000,001 to 2,000,000 on shard B. The main benefit is simplicity. Nearby keys stay together, so a question such as 'show me users 500 to 600' can be answered by one shard. The downside is that new data often lands in the newest range. If IDs or timestamps keep growing, the newest shard takes almost all the writes while older shards sit idle.

The hash strategy runs the shard key through a hash function and uses the result to pick the shard. Because a good hash function spreads its outputs widely, records end up spread across shards more evenly, even when the keys themselves follow a pattern. The cost is that neighboring keys no longer sit together. A question about a range of keys may have to ask every shard.

The directory strategy, also called the lookup strategy, keeps an explicit table that maps each key, or group of keys, to a shard. To find a row, the system first asks the directory where it lives. This is the most flexible option, because you can move any key at any time just by updating the table. The cost is an extra lookup on every request, and the directory must stay available, or nothing can be found.

- **Range:** Good for range questions and simple to explain. Weak when new data always lands on one end of the range.
- **Hash:** Good at spreading load evenly. Weak for range questions, which must visit many shards.
- **Directory:** Good at flexible placement and moving single keys. Weak because every request needs a lookup, and the lookup table must be kept running.

Hash-based placement has one more trap.

## How Do You Choose a Shard Key That Spreads the Load?

A good shard key gives each shard about the same share of the data and the traffic. A bad shard key sends most of the work to a few shards. Those overloaded shards are called hot spots, and they become the bottleneck of the whole system. Adding more shards does not help much if one shard still takes most of the writes.

- **Keys that always grow:** Timestamps and auto-increasing IDs send every new row to the newest range. With range sharding, that makes one shard the hot one.
- **Keys with few values:** If a key has only a handful of possible values, the shards cannot be split finely. Some values will be much more common than others, and their shards fill up.
- **Keys with a few very popular items:** Even a good hash can send too much traffic to one shard if a single user, product, or topic gets huge attention. Hashing spreads keys, not the popularity of each key.

A good shard key usually has many distinct values, is spread fairly evenly in real use, and appears in most of the questions the app asks. If most questions include the user ID, sharding by user ID lets the app answer them from one shard.

**Quick check:** A chat app stores its messages table on several shards. It sends each message to a shard based on the first letter of the sender's username. The shards for names that start with A and S are overloaded, while the shards for X and Z are almost idle. What is wrong, and what would work better?

A. Nothing is wrong. Letters are spread across the alphabet, so each shard should get about the same load.
B. The key is too clever. The app should shard by the length of the username instead.
C. The first letter has few values and is not spread evenly in real names. A hash of a unique user ID would spread the messages more evenly.
D. The app should stop sharding, since no shard key can be fixed once it is chosen.

**Answer:** C. Many usernames start with common letters, so a few shards get most of the traffic. A unique ID that is run through a hash spreads rows far more evenly, because it does not follow the habits of real names.

## Worked Example: Opening a Profile in a Photo App

Imagine a photo-sharing app like Instagram. It stores billions of photo records, far too many for one server. The team decides to shard the user data by hashing the user ID. Here is what happens when you open your profile, step by step.

1. **Your app reads your user ID:** Your phone sends a request for profile 48213. The user ID is the shard key, so the server handling the request has the value it needs.
2. **The hash function runs:** The server passes the user ID through the hash function. The output is a number that is the same every time for that same ID.
3. **The number picks the shard:** The server takes that number and computes which of the shards owns it. For example, it might use the remainder after dividing by the number of shards.
4. **Only one server is queried:** The request goes to that one shard. The other shards never see it, so the rest of the database keeps its time for other users.
5. **The profile comes back:** The shard finds the row and returns it. Because the work touched one server, the response stays fast even when the full data set is huge.

The code below uses a simple, made-up hash so that the output never changes. Real systems use stronger hash functions, but the idea is the same. It runs three user IDs through the rule and prints the shard for each one.

`shard_route.py`

```python
def shard_for(user_id, shards=4):
    h = 0
    for ch in str(user_id):
        h = (h * 31 + ord(ch)) % 1000
    return h % shards

for uid in [101, 102, 103]:
    print(uid, "-> shard", shard_for(uid))

```

Output:

```text
101 -> shard 2
102 -> shard 3
103 -> shard 0

```

Notice what the code does not do. It never loops over all the shards to find a user. It computes one answer and goes there. This is the main gain of hash sharding for lookups by key. The cost shows up in a different question, such as 'list all users whose IDs fall between 100 and 200.' Since those IDs land on different shards, the app would have to ask every shard and combine the answers.

> **Tip:** When you design a sharded app, write down the top five questions the app asks. If most of them include the shard key, sharding fits well. If many of them need data from everywhere, sharding will make the app slower and harder to build.

## What Are the Costs of Sharding?

Sharding solves the capacity problem, but it brings new problems of its own. Most of them come from the fact that the data is now split across separate machines that do not share a single view.

- **Cross-shard queries:** A question that needs rows from several shards must ask each one, wait for all the answers, and combine them. Joins, which link two tables together, get much harder when the linked rows sit on different servers.
- **Transactions:** A transaction is a group of changes that must all succeed or all fail. When the changes touch several shards, the system needs extra coordination to keep them consistent. Designers often avoid this where they can.
- **Rebalancing:** When you add a shard, some data has to move to it. Moving data while the app keeps serving users is slow and risky. A bad move can overload the very servers it was meant to relieve.
- **Operations:** More servers mean more things to monitor, back up, upgrade, and debug. Teams need tools and people to run the whole fleet, not just one machine.

Sharding also changes how the data can be reasoned about. Once the data is split, one shard can be down while others keep working. That can be a good thing for isolation, but it means the app must handle partial failure. Replication, which keeps copies of the same data on more machines, is a separate tool that helps with availability. The replication lesson covers it, and you should not confuse it with sharding. Sharding splits data. Replication copies it.

The usual order for growing a database is to start with simpler moves. Teams often add indexes, add a cache that keeps hot answers in memory, and add read copies first. These steps cost less and keep the design easy to understand. Sharding usually comes after them, when the single primary server still cannot keep up with writes or the data no longer fits.

> **Tip:** Sharding is hard to reverse. Once rows live on many servers and the app is built around the shard key, changing the key later means moving everything. Pick the shard key with care, and leave room in the plan for rebalancing.

**Quick check:** A small team has one database with 300 GB of data. Users complain that reports are slow, but writes are fine and the server has spare capacity. The team is considering sharding by a hashed report ID. What is the best advice?

A. Shard by the hashed report ID now, because hashing always makes reports faster.
B. Do not shard yet. Try caching and read copies first, and check whether the reports are slow because of how they are written.
C. Shard by the hashed report ID, and keep one shard for reports and one for everything else.
D. Turn off reports, because sharded databases cannot run reports at all.

**Answer:** B. Writes are fine and capacity is available, so the bottleneck is not write load. Sharding by a hashed ID would scatter related reports across shards and make report questions harder. The simpler steps come first.

## How Do You Plan a Shard Step by Step?

If you ever plan a sharded system, these steps keep you from skipping the hard parts. They are a learning guide, not a formal method.

1. **Measure the real limit:** Find out which limit you hit: storage, writes, reads, or memory. Sharding fixes some of these and not others.
2. **List your main questions:** Write down the questions the app asks most. Note which ones include a single key, such as a user ID.
3. **Choose a key that spreads:** Pick a key with many values, that does not grow in one direction, and that appears in most questions. Check for popular items that could overload one shard.
4. **Pick the strategy:** Use hash placement for even spread and key lookups, range placement for range questions, or a directory when you need full control and can keep the lookup running.
5. **Plan the moves:** Decide how you will add shards later and how you will move data without taking the app down. Consistent hashing and a clear rebalancing plan help.
6. **Watch for hot spots:** After launch, track the load on each shard. If one shard runs much hotter than the rest, the key or the strategy needs another look.

Notice that the steps start with measuring and end with watching. Sharding is not a one-time decision. The mix of data and traffic keeps changing, so the shards need checking as the app grows.

## Key Takeaways

- **Sharding splits data:** Each shard holds a portion of the rows, usually on its own server, so no single machine has to hold or serve everything.
- **The shard key decides placement:** A good key spreads rows and traffic evenly. A bad key creates hot spots and unbalanced shards.
- **Three strategies:** Range keeps neighbors together, hash spreads load evenly, and directory gives the most flexibility at the cost of a lookup.
- **The costs are real:** Cross-shard queries, transactions, rebalancing, and daily operations all get harder once data is split.
- **Use it late:** Try simpler steps first. Shard when a real limit is reached and the key is clear.

Sharding is one of the main tools for growing a database by adding machines. In the next lessons you can study the other ways to split a system, such as splitting by feature, and the tools for keeping copies of data safe.

## Sources

- [Aerospike: Database Sharding Explained for Scalable Systems](https://aerospike.com/blog/database-sharding-scalable-systems/)
- [Aerospike: What Is Sharding and How It Works for Database Scale](https://aerospike.com/blog/what-is-sharding/)
- [Linode Guides: Database Sharding: Concepts, Examples, and Strategies](https://www.linode.com/docs/guides/sharded-database/)
- [last9: Database Sharding: How It Works and When You Need It](https://last9.io/blog/database-sharding/)
- [AlgoMaster: Sharding (system design lesson)](https://algomaster.io/learn/system-design/sharding)
- [Hello Interview: Sharding in System Design Interviews](https://www.hellointerview.com/learn/system-design/core-concepts/sharding)
- [DesignGurus: The Complete Guide to Database Sharding for System Design Interviews](https://designgurus.substack.com/p/the-complete-guide-to-database-sharding)
- [DEV Community: Database sharding and partitioning (Gowtham Potureddi)](https://dev.to/gowthampotureddi/database-sharding-and-partitioning-the-scaling-move-an-interviewer-makes-you-justify-34l2)
- [Data Interview: Sharding & Partitioning: Splitting Data Without Splitting Your Sanity](https://www.datainterview.com/blog/system-design-concept-concept-sharding-and-partitioning)
- [Interviews Vector: Replication vs Sharding: How to Decide](https://www.interviewsvector.com/decisions/replication-vs-sharding)
- [Stack and System: Sharding vs Partitioning vs Replication Explained](https://stackandsystem.com/series/design-decisions/sharding-vs-partitioning-vs-replication)
- [Sujeet Jaiswal: Sharding and Replication](https://sujeet.pro/articles/sharding-and-replication)
- [System Design Sandbox: Sharding](https://www.systemdesignsandbox.com/learn/sharding)
- [Imraan: Database Sharding: Partitioning Strategies, Hot Keys, and Rebalancing](https://imraan.in/backend-development/database-sharding/)
