Sina Vafadar

Partitioning

Nov 3, 2026 · 24 min

Designing Data-Intensive Applications · Chapter 6 · Recall lab

Chapter 5 put a copy of the whole dataset on every node. That stops working when the data is too big for one machine, or the query throughput too high for one. The answer is to break the data up into partitions, so that each piece of data belongs to exactly one partition, and then put different partitions on different nodes. Each partition is a small database of its own, although the database may support operations that touch several at once.

The premise § Chapter opening

Spread the data, and with it the load

The main reason for wanting to partition data is scalability. In a shared-nothing cluster, different partitions can be placed on different nodes, so a large dataset can be distributed across many disks, and the query load across many processors. Queries that touch a single partition can be executed independently on their own node, so throughput scales by adding nodes.

What a partition is called depends on whose manual you are reading, which is most of the confusion around this chapter: a shard in MongoDB, Elasticsearch and SolrCloud, a region in HBase, a tablet in Bigtable, a vnode in Cassandra and Riak, a vBucket in Couchbase. The book says partition. The idea is old: partitioned databases were pioneered in the 1980s by products such as Teradata and Tandem NonStop SQL, and rediscovered more recently by NoSQL databases and Hadoop-based data warehouses.

Two ideas, combined § Partitioning and Replication

Every partition is also replicated

Partitioning is usually combined with replication, so that copies of each partition are stored on several nodes. Each record still belongs to exactly one partition, but it may be stored on several nodes for fault tolerance. A node may store more than one partition, and with leader-follower replication, a node can be the leader for some partitions and a follower for others.

Four partitions on four nodes, three copies each

node 1P0leaderP2followerP3follower
node 2P0followerP1leaderP3follower
node 3P0followerP1followerP2leader
node 4P1followerP2followerP3leader
4 partitions, 4 nodes, 3 copies of eachEvery node is the leader for one partition and a follower for 2 others. Partitioning decides which partition a record belongs to; replication, chapter 5, decides where that partition’s copies live. The choice of partitioning scheme is mostly independent of the choice of replication scheme, so this chapter mostly ignores replication.
The central decision § Partitioning of Key-Value Data

Which partition does a key go to?

The goal is to spread the data and the query load evenly across nodes. If the partitioning is unfair, so that some partitions have more data or queries than others, it is skewed, which makes it much less effective. A partition with disproportionately high load is a hot spot. Assigning records to nodes at random would spread them evenly, but then a read would have no way of knowing where a record is, and would have to ask every node.

The chapter’s running case is a network of sensors, keyed by timestamp. Below are six sensors, two days of history, and today’s readings arriving as writes, placed by four schemes. Each chip is one reading, day·hour sensor; today’s are tinted, and birch’s readings on 11-02 are outlined, because the panels also count what it costs to fetch them. Hashes are real MD5.

One sensor network, four partitioning schemes

P0start → 11-01 12h aspen01·00aspen01·00birch01·00cedar01·00dune01·00ember01·00fjord01·06aspen01·06birch01·06cedar01·06dune01·06ember01·06fjord0 of today’s writes
P111-01 12h aspen → 11-02 00h aspen01·12aspen01·12birch01·12cedar01·12dune01·12ember01·12fjord01·18aspen01·18birch01·18cedar01·18dune01·18ember01·18fjord0 of today’s writes
P211-02 00h aspen → 11-02 12h aspen02·00aspen02·00birch02·00cedar02·00dune02·00ember02·00fjord02·06aspen02·06birch02·06cedar02·06dune02·06ember02·06fjord0 of today’s writes
P311-02 12h aspen → end02·12aspen02·12birch02·12cedar02·12dune02·12ember02·12fjord02·18aspen02·18birch02·18cedar02·18dune02·18ember02·18fjord03·00aspen03·00birch03·00cedar03·00dune03·00ember03·00fjord03·06aspen03·06birch03·06cedar03·06dune03·06ember03·06fjord12 of today’s writes
Key range · timestamp

Like the volumes of an encyclopedia: each partition owns a continuous range of keys, here timestamps, with boundaries drawn where the existing data splits evenly. Keys stay sorted within a partition, so a day is one contiguous scan. But the key is the time, so every write that happens today falls in the last range.

All 12 of today’s writes land on one partition. It is a hot spot, and the other 3 sit idle.

