Course contentsShow
← Cloud and Infrastructure
Lesson 431 of 2,9898. Managed DatabasesFree lesson

DynamoDB Primary Keys and Partition Keys

Understanding partition keys, sort keys, and composite primary keys for efficient data distribution and access.

DynamoDB Primary Keys and Partition Keys

What You'll Learn

You'll understand how DynamoDB uses primary keys to uniquely identify items and partition keys to distribute data across servers for fast, reliable access.

The Foundation of DynamoDB Organization

In DynamoDB, every item (row) must have a primary key that uniquely identifies it. Think of this like a library card number—no two items can share the same primary key.

DynamoDB offers two types of primary keys:

Simple Primary Key (Partition Key Only)

A partition key is a single attribute that DynamoDB uses both to identify your item and to decide which physical server (partition) stores it. DynamoDB runs a hash function on this key to distribute items evenly across storage nodes.

Analogy: Imagine a filing cabinet where documents are sorted by last name. The last name determines which drawer holds the document. That's your partition key—it both identifies the item and determines its physical location.

Composite Primary Key (Partition Key + Sort Key)

For more flexibility, combine a partition key with a sort key. Items with the same partition key are stored together, but sorted by the sort key value. This allows multiple items to share a partition key as long as they have different sort keys.

Analogy: Think of organizing books by author (partition key) and then alphabetically by title (sort key). All Stephen King books sit together, sorted by title.

Why This Matters

The partition key determines data distribution and query performance. Choosing a key with high cardinality (many unique values) prevents "hot partitions" where one server gets overwhelmed while others sit idle.

Key Takeaway: DynamoDB primary keys uniquely identify items—use a partition key alone for simple lookups, or add a sort key to group related items together while maintaining uniqueness through the combination.