liveBook · Manning

Key hash partitioning

As explained in Latency

Key hash partitioning assigns records to partitions according to a hash of their partitioning-key values.

Key hash partitioning assigns records to horizontal partitions according to a hash of their partitioning-key values. Records with the same key go to the same partition. Spreading different keys across partitions can reduce hotspots and support lower-latency access, but the choice of key still matters. section 5.2.1

Figure 5.1 Horizontally partitioning a database table. On the left, you can see the full logical table with four rows. On the right side, you can see three horizontal partitions.
Figure 5.1: Example of horizontal partitioning of a database table. At the left side of the diagram, you see the full logical table with four rows. On the right side of the diagram, you see three horizontal partitions.

The table illustrates partitioning by Location: users in New York share a partition, while Sydney and Cape Town users occupy other partitions. section 5.2.1

Prefer a key with high cardinality, or combine columns to increase cardinality. A low-cardinality key creates too few distinct placements; skewed values can leave one partition much larger than the others. Hash partitioning can distribute unevenly spaced keys more effectively than key range partitioning, but range partitioning has an advantage when queries need range scans. section 5.2.1 section 5.5.1

Check partition size as well as key counts. Even partitions holding similar numbers of keys can differ substantially in size when their values differ—for example, when some partitions hold larger images. A poor scheme may require rebalancing every partition, which is difficult on a live system. section 5.5.1

Route a user-profile request

A partition-aware HTTP client could hash a user ID to choose the node holding that profile. The Rust REST API and client demonstration supplies node IDs and request routing, but its benchmark selects nodes round-robin; hashed user-ID routing and data partitioning are proposed extensions, not implemented results. Its lower tail latency with five nodes reflects parallel request handling, not a measured benefit of key hashing. section 5.6

How Latency covers it

How deep it goes
Defines placement and compares range partitioning, but defers implementation of hashed request routing.
Examples
Illustrates Location-based placement and proposes user-ID routing alongside a Rust/SQLite REST API benchmark that currently routes round-robin.
What it assumes
Assumes familiarity with database keys and partitions; introduces the hash-versus-range choice without teaching a hash-function implementation.
What you can do after
Evaluate/understand it when choosing a partition key or addressing hotspots, and identify where a partition-aware client needs hashed routing.
How it connects
Connects partition keys, cardinality, horizontal partitioning, key range partitioning, hot partitions, and request routing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Latency. The section links point to the parts of the book it draws on.