KKeySync
Back to quests

Quest · quest 1

Understanding Partition Keys

In DynamoDB, every item (row) must have a primary key— it uniquely identifies that item in a table. The Partition Key (also called Hash Key) decides where the item is stored inside DynamoDB's internal partitions.

Primary keysHashingData locality

Key takeaways

  • Primary keys uniquely identify each item in a table.
  • Partition keys determine the physical placement of data.
  • Items that share a partition key value are stored together.

What Is a Partition Key?

In DynamoDB, every item (row) must have a primary key — it uniquely identifies that item in a table.

The Partition Key (also called Hash Key) decides where the item is stored inside DynamoDB's internal partitions.

Think of it like assigning every Pokémon to a specific Pokéball based on its name — the Name determines which Pokéball it goes into.

How DynamoDB Uses It

When you insert or fetch data:

  • DynamoDB uses the Partition Key value to compute a hash.
  • That hash decides which internal partition the item lives in.
  • Items with the same Partition Key are always stored together.

Types of Primary Keys

DynamoDB supports two primary key patterns.

Simple Primary Key (only Partition Key)

Uses one attribute to uniquely identify an item.

Example:

  • Partition Key: Name
  • Every Pokémon's name is unique → Each item = one Pokémon.
NameType1Type2Height(m)Weight(kg)
Bulbasaurgrasspoison0.76.9
Charmanderfire---0.68.5
Squirtlewater---0.59.0

Each Pokémon is uniquely identified by its Name attribute.

Use Case: Perfect when each Pokémon is unique and can be fetched directly by name:

Query examplejs
{
  TableName: "FirstGenPokemon",
  Key: { Name: "Pikachu" }
}

Composite Primary Key (Partition + Sort Key)

Uses two attributes together for uniqueness.

The following is a small teaching example. Type1 has very few possible values, so a large production workload could concentrate traffic on hot keys. Start from real access patterns and choose a high-cardinality, evenly used partition key—or add a deliberate shard when one logical group receives heavy traffic.

Toy example:

  • Partition Key: Type1
  • Sort Key: Name
  • This way, Pokémon of the same type are grouped together.
Type1NameHeight(m)Weight(kg)
grassBulbasaur0.76.9
grassIvysaur1.013.0
fireCharmander0.68.5
fireCharizard1.790.5

Useful for seeing composite-key mechanics, not a blanket production recommendation for a high-traffic Pokédex.

Use Case: Quickly find all Pokémon of a specific type, sorted alphabetically:

Query examplejs
{
  TableName: "FirstGenPokemon",
  KeyConditionExpression: "Type1 = :type",
  ExpressionAttributeValues: { ":type": "fire" }
}

Check your understanding

Quick quiz

If you define a table with only one attribute (Name) as a key, what type of key is that?