Picking the Partition Key
The partition key is the decision in a partitioned design that is most expensive to change. Every row is placed by it, and every query either names it or pays to ask everywhere.
The access pattern decides it. List what the product reads and writes, rank the list by rate and by what may never be slow, and choose the key that keeps the top of the list on one partition. Then price each query that is left over, out loud, because the interviewer will ask for the one the key made expensive.
Start From the Queries
The key is the attribute that the most frequent and most important access names. It also needs many distinct values with none dominant, so a country code or a status fails: a handful of values makes a handful of partitions, and the largest of them runs hot. With ranges, its values must not arrive in order, for the reason the previous lesson gave.
The usual wrong turn is to read the key off the entity diagram instead of the query list. An order has an order ID, so the orders get keyed by it, and then the query behind every page view turns out to name the customer.
Pricing the Crossing
A query that does not name the key goes to every partition, does work on each, and waits for the slowest. A write that changes rows on two partitions needs a transaction across them, which Chapter 5 teaches.
There are three ways to pay. Accept the fan-out when the query is rare and may be slow. Keep a second copy keyed the other way, a global index or a second table, at the price of a second write and a lag. Or compute the answer off the request path, with the messaging tools of Chapter 6. The query's rate and its staleness budget choose among them.
The URL Shortener
In Chapter 7, every redirect and every create names the short key, so the partition key is the short key, hashed. No query in the agreed scope crosses partitions.
Neither rate needs more than one partition. About 200 writes a second at peak is inside one partition's 1,000, and about 200 store reads a second, after the cache, is inside its 3,000. The bytes decide: 9 TB of links needs at least 5 partitions at 2 TB each, and 27 TB with three copies is about 14 nodes.
The trap is ranges over the raw counter, which send every new link to the last partition. Links by user are out of scope; if the interviewer adds them, they become a second table keyed by user.
Chat
In Chapter 18, the hot read is one conversation's recent messages in order, and every send appends to one conversation. The key is the conversation ID, with the sequence number inside the conversation as the sort key: the hash-then-sort shape from the second lesson. History and appends stay on one partition.
The query it makes expensive is a user's list of conversations, which names a user. That list gets its own table keyed by user, one small row per conversation, and each send updates the row of every member: a timestamp per member, not a copy of the message. A conversation that lives for years grows without end on one partition, so its key becomes the conversation plus a time bucket.
Keying the messages by user is rejected. Every send would copy the whole message once for each member, which is fan-out on write, the subject of Chapter 17. Groups of a hundred thousand members are Chapter 18's curveball.
Hotel Reservation
In Chapter 23, a booking checks and lowers the counts for one hotel's room type over a run of nights, inside one transaction. The key is the hotel ID, with room type and night as the sort key, so each booking's rows sit on one partition and the transaction stays local.
Keying by date is rejected: tonight's partition takes every hotel's bookings, and a three-night stay can cross three partitions. The hotel key has its own cost, which is search. Search by city and dates runs at a hundred times the bookings and spans many hotels, so it reads a separate copy of availability that may be stale, while the booking path stays exact. That is the third hard part of Chapter 23.
A flash sale at one hotel is a hot partition that salting cannot fix, because the count must stay exact. Chapter 23 takes that up as a curveball.
A second table keyed by the other attribute answers its query from one partition, and costs a second write on every change and a moment in which the two disagree. Use it when the query runs often or must be fast.
Scatter-gather asks every partition and merges, and costs work on each and the latency of the slowest. Use it when the query is rare, internal, or allowed to take seconds.
How Interviewers Probe This
The first probe is "What is your partition key, and what does it make expensive?" A good answer derives the key from the top access pattern, names the leftover query and its rate, and gives the payment chosen with its cost. You might say: "Conversation ID, because opening a chat and sending to it are the hot paths. That makes a user's list of chats expensive, so I'll keep a second table keyed by user and update each member's row on every send."
The second is "The product now wants lookups by another attribute. What changes?" A good answer keeps the key and adds a second table or a global index with its lag, or a scatter-gather if the lookup is rare.
The third is "Why not partition by user?" A good answer tests that key against the hot query. Where the queries are per user, it agrees. In chat, it shows the conversation spread over all of its members.
- "The primary key is the partition key." The primary key names a row; the partition key decides which rows live together. With a compound key, the two differ.
- "A good key spreads load evenly, and that is all." Spread is necessary. The key must also keep the hottest query on one partition, or that query becomes a gather on every request.
- "User ID is always a safe key." It is right when the queries are per user. In chat or booking, the hot query names a conversation or a hotel.
- "Cross-partition queries are forbidden." They are priced. A rare report may scatter; the query behind every page load may not.
- "The key can be changed later." Changing it moves every row and rewrites every query, a migration run while the old key keeps serving.
- Write the access patterns with their rates before naming the key, and pick the key the top one names.
- Name the query your key makes expensive before the interviewer does, and say how you pay for it.
- Use a compound key when one entity's rows are read together in order, and add a time bucket when the entity grows without end.
- Keep a transaction's rows under one key wherever the product allows it, and where it does not, name the protocol for transactions across partitions.
Knowledge Check
A ride app's hottest read fetches one trip's location updates in order, and every update appends to one trip. Which key fits?
- The driver ID, hashed, so each driver's trips sit together in one place
- The update's timestamp, in ranges, so the newest updates are easy to find
- The trip ID, hashed, with the sequence number of each update as the sort key
- The city, hashed, so all of the trips in one area land on one partition
Why does a status field with five possible values make a poor partition key?
- Five values make at most five partitions, and the busiest one runs hot
- Short values hash poorly, so the rows cluster on a few of the nodes
- A partition key has to be unique, naming exactly one row of the table
- A status cannot be indexed, so every query has to ask all partitions
Messages are keyed by conversation ID. Which query does that key make expensive?
- The latest messages of one conversation, read in sequence order
- Appending a new message to the end of one conversation's history
- A user's list of conversations, which names a user, not a chat
- Reading one message by its conversation and its sequence number
Orders are keyed by order ID. A finance report needs every order above an amount, runs once a night, and may take minutes. How should it be served?
- A second table keyed by amount, written alongside every order
- A scatter-gather over every partition, run once a night
- A global index by amount, updated with every order write
- A new partition key by amount, migrated before it runs
Why does keying hotel inventory by hotel ID keep a booking on one partition?
- Hotel IDs are assigned in order, so new bookings go to the newest slice
- Each night of a stay hashes to one partition, whatever the key is
- The hotel ID is unique, so no two bookings ever touch the same row
- Every count a booking touches, for each night, shares the hotel ID
You got correct