Every sensor on 11-02: 2 partitions (P2, P3), 1 scan, 24 readings read to return 24.
birch on 11-02 (outlined): 2 partitions (P2, P3), 1 scan, 24 readings read to return 4.

Partitioning by key range

Like the volumes of a paper encyclopedia: each partition owns a continuous range of keys, from some minimum to some maximum, and if you know the boundaries you know which partition holds a key. The ranges are not necessarily even, because the data is not: they are chosen by an administrator or automatically, to follow it. Within each partition keys are kept sorted, so range scans are easy. Bigtable, its open source equivalent HBase, RethinkDB, and MongoDB before version 2.4. The weakness is access patterns that hit keys close together: with timestamps as keys, 12 of today’s 12 writes land on one partition.

Partitioning by hash of key

A good hash function takes skewed data and makes it uniformly distributed. It need not be cryptographically strong: Cassandra and MongoDB use MD5, Voldemort uses the Fowler–Noll–Vo function. It does need to give the same answer in every process, which rules out Java’s Object.hashCode() and Ruby’s Object#hash. Each partition owns a range of hashes. The price: adjacent keys are scattered, so the range query that cost the key-range scheme one scan costs a scan of all 4 partitions here.

Consistent hashing, as defined by Karger et al., is a way of evenly distributing load across an internet-wide system of caches such as a CDN, using randomly chosen partition boundaries to avoid central control or distributed consensus. The word consistent has nothing to do with replica consistency or ACID consistency; it describes a particular approach to rebalancing. That approach does not work very well for databases and is rarely used in practice, so the book avoids the term and says hash partitioning.

When hashing is not enough § Skewed Workloads and Relieving Hot Spots

Every write is to the same key

Hashing a key reduces hot spots, but it cannot avoid them entirely. In the extreme case where every read and write is for the same key, every request still goes to the same partition. The book’s example is a celebrity on a social media site with millions of followers: when they do something, a storm of activity hits one key, the celebrity’s user ID or the ID of the action being commented on.

A celebrity's key, and the same key split into many

400 ordinary keys, 1 write/s each user:celebrity, 800 writes/s

51P051P1849P253P349P464P543P640P7
One hot key

One partition carries 849 writes a second, 5.7× the average. Hashing cannot help: every write is to the same key, so they all hash to the same partition.

A read of the key is one lookup.

Today, most data systems are not able to automatically compensate for such a highly skewed workload, so it is the application’s responsibility to reduce the skew. The simple technique: if one key is known to be very hot, add a random number to the beginning or end of it. Just a two-digit decimal number splits the writes across 100 different keys, which can be distributed to different partitions. Perhaps in the future, data systems will detect and compensate automatically; for now, think through the trade-offs for your own application.

The harder half § Partitioning and Secondary Indexes

An index that doesn't follow the primary key

So far every record was found by its primary key, which determines its partition. A secondary index usually doesn’t identify a record uniquely; it is a way of searching for occurrences of a value: all actions by user 123, all articles containing the word hogwash, all cars whose color is red. They are the bread and butter of relational databases, common in document databases, and the raison d’être of search servers such as Solr and Elasticsearch. And they don’t map neatly to partitions.

The book’s example is a website for selling used cars. Every listing has a unique document ID and is partitioned by it, IDs 0 to 499 on partition 0 and 500 to 999 on partition 1, and users search by color and make, so both are indexed. There are two ways to partition that index, and the panels below build both from the same six listings, then run one read and one write against each.

The used-car listings, indexed two ways

client find color:red→ partition 0 partition 1
partition 0docs 0–499 · index: its own docs
idcolormake
191redHonda
214blackDodge
306redFord
  • color:black → [214]
  • color:red → [191, 306]
  • make:Dodge → [214]
  • make:Ford → [306]
  • make:Honda → [191]
partition 1docs 500–999 · index: its own docs
idcolormake
515silverDodge
768redVolvo
893silverAudi
  • color:red → [768]
  • color:silver → [515, 893]
  • make:Audi → [893]
  • make:Dodge → [515]
  • make:Volvo → [768]
Scatter/gatherEach partition indexes only its own documents, so nothing about the red cars says which partition holds them. The query goes to all 2 partitions, each answers from its own index, and the client combines [191, 306, 768]. This is scatter/gather, and it can make reads on secondary indexes quite expensive: even in parallel, the query is as slow as the slowest partition.

