DynamoDB interview question · Most asked

Explain partition keys, sort keys and the two kinds of primary key.

Summary

A primary key is either a simple partition key (each item has a unique partition key value) or a composite of partition key plus sort key (items share a partition key and are unique by sort key). The partition key is hashed to choose the storage partition; the sort key orders items within that partition.

Longer Answer

With a composite key, all items with the same partition key form an item collection, stored together and sorted by sort key. Query can then fetch the whole collection or a range of it (begins_with, between, <, >), which is how one-to-many relationships are modelled: PK = CUSTOMER#123, SK = ORDER#2026-10-17#987.

Good partition keys have many distinct values that spread requests evenly (user ID, order ID). Bad partition keys concentrate traffic (a status flag, today's date).

Code Example

PK              SK                          attributes
CUSTOMER#123    PROFILE                     name, email
CUSTOMER#123    ORDER#2026-10-15#981        total, status
CUSTOMER#123    ORDER#2026-10-17#987        total, status

Key Points

  • Simple PK or composite PK + SK.
  • PK is hashed to a partition.
  • SK sorts the item collection.
  • Query by PK with SK conditions.

More most-asked DynamoDB questions

← All DynamoDB questions