Databases advanced LIVE DEMO
πŸ—ƒοΈ

Database Sharding Lifecycle Visualization

Walk through the Database Sharding lifecycle with guided steps for hash sharding, range sharding, scatter-gather, hotspot, rebalance.

databaseshardinghash shardingrange shardingscalinghorizontal scalingvisualization
Interactive Demo

Continue with the guided workspace

Use the paired workspace to connect this guide's mental model with live component state, visible signals, and the outcome each step or scenario is meant to prove.

Database sharding splits one logical dataset across many physical nodes so a database can scale horizontally past the limits of a single machine. Each shard owns a disjoint slice of the data, a router directs each request to the correct shard, and a rebalance layer moves ranges when shards are added or hotspots appear.

For parameter experimentation, the Database Sharding Simulator sandbox is also available.

Mental Model

Database sharding is a horizontal scaling technique that partitions a single logical database into multiple physical database nodes (shards). Unlike replication, where every node holds a full copy of the data, sharding distributes subsets of data across nodes. The core challenge of sharding is routing: the system must decide which node holds a given piece of data. This routing is guided by a shard key and executed via either hash-based distribution (uniform dispersion) or range-based distribution (ordered segments).

Components and Why They Exist

Client

The client application initiates database queries and supplies the shard keys needed to locate target partitions. Query packets originate at the Client card and travel into the sharding router for resolution.

Router (Sharding Proxy)

The sharding router hides physical partition topology from application code, presenting the cluster as a single logical database. It extracts the shard key from incoming commands, consults the Shard Map, and switches its outbound route depending on whether the query is a point lookup, range scan, or scatter-gather request.

Shard Map

The shard map stores the authoritative key-to-node routing rules so the proxy can resolve target shards in microseconds. It maps hash slots or key ranges to physical shard addresses and highlights the matched lookup entry during routing steps.

Hash Strategy

The hash strategy applies a deterministic hash function to the shard key so writes spread evenly across all physical shards. Under hash routing, incoming write particles fan out uniformly across Shards A, B, and C without creating a single hot partition.

Range Strategy

The range strategy partitions contiguous keys onto the same shard so bounded range queries can execute without scanning the entire cluster. It divides the keyspace into ordered intervals and routes range queries directly to the shard holding that slice.

Shards (A, B, and C)

Shards A, B, and C partition the dataset across independent database servers so storage and write I/O scale past single-machine limits. Each shard card runs its own local storage engine and displays live queue depth, health status (online or offline), and key capacity metrics.

Scatter-Gather Coordinator

The scatter-gather coordinator handles queries that omit the shard key by querying every partition in parallel and merging the results. Multi-branch request lines fan out from the coordinator to all three shards simultaneously, waiting on the slowest shard before returning the merged response.

Hotspot Monitor

The hotspot monitor tracks per-shard request rates and storage skew so operators can rebalance before an overloaded node collapses. When write traffic clusters on a single partition, the monitor highlights the skewed shard card in amber or red.

Rebalance Worker

The rebalance worker splits overloaded ranges and migrates data chunks to underutilized shards to restore cluster balance. Data migration blocks stream between shard nodes while the worker splits the hot interval and updates partition ownership.

Metadata Store

The metadata store persists shard map configurations with strong consensus guarantees so routers never use conflicting partition tables. Once a rebalance finishes, the metadata store commits the updated range map and broadcasts a cache-invalidation event to the router.

Edge Cases and Misconceptions

  • Sharding is not replication: Sharding partitions data for write scalability, while replication duplicates data for high availability. In production, each shard is typically a replicated group (replica set) to avoid single points of failure.
  • Cross-shard joins are prohibited: Joins across shards are extremely expensive and often unsupported. Databases must denormalize data or design query access patterns to execute within a single shard.
  • The β€œauto-increment” timestamp trap: Using a sequential ID or timestamp as a range shard key causes every new write to hit the newest shard. This completely defeats the purpose of sharding by creating a single write bottleneck.

Common Questions

What is the difference between hash sharding and range sharding?

Hash sharding applies a hash function to the shard key to distribute writes uniformly across shards, preventing hotspots but making range queries inefficient. Range sharding partitions data based on ordered value boundaries, allowing efficient range queries but risking hot shards if writes cluster in a specific range (e.g., auto-incrementing IDs or recent timestamps).

Why is a scatter-gather query considered expensive?

Scatter-gather queries must run across every single shard in parallel because the query does not specify a shard key. The total latency is bound by the slowest responding shard (tail latency), and it consumes connection pool resources across the entire cluster.

What is a database sharding hotspot and how is it resolved?

A hotspot occurs when a single shard receives a disproportionate amount of read or write traffic (skew). It is resolved by splitting the range boundary (rebalancing) or using a hashed suffix on hot keys to spread the write load across multiple physical nodes.

Why is choosing a good shard key so critical?

A poor shard key cannot be easily changed later without rewriting the entire dataset. It leads to unbalanced shard sizes, frequent scatter-gather queries, or severe hotspots that limit horizontal scalability.

What happens to active transactions during a shard split?

During a split, the rebalance worker must lock or track writes to the range being moved. Highly advanced sharding layers use logical replication to mirror writes to the new shard, then briefly pause traffic (or redirect reads) during the final cutover.

Can hash sharding support range queries?

Yes, but they must be executed as scatter-gather queries. The router must query every shard in the system and merge the results, which is significantly slower than range routing.

How does consistent hashing differ from modulo-based hash sharding?

Modulo-based sharding (key % N) requires re-sharding nearly all keys in the database when a shard is added or removed. Consistent hashing maps keys and nodes to a circular ring, requiring only a fraction (1/N) of the keys to be migrated when cluster membership changes.