Document-partitioned: local

Each partition maintains its own secondary indexes, covering only the documents in that partition. A write touches 1 partition. A read on the index must go to all 2 and combine the answers. Vendors recommend structuring the partitioning so that secondary index queries can be served from a single partition, which is not always possible, especially with several secondary indexes in one query, such as color and make at once.

Term-partitioned: global

One index covering data in all partitions, itself partitioned, but by the term rather than by document: the term being searched for decides which partition of the index to ask. (The name comes from full-text indexes, where the terms are the words in a document.) A read asks 1 partition; a write may touch 2. Riak’s search feature and Oracle’s data warehouse, which lets you choose local or global, use it.

Scatter/gather is worse than it sounds, because a query sent to every partition is only finished when the last one answers. That is tail latency amplification, from chapter 1:

Four partitions answer; the client waits for the slowest

ClientPartition 0Partition 1Partition 2Partition 312 ms9 ms41 ms11 msdone at 41 ms

The median partition answered in 11 ms. The query took 41.

Things change § Rebalancing Partitions

Moving load without moving everything

Over time, query throughput increases, so you add CPUs; the dataset grows, so you add disks and RAM; a machine fails, and others take over its share. All of these move data and requests from one node to another: rebalancing. Whatever the scheme, it should meet three requirements: afterwards the load is shared fairly; while it happens the database keeps accepting reads and writes; and no more data than necessary moves, so it is fast and light on the network and disks.

How not to do it: hash mod N

Why not simply assign key k to node hash(k) mod N? Because when N changes, most keys move. With hash(key) = 123456, that key is on node 6 of 10, node 3 of 11, and node 0 of 12. Going from 10 nodes to 11 moves 92% of the keys in the dial below, when moving 1 in 11 would have been enough.

Add or remove nodes, and count the keys that move

480 keys, one cell each · lit: changed node

46n143n244n348n438n543n630n760n840n943n1045n11
10 → 11 nodes

92% of the keys change node, when moving 9% would have been enough. The busiest node holds 60 keys, 1.38× the mean.

strategykeys moved, 10 → 11
hash mod N92%
1000 fixed partitions10%
256 partitions per node9%

A fixed number of partitions

Create many more partitions than there are nodes, say 1000 for a 10-node cluster, and give each node several. A new node steals a few whole partitions from every existing node until the spread is fair again. The number of partitions and the assignment of keys to partitions never change; only which node holds which partition does. More powerful nodes can be given more partitions. Riak, Elasticsearch, Couchbase and Voldemort. The hard part is picking the number up front: each partition has management overhead, but if partitions are very large, rebalancing and recovery from node failures get expensive.

Partitioning proportionally to nodes

Cassandra and Ketama give each node a fixed number of partitions, 256 by default in Cassandra. A joining node picks that many existing partitions at random, splits each, and takes one half, so partitions shrink as the cluster grows and their size stays fairly stable. Random splits can be unfair, but averaged over 256 of them a new node takes a fair share. Picking boundaries randomly requires hash partitioning, and this is the closest to Karger’s original consistent hashing.

Partitions that grow § Dynamic partitioning

Split when big, merge when small

For key-range partitioning, fixed boundaries are very inconvenient: get them wrong and all of the data can end up in one partition while the rest stay empty, and reconfiguring them by hand is tedious. So key-range databases such as HBase and RethinkDB create partitions dynamically: a partition that grows past a configured size splits in two, and one that shrinks below a threshold merges with a neighbour.

A key-range store, from empty to split and back

node 11 partitionstart → end
node 2idle
node 3idle
An empty databaseHBase and RethinkDB create key-range partitions dynamically, and an empty database starts with a single partition, because there is no a priori information about where to draw the boundaries.

Operations: automatic or manual rebalancing

Fully automatic rebalancing is convenient and unpredictable. Rebalancing is expensive: requests are rerouted and large amounts of data move, and done carelessly it can overload the network or the nodes. Combined with automatic failure detection it can be dangerous: one node is overloaded and slow to respond, the others conclude it is dead and move load away from it, which puts more load on it, the other nodes and the network, and can cascade. Couchbase, Riak and Voldemort generate a suggested assignment automatically and wait for an administrator to commit it. Slower, and it can prevent operational surprises.

