Database sharding: how to split data across servers
Sharding is a horizontal scaling method that divides one logical data set among multiple database servers and routes requests to the server responsible for each part. It can help when a single server is approaching its storage or write-throughput limits, but query routing, data rebalancing, backups, and failure recovery become more complex. If reads alone are the bottleneck, consider read replicas or caching first. Consider sharding only after measuring storage or write limits that cannot be addressed by scaling one instance.
How is data divided?
Choose a shard key for each row or document. Its value determines which shard stores that item. A shard is a database instance or logical unit that holds part of the full data set. When a client sends a request, a router or application shard map interprets the key and directs the request to the destination. As data grows or load becomes uneven, key ranges or data chunks may need to move between shards and be redistributed.
Application
| request includes a key
v
Router / shard map -- looks up key and placement metadata
+--> Shard A: some key ranges or hash values
+--> Shard B: some key ranges or hash values
+--> Shard C: some key ranges or hash valuesThis diagram illustrates a general routing concept. Real products differ in routers, metadata, chunk sizes, and replication. For example, in a MongoDB sharded cluster, mongos acts as the router and each shard is deployed as a replica set. That is a product-specific design, not a requirement for every sharding system. MongoDB: Sharding Introduction and Architecture
How does sharding differ from partitioning and replication?
All three approaches can involve pieces of data, but they solve different problems.
| Approach | Where the data lives | Main purpose | Typical added responsibility |
|---|---|---|---|
| Sharding | Distributed across multiple database instances or servers | Horizontally scale storage and write capacity across nodes | Shard keys, request routing, cross-shard queries, and rebalancing |
| Partitioning | Usually within one database instance, with a logical table split into physical partitions | Manage parts of a large table for some queries, retention, or deletion tasks | Partition-key design and partition maintenance; it does not by itself mean multiple servers |
| Replication | Copies of the same changes are propagated to multiple servers | Availability, read distribution, or data delivery | Lag and consistency policy and failover; it does not divide the data set itself |
Partitions can split a large table into smaller physical units while still appearing to an application as one logical table. PostgreSQL 18's built-in table partitioning, for example, manages partitions within the same database server. Under suitable conditions it can prune query scope or make it faster to detach or drop old partitions. That differs from sharding data across servers. PostgreSQL 18: Table Partitioning
A shard can itself be replicated for high availability. Sharding and replication are therefore not mutually exclusive: sharding assigns portions of data to nodes; replication maintains additional copies of each portion.
Why is the shard key the most important design choice?
The shard key determines both where data is placed and how many shards a query must contact. A good key distributes data and request load while allowing common queries to target a specific shard. If no single field meets the needs, a compound key may help, but selection should account for real read/write patterns and data distribution.
For example, imagine a service that stores orders for many business customers. Using customer ID as the shard key can keep one customer's orders and related data together, making customer-specific queries easier to route. But if one very large customer generates most of the writes, its shard may become overloaded. If there are only a few customers, key cardinality may be too low to use many shards effectively. This is an illustrative design scenario, not a measured distribution or performance result.
Evaluate at least these factors:
**Cardinality and frequency:** Too few possible values, or most records sharing one value, makes even distribution difficult.
**Monotonic growth:** With range-based placement, an always-increasing value such as time or an auto-increment ID can send new writes to one end shard.
**Query patterns:** If common queries include the shard key, the router can target only the necessary shard. Without it, a fan-out or scatter-gather query may contact every shard.
**Key changes and movement:** Changing a key so data belongs on another shard can be costly. Changing the strategy later may require redistributing existing data.
MongoDB's documentation explains that low cardinality, high frequency of particular values, and monotonically changing keys can lead to uneven distribution. Queries containing the shard key can be routed to target shards; queries without it may be broadcast. MongoDB: Choose a Shard Key, Distributed Queries
MongoDB range-based sharding places nearby key values into the same range chunks, which can help target range queries to fewer shards. Hashed sharding hashes key values and may reduce write concentration for monotonically increasing keys, but range queries may then touch more shards. These are MongoDB strategy details and should not be assumed to describe other products.
What changes when a query touches multiple shards?
If a request maps to one shard, the router can contact just that node. If it cannot determine where the needed data lives, it may send the query to several shards and merge the results. Fan-out adds network round trips, result coordination, and failure points; adding shards does not make every query linearly faster. Parallel execution can help large analytics or whole-dataset aggregates, but routine user requests that touch every shard can erase the scaling benefit.
Joins and transactions across multiple shards can also become more expensive. A data model may need to keep records that are commonly read and written together near one another, reduce cross-shard operations, or handle distributed transaction failures and retries in the application. Sharding changes query and business boundaries; it is more than adding servers.
What must you operate after sharding?
Monitor whether data and load remain balanced. Track per-shard data size, CPU, disk and network use, read/write mix, request frequency by key, the share of fan-out queries, and progress or failures in data movement. A single overloaded shard can be the bottleneck even when the cluster has spare capacity elsewhere.
Backups and schema changes also need per-shard planning. Restoring only some shards to a different point in time can leave cross-shard data inconsistent. Coordinate schema changes across all shards, understand how reads and writes behave during rebalancing, and decide how a shard failure affects the whole service. Check whether the platform manages shard placement and routing or leaves those responsibilities to the application.
How should you decide whether to shard?
Measure data volume, write throughput, concurrency, and storage against the limits of one instance.
Evaluate whether vertical scaling, index/query improvements, read replicas, caching, or table partitioning within one instance can address the bottleneck.
Analyze key fields and filters in representative queries. Compare shard-key candidates for distribution, hotspots, fan-out, and tenant or business isolation needs.
Estimate the operational cost of failure recovery, backups, schema rollout, rebalancing, and key changes. Key changes may require data movement, so evaluate them early.
Validate realistic distributions and queries in an isolated environment. Measure whether more shards actually reduce the bottleneck, and exercise cross-shard reads/writes and recovery scenarios.
Sharding divides one data set among servers to move beyond a single instance's storage or write limits. The shard key controls request routing and load distribution, while coordination across shards adds complexity. Measure the current bottleneck, compare simpler options, and shard only when the key design and cross-shard work are supportable. Microsoft's Azure Architecture guidance likewise describes sharding as a lasting source of complexity across development, operations, queries, and recovery. Azure Architecture Center: Sharding pattern
Scope and sources
[MongoDB Manual: Sharding Introduction and Architecture](https://www.mongodb.com/docs/manual/sharding/)
[MongoDB Manual: Choose a Shard Key](https://www.mongodb.com/docs/manual/core/sharding-choose-a-shard-key/)
[MongoDB Manual: Distributed Queries](https://www.mongodb.com/docs/manual/core/distributed-queries/)
[PostgreSQL 18: Table Partitioning](https://www.postgresql.org/docs/18/ddl-partitioning.html)
[Microsoft Azure Architecture Center: Sharding pattern](https://learn.microsoft.com/en-us/azure/architecture/patterns/sharding)
Official documentation was checked on 2026-11-22. General concepts describe distributing one logical data set across servers; range/hash strategies and mongos routing are MongoDB-specific examples. The same-server partitioning description is specific to PostgreSQL 18. The customer-order scenario and diagram are illustrative, not measured. No product was configured and no query or recovery test was run.