In this lesson · topic 6 of 7
Topic 6
Looking up by the column you did not partition on
6 min read
Partitioning by user_id makes “get user 42” a one-partition lookup. Then someone asks for all users with email = 'a@b.com', or all orders with status = 'late'. The data is not laid out for that.
A local index per partition
The first design gives each partition an index over only its own rows. A write updates the row and its index entry on the same machine, which stays cheap and needs no coordination.
A read by the indexed column is the problem. Matching rows could be on any partition, so the query goes to all of them and the results are merged. This is scatter and gather. Adding partitions does not make it faster, because each query still touches every one. It adds load, since every query now costs one request per partition.
A global index, partitioned by the indexed value
The second design builds one index over the whole table, and partitions it by the indexed value, not by the row key. A query for status = 'late' goes to the single index partition that owns that value. One or a few requests, not all.
Writes pay for it. Changing a row’s status updates the row on its own partition and the index on a different one. A row with three indexed columns can touch up to four partitions. Doing this atomically needs a distributed transaction, so most systems update the index asynchronously. DynamoDB’s global secondary indexes work this way, and its documentation says they are eventually consistent: a query on the index can briefly miss a write the table already acknowledged.
| Local index | Global index | |
|---|---|---|
| Write cost | one partition | row partition plus index partitions |
| Query by indexed column | every partition | one or a few |
| Freshness | consistent with the row | usually behind, updated asynchronously |
Fan-out and the tail
Scatter and gather has a subtler cost than the request count. A response is only as fast as the slowest partition. Suppose each partition answers within 100 ms 99 percent of the time. For one partition, 1 request in 100 is slow. Fan out to 8 partitions and a query is slow if any one is:
- 1 - 0.99^8 = 7.7 percent of queries exceed 100 ms
Fan out to 64:
- 1 - 0.99^64 = 47 percent of queries exceed 100 ms
So a rare delay on single-key reads becomes nearly a coin flip on queries that touch every partition. Doubling the partition count to relieve write load worsens this at the same time.
Predict first
Ways to reduce the pain
Tail-latency techniques apply. Send each partition request redundantly after a short delay, and take the first answer, a hedged request. Dean and Barroso describe this and related techniques.
A different fix is to partition the data so the common query does not need a second index. Putting the tenant first in the key, for instance, turns “orders for this tenant by date” into one range scan. Reserve secondary indexes for the queries that remain.
Sources
- Martin Kleppmann, Designing Data-Intensive Applications, O’Reilly, 2017. Chapter 6 covers partitioning and secondary indexes.
- Amazon Web Services, Using global secondary indexes in DynamoDB.
- Jeffrey Dean and Luiz André Barroso, The Tail at Scale, Communications of the ACM, 2013.