← Lessons

quiz vs the machine

Silver1050

Databases

DynamoDB Partition and Sort Keys

How DynamoDB places and retrieves items using the primary key.

4 min read · intro · beat Silver to climb

The primary key

Every DynamoDB table has a primary key that uniquely identifies each item. It comes in two shapes.

  • Simple key is just a partition key. Each item must have a unique partition key value.
  • Composite key is a partition key plus a sort key. Items can share a partition key as long as their sort keys differ.

How the partition key works

DynamoDB hashes the partition key value to decide which internal partition stores the item. This is why it is sometimes called the hash key. Choosing a partition key with many distinct, evenly used values spreads load across partitions.

  • A good partition key has high cardinality so traffic is balanced.
  • A poor partition key concentrates traffic on one partition, creating a hot partition.

How the sort key works

Within one partition key, items are stored sorted by the sort key. This lets you run efficient range queries.

  • Query by partition key alone returns all items in that collection.
  • Add a sort key condition like begins with or between to fetch a slice.

Diagram

Key idea

The partition key decides where an item lives, and the sort key decides its order within that location, enabling fast lookups and range queries.

Check yourself

Answer to earn rating on the learn ladder.

1. What does DynamoDB use the partition key for?

2. Within a single partition key, items are stored in what order?

3. Why prefer a high cardinality partition key?