Range, Hash and Directory
Every partitioned store answers one question on every request: which partition holds this key. There are three ways to answer it. Range partitioning gives each partition a slice of the key order, hashing scatters keys by their hash, and a directory looks each key up in a table.
Each one makes one thing cheap and something else expensive, so choosing among them is choosing which query the design can afford to make slow. The one-machine versions of the first two, the B-tree and the hash table, are taught in Computer Science Deep Dive; here the same ideas are spread over machines.
By Range
Each partition owns a contiguous slice of the sorted keys, and a router holds the boundaries between slices. A scan of neighbouring keys reads one partition or two, and that is the reason to choose it: a run of IDs or a day of events comes back from one place.
The cost is that load follows the keys. Keys that arrive in order, such as timestamps or counters, all land in the last slice, which takes every new write while the others sit idle. The fourth lesson of this chapter calls this a moving hot spot. Slices split as they grow, so the partitions stay near one size; HBase regions and Bigtable tablets are range partitions that split this way.
By Hash
The key's hash picks the partition, so neighbouring keys land far apart and load spreads evenly whatever the keys look like. A point read is one partition. A range scan is every partition, because the order is gone.
The plain way to turn a hash into a partition is the remainder after dividing by the partition count. It moves almost every key when the count changes. Consistent hashing, the next lesson, is the fix.
Hash, Then Sort
A compound key hashes its first part to choose the partition and keeps rows sorted by the second part inside it. One user's messages, or one hotel's nights, then sit together and in order, so a lookup and a scan within one entity both stay on one partition. Cassandra's partition key with clustering columns has this shape, and so does DynamoDB's partition key with a sort key.
The cost is a ceiling: in Cassandra, one entity can never be bigger or busier than one partition.
By Directory
A lookup table maps each key, or each tenant, to its partition. It is the one scheme in which a single key moves alone: a large tenant gets a partition of its own by changing one row of the map.
The cost is a service on every request path. The map is replicated and cached by every router, and a stale cached entry has to fail safely, with a redirect or a retry, instead of sending a request to the wrong partition. Such a map usually lives in a consensus store, which Chapter 5 covers. Use a directory when tenants differ widely in size; skip it when every key is alike.
Secondary Indexes, Local or Global
Partitioning by one attribute leaves every other attribute unindexed across the cluster. Orders partitioned by order ID say nothing about where one customer's orders are. There are two ways to index that second attribute.
A local index lives in each partition and covers only that partition's rows. A write touches one partition. A read by the indexed attribute asks every partition and waits for the slowest of them, so one slow partition sets the latency of the whole read.
A global index is partitioned by the indexed value itself. A read asks one index partition, but a write touches the row's partition and the index's, so the index is usually updated asynchronously and lags behind. DynamoDB's global secondary indexes are this kind, and its documentation says plainly that they are updated asynchronously. Its local secondary indexes are narrower than the name: they keep the table's partition key and only sort each key's rows another way.
Choose local when lookups by the second attribute are rare. Choose global when they are frequent and a moment of lag is acceptable.
Range keeps keys in order, so scans are cheap, and keys that arrive in order pile onto one partition. Use it when the product reads ranges of the key and new keys arrive spread across the key space.
Hash spreads any keys evenly and gives up order, so a scan touches every partition. Use it when access is by exact key, or put the hash on the first part of a compound key and keep the order in the second.
How Interviewers Probe This
The first probe is "Why did you hash instead of using ranges?" A good answer names the access pattern: lookups by exact key and no ordered scan, or a scan kept inside one entity by a compound key. Then it says what the choice gives up.
The second is "You partitioned orders by order ID. How do you list one customer's orders?" A good answer prices both kinds of index, every partition against a lagging write, and ties the choice to how often the listing runs. It may offer a second table keyed by customer instead. You might say: "Customers open that list on every visit, so I'd keep a global index by customer and accept that a new order shows up a moment late."
The third is "Your directory is down. What happens?" A good answer says that routers keep serving from their cached map, that only moves and new tenants stop, and that the map lives in a replicated consensus store.
- "Hashing removes hot spots." It spreads different keys. One key that takes a tenth of the traffic still lands on one partition, and the fourth lesson of this chapter is about that key.
- "Even range boundaries mean even load." Boundaries even in key space are not even in traffic: the newest slice takes every new timestamp while the old ones sit idle.
- "A secondary index works the same once the data is split." A local index turns a lookup into a query to every partition, and a global one turns a write into two writes that disagree for a moment.
- "A directory is a single point of failure." Replicated and cached by every router, it is a small map that rarely changes. Its real cost is one more component, priced against moving a tenant alone.
- "Hash modulo the node count is good enough." It is, until the count changes and nearly every key moves at once.
- Choose range only when the product reads ranges of the key, and say how new writes avoid the newest slice.
- Hash the entity's identifier and sort by a second part when one entity's rows are read together.
- Use a directory when single tenants vary widely in size and must move alone, and say where the map lives and how routers cache it.
- For each query that does not name the partition key, say whether it asks every partition through a local index or one partition of a global index that lags, and how often it runs.
Knowledge Check
Which placement makes a scan of neighbouring keys read one or two partitions instead of all of them?
- Hash partitioning, where the key's hash spreads neighbours evenly
- A directory, where every key is looked up in a replicated table
- Range partitioning, where each partition owns a slice of sorted keys
- Modulo placement, where the key's remainder picks the partition
Why does a hash-partitioned store turn a range scan into a query to every partition?
- Every partition keeps a replica of the range index and has to confirm it
- Neighbouring keys hash to unrelated partitions, so their order is lost
- The hash changes whenever partitions are added, so keys must be found again
- A scan needs a lock on every partition so that no write slips into the range
A chat store hashes the conversation ID as the first part of its key and sorts by message sequence number as the second. What stays on one partition?
- One user's messages, across all of the conversations they are in
- All messages sent in the same second, across every conversation
- One conversation's messages, stored in the order of their sequence numbers
- Each message on its own, scattered by the hash of its sequence number
Orders are partitioned by order ID. Customers open their order list on every visit, and a new order may show up a second late. Which index fits?
- A global index by customer, read from one index partition, that lags
- A local index in each partition, asked on every visit and waited on
- No index at all, with a scan of every partition on each visit
- A directory mapping each customer to the partition with their orders
What does a directory buy over hashing, and what does it cost?
- An even spread of any keys, at the price of scans across every partition
- Cheap scans of neighbouring keys, at the price of a hot newest slice
- No hot keys at all, at the price of a second write for every change
- One key or tenant can move alone, at the price of a map on every request
You got correct