Database Sharding - System Design

Last Updated : 22 Oct, 2026

Database sharding is a technique used to distribute a large dataset across multiple database nodes. It helps increase storage capacity and distribute query or write workloads when a single database instance is no longer sufficient.

  • Data is divided into smaller partitions called shards.
  • Each shard stores a subset of the overall data.

Example: An e-commerce platform may distribute users across multiple database servers using user_id as the shard key.

database

Methods of Database Sharding

1. Hash-Based Sharding

Hash-based sharding applies a hash function to a shard key and uses the result to select the target shard.

For example: shard = user_id % number_of_shards

key_based_sharding

If there are three shards:

  • user_id = 10 -> Shard 1
  • user_id = 11 -> Shard 2
  • user_id = 12 -> Shard 0

Advantages

  • Can distribute records relatively evenly when the shard key has good distribution.
  • Simple routing logic.

Limitations

  • Adding or removing shards may require data redistribution.
  • Poor shard-key selection can create hotspots.

2. Range-Based Sharding

Range-based sharding distributes records according to ranges of the shard key.

Example:

  • Shard 1: Customer IDs 1–1000
  • Shard 2: Customer IDs 1001–2000
  • Shard 3: Customer IDs 2001–3000
2056958696

Advantages

  • Easy to understand and implement.
  • Efficient for range queries when the relevant range is stored on one shard.

Limitations

  • Sequential or skewed keys can create hotspots.
  • Some ranges may grow faster than others.

3. Directory-Based Sharding

Directory-based sharding uses a lookup service that maps each shard key to the shard where the data is stored.

pre_sharded_table

The directory can be updated when data moves or shards are added or removed.  

Advantages

  • Flexible data placement.
  • Makes shard movement and rebalancing easier.

Limitations

  • The lookup service adds an extra dependency and network hop.
  • It must be replicated or otherwise made highly available to avoid becoming a single point of failure.

4. Geographic Sharding

Geographic sharding distributes data according to geographic attributes such as country, region, or data-residency requirements.

Example:

  • Europe users -> EU database cluster
  • Asia users -> Asia database cluster
  • US users -> US database cluster

Advantages

  • Can reduce latency by storing data closer to users.
  • Can help satisfy data-residency requirements.

Limitations

  • User or workload distribution may be uneven across regions.
  • Cross-region queries and migrations can become complex.

Choosing a Good Shard Key

Shard-key selection is one of the most important parts of sharding design.

A good shard key should:

  • Distribute data evenly.
  • Avoid high-traffic hotspots.
  • Support common query patterns.
  • Have sufficient cardinality.
  • Avoid frequent changes.

Poor shard-key selection can cause one shard to receive far more traffic or data than others.

Keeping Shards Balanced

Several techniques can help maintain balanced data distribution:

  • Choose a well-distributed shard key: Avoid values that concentrate most traffic on a small number of shards.
  • Use Consistent Hashing: Can reduce the amount of data that must be remapped when nodes are added or removed.
  • Monitor Shard Usage: Track storage, request rate, latency, and CPU usage across shards.
  • Rebalance Data: Move partitions or records when some shards become significantly larger or busier.
  • Use Virtual Shards or Partitions: Logical partitions can make redistribution easier than mapping data directly to physical nodes.
  • Automate Rebalancing: Use database or infrastructure features that support automatic partition movement where available.

Challenges of Database Sharding

Sharding improves capacity but introduces distributed-database complexity.

  • Cross-Shard Queries: Queries involving multiple shards require additional coordination.
  • Distributed Transactions: Transactions spanning shards are more difficult to manage.
  • Rebalancing: Adding or removing shards may require moving large amounts of data.
  • Hotspots: Poor shard-key selection can overload particular shards.
  • Operational Complexity: Monitoring, backups, schema changes, and failover become more complex.
  • Joins and Aggregations: Operations across multiple shards may require application-level or distributed processing.
Comment

Explore