Rebalancing Partitions
How a partitioned database moves data when machines join or leave: fixed, dynamic and per-node partitioning, moving a partition without downtime, and how clients find the new owner.
Overview
A partitioned database does not stay the same size. Data grows, traffic grows, machines fail and get replaced. Rebalancing is moving partitions between machines so that data and load are even again. It has three goals: after the move, load is spread fairly; during the move, reads and writes keep working; and no more data moves than necessary, because every byte copied competes with real traffic for disk and network.
The main design choice is how keys map to partitions, and how partitions map to machines. If you keep the first mapping stable and only change the second, a rebalance is just copying whole partitions. That idea underlies all three strategies below.
Keep the key-to-partition map stable
Think of a lookup as two hops. A fixed rule, such as hash(key) to a partition number, sends a key to its partition. A small assignment table then sends that partition to a node. Rebalancing edits only the table.
The rule to avoid is hash(key) % number_of_nodes. It mixes the two mappings, so changing the node count changes the partition of almost every key: going from 10 to 11 nodes moves about 91% of keys (see Partitioning Strategies).
Key concepts
- Rebalancing
- Fixed partition count
- Dynamic partitioning
- Pre-splitting
- Partitions proportional to nodes
- Copy, then switch
- Request routing
- Hash slot
Interview questions
- What happens to reads and writes while a partition is being moved?
- You have 1,000 fixed partitions on 10 nodes and add one node. What moves?
- Fixed or dynamic partitioning: how do you choose?
- Should rebalancing be automatic?
Sign up free to read the full topic
More DDIA topics
- MapReduce
- Modes of Dataflow: Databases, Services, Messages
- Multi-Leader Replication
- OLTP vs OLAP
- Ordering and Causality
- Partitioning Strategies: Range vs Hash
- Relational vs Document Data Models
- Reliability, Scalability, and Maintainability
- Replication Lag and Consistency Anomalies
- Schema Evolution and Backward/Forward Compatibility
- Schema-on-Read vs Schema-on-Write
- Secondary Indexes and Index Trade-offs
Start free on Cariara · Coding problems · System design studio