Course contentsShow
← System Design
Lesson 1472 of 1,91936. Data Partitioning Strategies in DepthFree lesson

Range Partition Key Selection

Choosing effective partition keys that distribute data evenly across ranges.

Range Partition Key Selection

What you'll learn: How to choose partition keys that keep range-partitioned data evenly distributed and avoid hotspots.

Range partitioning divides data based on contiguous key ranges (like A-M in one partition, N-Z in another). The partition key you choose determines whether data spreads smoothly or piles up unevenly, so selecting it wisely is critical for performance.

Why Partition Key Choice Matters

Imagine organizing books by author last name. If you split A-M and N-Z, you'll find M contains far more authors than Q. Your partitions are skewed—one shard handles heavy traffic while others sit idle. This creates hotspots where one partition becomes a bottleneck.

Principles for Effective Selection

High Cardinality: Choose keys with many distinct values. user_id (millions of unique users) beats country_code (200 countries) because finer granularity enables more balanced splits.

Even Distribution: The key values should spread naturally across the range. Timestamps work well for time-series data with steady traffic; they work poorly if 90% of writes happen between 9 AM and 10 AM daily.

Query Alignment: Select keys that match your access patterns. If you query by created_date, partition on that—you'll hit one or few partitions per query. Partitioning on user_id when querying by date forces scanning all partitions.

Avoid Sequential Hotspots: Auto-incrementing IDs or timestamps cause all new writes to hit the latest partition. Consider composite keys like (region, timestamp) to spread the load across regions.

Practical Example

For an e-commerce order system: order_id (auto-incrementing) creates write hotspots. Instead, use customer_id if queries fetch per-customer orders, or (region, order_date) if analyzing regional sales trends.

Key Takeaway: Effective range partition keys balance high cardinality, natural distribution, and query access patterns to prevent skew and hotspots while enabling efficient range scans.