Finding the data again § Request Routing

Which node do I connect to?

The data is partitioned across nodes, and the assignment keeps changing as partitions rebalance. When a client wants to read user:42, which IP address and port does it connect to? This is an instance of a more general problem, service discovery, and there are three answers, which differ in who knows the assignment.

Three ways to reach the node that owns a key, and who keeps the map

round-robinforwardnode 1node 2node 3owns the keyclientget user:42
Contact any node, and let it forwardClients connect to any node, for example through a round-robin load balancer. If that node happens to own the partition it answers; otherwise it forwards the request to the node that does, waits for the reply, and passes it back. Cassandra and Riak work this way, and learn about changes in the cluster by a gossip protocol among the nodes: more complexity in the database, no dependency on an external coordination service.

Clients still need to find the IP addresses of the nodes or the routing tier, but those change far less often than the assignment of partitions to nodes, so DNS is usually enough for that part.

Parallel query execution

Everything so far has been a query that reads or writes a single key, plus scatter/gather for document-partitioned indexes. That is about the level of access most NoSQL distributed datastores support. Massively parallel processing (MPP) relational databases, often used for analytics, support much more: a typical data warehouse query has several joins, filters, groupings and aggregations, and the MPP query optimizer breaks it into execution stages and partitions, many of which run in parallel on different nodes. Queries that scan large parts of the dataset benefit most. Chapter 10 returns to it.

Recall check 12 questions

Did it stick?

One attempt per question; the explanation appears either way.

1. What is the main reason for partitioning data?

2. What is a hot spot?

3. Why does partitioning sensor readings by timestamp create a hot spot?

4. What does hash partitioning give up in exchange for an even spread?

5. In Cassandra, a table with primary key (user_id, update_timestamp). How is a row partitioned?

6. Why does the chapter avoid the term “consistent hashing”?

7. A celebrity’s key receives a storm of writes. What can the application do?

8. With a document-partitioned (local) secondary index, how is a query on that index served?

9. What is the downside of a term-partitioned (global) secondary index?

10. Why is hash mod N a bad way to assign keys to nodes?

11. With a fixed number of partitions, what changes when a node is added?

12. Why can fully automatic rebalancing be dangerous?

Answered 0 of 12 · 0 correct

Vocabulary Click to flip

The terms this chapter installs


Chapter 6 in one breath. Partitioning is necessary when you have so much data that storing and processing it on a single machine is no longer feasible. The goal is to spread the data and the query load evenly across several machines, avoiding hot spots, which means choosing a scheme that suits the data, and rebalancing when nodes are added or removed.

Two main approaches. Key range partitioning sorts the keys and gives each partition everything from some minimum to some maximum: range queries are efficient, and keys accessed close together in sorted order make hot spots. Partitions are typically rebalanced dynamically, by splitting a range in two when it gets too big. Hash partitioning applies a hash function to each key and gives each partition a range of hashes: the ordering is gone, range queries are inefficient, and the load may be spread more evenly. It is common to create a fixed number of partitions in advance, assign several to each node, and move whole partitions when nodes come and go. Hybrids exist, such as a compound key that uses one part to find the partition and another for the sort order.

A secondary index has to be partitioned too. Document-partitioned (local) indexes live with the documents: a write updates one partition, and a read scatters across all of them. Term-partitioned (global) indexes are partitioned by the indexed values: a write may update several partitions of the index, and a read can be served by one. Routing a query to the right partition runs from simple partition-aware load balancing to sophisticated parallel query engines. By design, each partition operates mostly independently, which is what lets a partitioned database scale to many machines. But operations that write to several partitions are hard to reason about: what if the write to one partition succeeds and the other fails? That is where the next chapters go.

Study aid built for recall practice. Concepts, examples, figures and named systems are Martin Kleppmann’s, from Designing Data-Intensive Applications, chapter 6. Go back to the book for his wording and the full reference list. Nothing in a caption here is typed in by hand: src/lib/ddia6.ts hashes every key with MD5, places it by each scheme, and counts what lands where. The sensor names and readings are invented; the used-car listings and the hash-mod-N example are the book’s, and the indexes and node numbers come out as they do in its figures.

← all lab notes