Database sharding is the practice of splitting a single logical database into multiple smaller databases, called shards, each running on its own server. It is one of the most effective strategies for scaling beyond the limits of a single machine, but it introduces significant complexity. Understanding when to shard and which strategy to apply is critical to avoiding costly architectural mistakes.
When Should You Shard?
Sharding should be a last resort, not a first instinct. Before considering it, exhaust vertical scaling options: add more RAM, use faster SSDs, optimize queries, add read replicas, implement caching layers. Shard only when your dataset outgrows a single machine's storage capacity, when write throughput exceeds what a single primary can handle, or when your application requires data locality across geographic regions.
Signs that you may need sharding include: tables exceeding hundreds of millions of rows with sustained high write rates, single-node CPU or I/O saturation despite query optimization, and regulatory requirements demanding data residency in specific regions.
Sharding Strategies
Key-Based (Hash) Sharding
Hash sharding applies a hash function to a chosen shard key (such as a user ID) and uses the result to determine which shard stores the record. This approach distributes data evenly across shards, preventing hotspots.
// Determine shard for a user
function getShard(userId: string, totalShards: number): number {
const hash = murmurhash3(userId);
return hash % totalShards;
}
// Example: 4 shards
// User "abc123" -> hash 2948172 -> shard 0
// User "def456" -> hash 7391045 -> shard 1
The downside is that adding or removing shards requires rehashing and redistributing data. Consistent hashing mitigates this by minimizing the number of keys that need to move when the shard count changes.
Range-Based Sharding
Range sharding partitions data based on value ranges of the shard key. For example, orders from January–March go to shard 1, April–June to shard 2, and so on. This strategy works well when queries naturally filter by the shard key's range, enabling shard-local query execution without scatter-gather operations.
The risk is uneven distribution. If recent data receives disproportionate traffic, the shard holding the latest range becomes a hotspot. This can be mitigated by combining range sharding with time-based rotation of write-active shards.
Directory-Based Sharding
A lookup table maps each shard key to its corresponding shard. This provides maximum flexibility—data can be moved between shards by updating the directory without changing the application's sharding logic. The trade-off is that every query must first consult the directory, adding latency and creating a potential single point of failure. Caching the directory and replicating it across regions helps mitigate these issues.
Cross-Shard Queries and Joins
Once data is sharded, queries that span multiple shards become expensive. Cross-shard joins are generally impractical at scale. Design your data model so that entities accessed together reside on the same shard. For example, store a user's orders on the same shard as the user record. If cross-shard aggregation is necessary, consider a denormalized analytics store or a dedicated OLAP system that aggregates from all shards.
Rebalancing and Operational Complexity
As data grows unevenly, shards need rebalancing. Automated rebalancing tools like Vitess (for MySQL) or Citus (for PostgreSQL) simplify this process. Without automation, rebalancing is a manual, error-prone operation that requires careful coordination to avoid data loss or inconsistency.
Alternatives to Sharding
- Read replicas: Scale read-heavy workloads without sharding writes.
- Partitioning: PostgreSQL native table partitioning keeps data on one server but improves query performance and maintenance on large tables.
- NewSQL databases: Systems like CockroachDB and TiDB offer automatic horizontal scaling with SQL semantics, abstracting away manual sharding.
Sharding is a powerful tool when applied correctly, but it transforms a simple database into a distributed system with all the associated complexity. Evaluate your growth trajectory carefully, and consider managed solutions before building custom sharding infrastructure. At Nexis Limited, we help teams design data architectures that scale efficiently without over-engineering early.