TracksSpecializations and Deep DivesAdvanced Database ConceptsSharding and Horizontal Scaling(5 of 7)

Sharding and Horizontal Scaling

Eventually, even the most powerful single database server hits its limits. When you've optimized queries, added indexes, implemented caching, and scaled up to the biggest machine available, sharding becomes the next option. It's powerful but comes with significant complexity.

What Is Sharding?

Sharding splits your data across multiple independent databases. Instead of one database holding all users, you might have ten databases, each holding a portion of users. Each portion is called a shard.

Without sharding:
  All data → Single Database

With sharding:
  Users 1-1M      → Shard 1
  Users 1M-2M     → Shard 2
  Users 2M-3M     → Shard 3

Your application must know which shard holds which data and route queries accordingly.

Sharding Strategies

Range-based sharding divides data by ranges. User IDs 1-1000000 go to shard 1, 1000001-2000000 to shard 2, and so on. Simple to understand, but can create hotspots if recent data is accessed more frequently.

Hash-based sharding uses a hash function: shard = hash(user_id) % num_shards. This distributes data more evenly but makes range queries across shards difficult.

Directory-based sharding maintains a lookup table mapping keys to shards. More flexible but adds a lookup step and another component to manage.

The Challenges Are Real

Sharding introduces problems that don't exist with a single database:

Cross-shard queries are expensive. If you need data from multiple shards, you query each one and combine results in your application.

Transactions across shards are extremely difficult. ACID guarantees don't naturally span multiple databases.

Rebalancing when adding new shards requires moving data around, which is complex and risky.

Application complexity increases significantly. Every query must know which shard to hit.

Try Everything Else First

Before sharding, exhaust other options:

  • Vertical scaling: Bigger server with more CPU, RAM, and faster storage
  • Read replicas: Distribute read load across multiple copies
  • Caching: Keep frequently accessed data in Redis or similar
  • Query optimization: Better indexes and query patterns

Many applications never need sharding. Those that do typically have millions of users or massive data volumes.

When Sharding Makes Sense

Sharding becomes necessary when you've genuinely outgrown a single server — typically at very large scale. If you're not sure whether you need it, you probably don't yet.

See More

Further Reading

Last updated December 26, 2025

You need to be signed in to leave a comment and join the discussion