Partition Key Selection
Partition key selection determines how evenly data and workload are distributed while preserving useful query locality. A good key limits hotspots and partition imbalance while allowing common queries to target as few partitions as practical.
A partition key determines which partition stores a row or record. In a hash-partitioned system, the key is typically transformed by a hash function and mapped to a partition; in range partitioning, key values are assigned to value intervals. The exact mechanism depends on the database or distributed storage system.
The key should distribute both data volume and request load. A key with low cardinality can concentrate many records into a small number of partitions, while a monotonically increasing key such as a timestamp or sequence can overload the newest range in range-partitioned systems. Evenly sized partitions are not sufficient if one partition receives most of the reads or writes.
Query patterns matter as much as distribution. If an application frequently retrieves all records for one customer, using `customer_id` can keep those records together and make partition pruning or targeted routing possible. However, if one customer can generate disproportionate traffic, that same locality can create a hotspot.
For high-volume workloads, consider both average distribution and worst-case behavior. Composite keys, hashing, bucketing, or adding controlled sharding can spread hot workloads, but these techniques may make queries more complex. The correct choice therefore comes from workload characteristics rather than from a universally ideal column.
Key points
- Choose a key that distributes storage and request load across partitions.
- Check cardinality, skew, and whether particular values can become hotspots.
- Align the key with frequent access patterns so queries can target relevant partitions.
- Beware monotonically increasing keys with range-based partitioning.
- Use composite keys, bucketing, or controlled sharding when locality conflicts with load distribution.
Interview questions
How would you choose a partition key for a large customer-order table?
Start with the application's dominant access patterns and expected data distribution. `customer_id` can be attractive when most queries retrieve orders for one customer because requests can be routed to a limited partition set. Then check whether a few customers have unusually large datasets or traffic, since that can create hotspots. If necessary, combine customer locality with bucketing or another controlled distribution technique.
Why can a monotonically increasing key be problematic for partitioning?
With range partitioning, increasing values tend to arrive in the newest range. If most writes target that range, one partition can receive disproportionate write traffic while older partitions are mostly idle. This is a workload hotspot even if the historical data is evenly sized. Hashing or another distribution strategy can spread writes, although that may reduce range locality.
What is the trade-off between a highly distributed partition key and query locality?
A key designed primarily for uniform distribution can scatter related records across many partitions. That may balance load but force queries to contact many partitions, increasing coordination and network work. A locality-oriented key can make common queries efficient, but correlated traffic can overload particular partitions. Good design therefore balances distribution against the actual query and traffic patterns.
How can you handle a hot partition caused by one very active key?
First identify whether the hotspot comes from a single value, a range, or a broader workload pattern. If one logical entity is too hot, a controlled bucketing or sharding scheme can split its traffic across multiple partitions. The application may then need to fan out reads and aggregate results, so the mitigation introduces complexity. The design should be based on measured traffic and acceptable query latency rather than applying sharding indiscriminately.
What metrics would you use to evaluate whether a partition key is working well?
Measure data size and request volume per partition rather than looking only at global averages. Useful signals include partition size variance, read and write rates, latency by partition, throttling, and the frequency of cross-partition queries. I would also examine the hottest keys because averages can hide a severe hotspot. These measurements should be evaluated under realistic production traffic.