Lesson 2 of 10 Amazon DynamoDB

Designing Partition Keys and Sort Keys from Access Patterns

This lesson explains how a DynamoDB table's primary key controls how data is spread across partitions and which queries run efficiently. Good design therefore starts with the questions the application will ask, not with the shape of the data.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish simple and composite primary keys and explain how the partition key distributes data.
  • Design keys from a list of access patterns, including single-table designs.
  • Explain how write sharding spreads writes when a natural key is too concentrated.
  • Describe the 400 KB item size limit and how to store larger objects.

Primary keys and partitions

Every DynamoDB table has a primary key that uniquely identifies each item, and it is the only attribute set that must be defined when the table is created. The key can be simple or composite, and the choice determines how items can be read efficiently.

A simple primary key consists of a partition key alone, such as userId. Each value identifies exactly one item, so the table supports direct lookups by that value and nothing else. A composite primary key adds a sort key, such as orderDate. Many items can share a partition key, as long as their sort keys differ, and items with the same partition key are stored together in sort key order as an item collection. A single Query can then read a customer's orders, or only those between two dates, in one request. Composite keys are the basis of most DynamoDB data models, because they let one table answer several related questions.

The following table summarises the two key types.

Key typeMade ofUse when
SimpleA partition key onlyEach item is looked up by one unique value, such as a user ID
CompositeA partition key and a sort keyRelated items are grouped and read together, such as a customer's orders sorted by date

DynamoDB hashes the partition key to decide where an item is stored. A key with many distinct, evenly used values spreads the load. A key with few values, or one value used far more than the rest, creates a hot partition that throttles even when the table has spare capacity overall.

The following diagram shows how the partition key places an item and how the sort key orders an item collection.

How DynamoDB stores items. The partition key is hashed to choose a partition. Items that share a partition key form an item collection stored in sort-key order, so one Query can read a customer's orders between two dates. A partition key value used far more than the others creates a hot partition that throttles.
Partition and item values are illustrative. Many distinct, evenly used partition key values spread the load; a single heavily used value concentrates it on one partition.
Sort into groups

Is it a good partition key?

Exam skills 1.3.4

Recognise attributes that spread data evenly and those that create hot partitions.

Log in to try this activity Interactive activities are free with an account.

Designing from access patterns

Because keys decide what can be queried, start by listing the application's access patterns. In single-table design, several entity types share one table with generic key names, so related items can be fetched in one query.

Example. An order system must fetch a customer's profile, list their orders newest first, and find all orders in a given status. Generic PK and SK attributes serve the first two; a global secondary index on status serves the third.

PK              SK                          status     createdAt
CUSTOMER#42     PROFILE                     -          -
CUSTOMER#42     ORDER#2026-10-01#9001       SHIPPED    2026-10-01
CUSTOMER#42     ORDER#2026-10-05#9017       PENDING    2026-10-05

Query PK = "CUSTOMER#42" AND begins_with(SK, "ORDER#"), ScanIndexForward = false  -> newest orders first
Query GSI status-index: status = "PENDING"                                       -> all pending orders
Scenario questions

Design keys from access patterns

Exam skills 1.3.2 · 1.3.4

Practise turning access patterns into keys and indexes.

Log in to try this activity Interactive activities are free with an account.

Write sharding

When the natural key is too concentrated, such as a date that every write shares, a random or calculated suffix spreads the writes. Writing to 2026-10-05#0 to 2026-10-05#9 uses ten partition key values instead of one; reads then query all ten suffixes and merge the results.

Item size

An item can be at most 400 KB, including attribute names. Store larger objects, such as images or documents, in Amazon S3 and keep the object key in the item. Short attribute names also reduce the item size and therefore the capacity each read and write consumes.

400KB

Maximum item size, including attribute names

Worked example

Designing keys from access patterns

A ticketing application must support three access patterns: fetch a ticket by ID, list a customer's tickets newest first, and list a ticket's comments in order.

PK               SK                            Attributes
TICKET#981       META                          customerId, subject, status
TICKET#981       COMMENT#2026-10-05T09:12Z     author, body
TICKET#981       COMMENT#2026-10-05T10:40Z     author, body
CUSTOMER#42      TICKET#2026-10-05#981         subject, status

Get ticket:           GetItem   PK = TICKET#981, SK = META
Ticket with comments: Query     PK = TICKET#981
Customer's tickets:   Query     PK = CUSTOMER#42, begins_with(SK, "TICKET#"), ScanIndexForward = false

Every pattern is a single GetItem or Query, with no scans and no joins. The partition keys are ticket and customer IDs, which have high cardinality, so traffic spreads evenly across partitions.

Check yourself

A table uses the order date as its partition key, and most writes are for today's date. What problem does this cause?

Show answer

A hot partition: almost all writes go to one partition key value and can be throttled. A key with many distinct values, such as the order ID, or write sharding with a random suffix spreads the load.

Exam Tips

Remember for the exam

  • Throttling despite enough total capacity points to a hot partition key.
  • To retrieve a customer and their orders in one query, give them a shared partition key with different sort keys.
  • Design the keys from the application's access patterns, not from the shape of the data.
  • Dates and status values make poor partition keys; spread them with write sharding if they must be used.
  • Objects larger than 400 KB belong in S3, with their key stored in DynamoDB.