What is database sharding?

In short
Database sharding is a way to split one large database across several machines, so each machine stores and handles only part of the data. It lets a system grow by adding servers instead of buying one bigger server. MongoDB calls this approach sharding and uses it for very large data sets and heavy traffic.
Why one server is not always enough
A database runs on a server with a fixed amount of CPU, memory and disk. When an application gets many queries per second, the processor can become the bottleneck. When the data you read often is bigger than the available memory, the disk has to do much more work, and everything slows down.
There are two main ways to grow a system. Vertical scaling makes one server stronger, with a faster CPU, more RAM or more storage. This works well for a while, but there is a practical limit to how powerful a single machine can get, both in technology and in what cloud providers offer.
Horizontal scaling takes a different path. You split the data and the workload across several servers and add more servers as you need more capacity. Each machine handles only a subset of the work. This can cost less than buying high-end hardware, but it brings more complexity in setup and maintenance.
Sharding is the horizontal approach applied to a database. Each piece of the data lives on a different machine, called a shard.
How it works
In MongoDB, a sharded cluster has three main parts:
- Shards hold the data. Each shard contains a subset of the sharded data, and each shard runs as a replica set, which is a group of servers that keep copies of the same data.
- The query router, called mongos, sits between your application and the cluster. Your app talks to mongos, and mongos sends each request to the right shard.
- Config servers store metadata and settings, such as which data lives on which shard. They also run as a replica set.
MongoDB shards data at the collection level. A collection is a group of related documents, and its documents are spread across the shards. To decide where each document goes, MongoDB uses a shard key, which is one field or several fields in the document. The value of the shard key determines the document's place in the cluster.
MongoDB splits the data into chunks. Each chunk covers a range of shard key values. A background process called the balancer moves chunks between shards so the data stays evenly spread out.
Choosing a shard key
The shard key is the most important decision in sharding. You pick it when you shard a collection. MongoDB allows you to change the shard key later through resharding, but that process is slow and uses a lot of resources, so it is better to get the choice right at the start.
A good shard key spreads writes across shards and lets common queries reach only a few shards. If queries do not include the shard key, mongos has to send the query to every shard and gather the results. These scatter-gather queries can run for a long time.
MongoDB offers two strategies:
- Hashed sharding computes a hash of the shard key value and uses that hash to place data. This gives a more even spread, especially when the key grows steadily, such as an increasing timestamp. The trade-off is that range queries on the key are less likely to hit only one shard.
- Ranged sharding places documents by ranges of shard key values. Nearby values tend to sit on the same chunk, so range queries can target fewer shards. The downside is that a poorly chosen key can leave data unevenly distributed.
If you want to learn the basics of how a database stores records before splitting them, our database indexing guide is a useful starting point.
A simple example
Imagine an online store that keeps orders in a collection. Sharding the collection on a customer ID field could place each customer's orders on one shard. A query for one customer then goes to a single shard. A query for all orders in a date range might still need every shard.
Here is a simplified shell sequence in MongoDB that enables sharding on a database and then shards a collection by a hashed key:
sh.enableSharding("shop") sh.shardCollection("shop.orders", { customerId: "hashed" })
After the collection is sharded, your application still connects to mongos through mongosh or a driver. It should never connect straight to a single shard for normal reads and writes.
Common mistakes
A few mistakes show up often. Choosing a shard key with low variety, such as a field with only a few possible values, can leave most data on one shard. Choosing a key that always rises, such as a plain timestamp, can send all new writes to the same shard when using ranged sharding. Connecting applications directly to a shard instead of mongos is another common error, because unsupported commands on a shard return errors.
Sharding also adds operational work. You need to plan the cluster, monitor the balancer and keep config servers and shards healthy.
Alternatives
Sharding is not the only way to scale. Many applications first benefit from better queries, indexes and more memory on one server. Read replicas can offload read traffic without splitting the data. Vertical scaling is simpler and is often enough for a long time. Consider sharding when the data set or write load clearly exceeds what one well-tuned server can handle.
Sharded clusters remain partly available even when some shards are down. Data on an unavailable shard cannot be reached, but operations aimed at the other shards can still succeed.
FAQ
Is sharding the same as replication?
No. Replication copies the same data to several servers for safety and read capacity. Sharding splits different data across servers so that no single machine holds everything. MongoDB often uses both together, with each shard running as a replica set.
Can I change my shard key after I choose it?
Yes, MongoDB lets you reshard a collection by changing its shard key, starting in version 5.0. The process is slow and resource intensive, so it is wiser to plan the key carefully before you start.
Do I need sharding for a small application?
Usually not. Sharding adds infrastructure and maintenance work. Most small and medium applications run well on a single replica set, and you should consider sharding only when data size or traffic outgrows that setup.
What happens if one shard goes offline?
The cluster can keep serving reads and writes for data on the other shards. Data that lives only on the unavailable shard cannot be accessed until it returns.
Sources
This page is based on the official documentation below. Read it for full detail.
Database sharding — official documentation ↗