Course contentsShow
← Databases & SQL
Lesson 1395 of 1,95313. Specialized Data StorageFree lesson

Partition Keys and Cluster Keys

Design effective shard keys to balance data distribution across nodes.

In this lesson, you will learn how to select effective shard keys to achieve a uniform distribution of data across your database nodes.

When scaling a database horizontally (sharding), the partition key is the cornerstone of your architecture. It dictates which physical node stores a specific piece of data. If you choose a poor partition key, you risk creating "hot spots"—where one node handles 90% of the traffic while others sit idle—or "scatter-gather" queries that force your system to query every single node just to retrieve a simple record.

Designing for Balance

The goal of a partition key is cardinality. You want a key with high variability. For example, if you are building an e-commerce platform and use region_id as your partition key, your US-East node will be overwhelmed compared to your other regions.

Instead, consider using a user_id or a composite key. If you are sharding a transaction log, you might combine account_id and timestamp to ensure the data is spread evenly:

-- Partitioning strategy using a composite shard key
CREATE TABLE transaction_logs (
    account_id UUID,
    transaction_time TIMESTAMP,
    data JSONB,
    PRIMARY KEY (account_id, transaction_time)
) PARTITION BY HASH (account_id);

The Role of Cluster Keys

While the partition key determines where the data lives, the cluster key determines how data is physically sorted within that node. A well-chosen cluster key acts as an index that keeps related records close together on the disk. By clustering your data by transaction_time within the shard defined by account_id, you ensure that range queries (e.g., "get all transactions for this user in July") are incredibly fast because the database reads a contiguous block of disk rather than jumping around.

The tension lies in the balance: a shard key that is too granular (like a random UUID) makes range scans impossible, while a shard key that is too broad (like a date-only key) creates massive hot spots. The best design uses a partition key that provides enough uniqueness for distribution, paired with a cluster key that supports your most frequent query patterns.

Key Takeaway: Design your partition keys for high cardinality to avoid hot spots, and use cluster keys to physically group data that is frequently accessed together.