Course contentsShow
← Computer Science
Lesson 2685 of 2,87260. Distributed Data and CommunicationFree lesson

Partition Keys and Shard Keys

Selecting the attribute used to determine which partition or shard holds each record.

Partition Keys and Shard Keys

What You'll Learn

You'll learn how to select the right attribute to determine which partition or shard stores each record in a distributed database.

The Core Idea

When you partition or shard data across multiple nodes, you need a systematic way to decide where each record lives. The partition key (also called shard key) is the attribute you choose to make this determination. It's the routing mechanism that maps data to physical locations.

Think of it like sorting mail into different postal zones based on ZIP codes. The ZIP code is your "partition key"—it determines which distribution center handles each piece of mail.

How It Works

When you insert or query data, the system applies a function to the partition key value:

hash(partition_key) % number_of_partitions = target_partition

Or for range-based partitioning, values might be grouped by ranges (e.g., A-M goes to partition 1, N-Z to partition 2).

Choosing the Right Key

Your partition key choice dramatically affects performance:

  • Good distribution: Keys should spread data evenly across partitions. Using user_id often works well; using country might create hotspots if most users are from one country.
  • Query patterns: Queries filtering by the partition key can go directly to one partition. Queries ignoring it must scan all partitions.
  • Related data co-location: Items that are queried together should share the same key to avoid distributed joins.

For example, in a social media app, partitioning by user_id keeps all of a user's posts together, making profile queries efficient.

Key Takeaway: The partition key is your most critical sharding decision—it determines data distribution, query performance, and scalability, so choose an attribute that balances even distribution with your access patterns.