Free preview

Partitioning: Vertical and Horizontal Sharding

Why this matters: replication gives you copies; partitioning gives you capacity. It is also the one database decision that is genuinely expensive to reverse, because the partition key determines the physical layout of every row.

Key takeaway

Data partitioning (or sharding) distributes data across multiple nodes, with subsets managed on each. The goal is balanced partitions that efficiently handle increasing query rates and data volume.

Why partition

As data volumes and concurrent traffic grow, single-node databases reach scalability limits and latency and throughput degrade. Traditional databases offer robust features — range queries, secondary indexes, ACID transactions — but maintaining those in a distributed environment is challenging.

At some point a single node becomes the bottleneck. The alternatives all cost something:

OptionCost
Migrate to NoSQLExpensive — many codebases are tightly coupled to relational databases
Adopt third-party scaling toolsAdds operational and architectural complexity
Partition the existing databaseOptimizes for the specific problem while preserving the relational model

The hotspot problem

Partitioning must be balanced. If one partition receives significantly more data or queries it becomes a hotspot — a single congested node absorbing the majority of traffic, degrading the whole system.

There are two ways to shard.

Vertical sharding

Vertical sharding moves specific tables or columns to different database instances or physical servers. It is often used to separate columns containing large text or binary data (blobs) from the main table to improve retrieval speed.

If an Employee table contains a large photo blob, split it into a lighter Employee table holding metadata and an EmployeePicture table holding the blob. Both retain the primary key EmployeeID so the data can be reconstructed efficiently.

Vertical sharding is often manual and static; horizontal sharding is better suited to automation and dynamic scaling.

Horizontal sharding

Horizontal sharding divides a table row-wise into multiple smaller tables. Each partition is a shard. Two primary strategies:

Key-range-based sharding

Each partition is assigned a continuous range of keys. An Invoice table partitioned on Customer_Id puts specific ID ranges in different partitions.

Shard 1: Customer_Id 1-2      Shard 2: Customer_Id 3-4

When multiple tables are linked by foreign keys, shard them on the same partition key so related data — a Customer and their Orders — stays on the same shard, keeping retrieval local.

Common design techniques for multi-table sharding:

TechniqueWhat it does
Mapping tableA table on each shard mapping partition keys to shards, so applications can route queries efficiently
Replicated partition keyThe partition key is included in all related tables, aiding routing and isolation at the cost of storage
Unique primary keysKeys must be unique across all shards to prevent collisions during migration or analytical merges
Consistency pointA column such as Creation_date serves as a merge point for a global view, assuming clocks are synchronized
AdvantagesDisadvantages
Key-range shardingRange queries are easy — you know exactly which node holds a key range; data stays sorted within partitionsRange queries only work on the partitioning key; uneven key distribution causes hotspots

Hash-based sharding

A hash function determines the partition: compute the hash of the key and take the modulus of the partition count.

partition = hash(key) mod n          where n = number of nodes

With n = 4, keys whose hash mod 4 equals 2 go to node 2, and so on. This randomizes placement to ensure uniform distribution.

AdvantagesDisadvantages
Hash-based shardingKeys are uniformly distributed across nodes, minimizing hotspotsRange queries are inefficient — keys are scattered randomly across all partitions

The two strategies are exact opposites: key-range preserves locality and risks skew; hashing destroys locality and guarantees spread.

Consistent hashing

Consistent hashing maps both servers and data keys onto positions on an abstract circle, or ring. This allows nodes to scale with minimal data movement, preserving performance.

AdvantagesDisadvantages
Consistent hashingFacilitates easy horizontal scaling; improves throughput and latencyRandom node assignment can lead to non-uniform data distribution

Its disadvantage is real and has a standard fix — virtual nodes, where each physical server owns many small arcs of the ring rather than one large one, which smooths the distribution. The Key-Value Store chapter covers that mechanism in depth.

Key takeaway

Choose key-range when range queries matter and you can guarantee even key distribution. Choose hashing when uniform spread matters more than locality. Choose consistent hashing when the node count will change — which, at any real scale, it will.

Interview signal by level

LevelWhat a strong answer sounds like
L4"We'd shard the database to spread the data."
L5Picks a strategy for a reason: "hash-based on user ID for even distribution, since we don't need range scans across users."
Staff+Leads with the hotspot risk and sizes it: "the partition key is the decision I can't cheaply reverse, so I'd check its distribution first — timestamps or country codes create hotspots. 10 TB at 50 GB per node is about 200 shards, and I'd use consistent hashing with virtual nodes so adding capacity doesn't reshuffle everything."

Next: what happens when you add or remove a node.

Enjoying the preview?

Create a free account to unlock the rest of this course, the in-browser judge, and live AI mock interviews.

Sign up free to continue