Partitioning Strategies: Range vs Hash

How a database splits data across machines by key range or by key hash, what each does to range queries and hot spots, and how compound keys give you some of both.

Overview

Partitioning (also called sharding) splits one large dataset into pieces, called partitions, and puts different partitions on different machines. Each record belongs to exactly one partition. The point is to spread both the data and the work: with ten machines, each stores about a tenth of the data and handles about a tenth of the reads and writes.

That only works if the split is even. If one partition gets much more data or traffic than the others, it becomes a hot spot and limits the whole system. So the core question is how to map a key to a partition so that load spreads evenly, while the queries you need still touch as few partitions as possible. There are two basic answers, by key range and by key hash, plus a compound key that mixes them.

Partitioning is usually combined with replication: each partition has copies on several machines. This topic is about how keys are split; how copies are kept in step is covered in the replication topics.

Partition by key range

Sort the keys and give each partition one continuous range, like the volumes of an encyclopedia. To find a key, look up which range contains it.

Picture names sorted from aaron to zoe and split four ways: P1 holds a to f, P2 holds g to m, P3 holds n to r, and P4 holds s to z. Each partition owns a continuous slice of the sorted keys. In a real system the boundaries follow the data, not the alphabet, as the next paragraphs explain.

Because keys stay in order, a query for all keys between two values reads only the partitions that cover that range. That is ideal for time ranges, prefixes and sorted listings. Bigtable keeps rows in key order and splits a table into row ranges called tablets; HBase calls its ranges regions and splits a region in two when its files pass a size limit (10 GiB by default).

The risk is uneven load. The boundaries must follow the actual data (far more names start with S than with X), so good systems choose them automatically by splitting large ranges. And some key patterns send all new writes to one range. The classic case is a timestamp key: every write carries the current time, so every write lands in the last partition while the others sit idle.

Key concepts

  • Partition (shard)
  • Range partitioning
  • Hash partitioning
  • Consistent hashing
  • Virtual nodes (tokens)
  • Compound primary key
  • Hot spot
  • Scatter/gather

Interview questions

  • How would you partition a time-series store for millions of sensors?
  • Why does hash mod N move so much data, and what do real systems do instead?
  • When would you choose range partitioning over hash partitioning?
  • A celebrity with 50 million followers posts, and one partition melts. What do you do?

Sign up free to read the full topic

More DDIA topics

Start free on Cariara · Coding problems · System design studio