Database Sharding Lifecycle Visualization
Walk through the Database Sharding lifecycle with guided steps for hash sharding, range sharding, scatter-gather, hotspot, rebalance.
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
This component exists to initiate operations and supply the shard keys needed to locate target database nodes. It submits read and write commands containing shard keys to the sharding gateway proxy. In the demo, observe visual query packets originating here and traveling to the router.
Router (Sharding Proxy)
This component exists to hide the partition details from clients, presenting the cluster as a single logical database. It intercepts client commands, extracts the shard key, consults the lookup mapping, and forwards the query to the correct physical shard. In the demo, observe the router changing its target selection depending on the query type (point vs range).
Shard Map
This component exists to store key-to-node routing rules consistently so that query targets can be resolved quickly. It maps hash values, slots, or range boundaries to physical connection strings. In the demo, watch the mapping highlight path during shard resolution steps.
Hash Strategy
This component exists to distribute write operations evenly across databases to prevent server overload. It calculates the hash coordinate of shard keys and wraps them modulo-style to match server partitions. In the demo, watch the uniform distribution of queries and data across shards, avoiding load skew.
Range Strategy
This component exists to keep contiguous keys on the same database node, allowing fast range scans without querying all shards. It divides key values into ordered segments (like alphabetical or date ranges) and matches them to shards. In the demo, observe range queries flowing directly to specific target shards holding the requested values.
Shards (A, B, and C)
This component exists to partition large datasets across multiple servers, avoiding single-disk storage bottlenecks. They run database storage engines locally to process and store key-value partitions independently. In the demo, watch queue lengths, health states (online/offline), and storage capacity metrics changing on each card.
Scatter-Gather Coordinator
This component exists to query multiple partitions in parallel when queries do not include the shard key. It fans out queries to all shards concurrently and aggregates the individual responses. In the demo, observe multi-point query lines branching out to all shards simultaneously.
Hotspot Monitor
This component exists to collect traffic and capacity metrics across shards to trigger rebalancing before servers crash. It measures query load skew and alerts administrators or rebalance processes when a shard exceeds capacity thresholds. In the demo, watch the target shard cards highlight in amber or red to indicate traffic skew.
Rebalance Worker
This component exists to split overloaded shards and migrate data blocks dynamically to maintain partition balance. It divides range intervals, transfers records to under-utilized shards, and updates the global shard map. In the demo, watch visual data transfer blocks migrating between shard nodes during rebalancing.
Metadata Store
This component exists to store shard mapping configurations with strong consistency guarantees to prevent routing drift. It writes mapping changes durably and broadcasts routing updates to sharding routers. In the demo, watch the routing update event trigger router cache invalidation.
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.