Database Sharding Patterns: Architecting Horizontal Scale-Out for Web SaaS

Scale beyond a single database. We explore hash sharding, directory-based routing, and cross-shard queries.

VP
SHIVAM ITCS
·25 November 2013·10 min read·1 views

The Limits of Single-Server Scaling

Even with read replicas and memory caching, a single database server hits hard write limits.

  • The Bottleneck: Disk I/O bottlenecks and write lock contentions.
  • The Solution: Database Sharding—partitioning your database tables across multiple independent servers.

Sharding database systems requires careful planning, as queries must route to correct nodes programmatically.

Scale Rule: Partition tables using a consistent shard key (e.g. TenantId or UserId) to distribute write operations evenly across servers.

Core Sharding Algorithms

Architects utilize specific partitioning patterns:

1. Range Sharding

Partitioning data based on ranges of values (e.g. Users A-M go to Shard 1, N-Z go to Shard 2).

  • *Con:* Causes hotspots if user registrations are uneven.

2. Hash Sharding

Applying a hash function to the shard key, dividing the result by the number of shards to locate the target database node:

csharpcode
// Shard routing calculation in C#
int shardIndex = Math.Abs(tenantId.GetHashCode()) % totalShards;
Sharding PatternOperational ProsOperational Cons
Hash ShardingDistributes data and writes evenly.Difficult to add new shards.
Directory RoutingFlexible. Shards can be moved dynamically.Requires database lookup on routing.

Managing Cross-Shard Queries

Queries that cross shard boundaries (e.g. listing all users globally) are slow. Avoid these by designing tables so that all related child records share the parent tenant key, localizing queries to a single shard database.

VP
Vijay Paliwal
Founder, SHIVAM ITCS · 18+ years enterprise & AI engineering
MCA · Ex-HiveGPT USA · Ex-Social27 Seattle
Database Sharding Patterns: Architecting Horizontal Scale-Out for Web SaaS | SHIVAM ITCS Blog | SHIVAM ITCS