Stop 09 · Databases on AWSTopics · 7/11

DynamoDB Keys, Indexes & Access Patterns

Part of the AWS Roadmap

In short

In SQL you design tables first and write queries later. In DynamoDB it's the other way round: list the questions your app will ask, then pick keys that answer them in one fast lookup.

DynamoDB is a fully managed NoSQL key-value and document database with single-digit millisecond performance at scale. Each table has a partition key, an optional sort key, and global or local secondary indexes for other query patterns.

The jaadu in a postal uniform drops letters into a long row of labelled sacks, and inside one open sack the letters stand neatly in order.

Think of it like this

Post office sorting by PIN code

At the Chennai sorting centre, letters go into a bag by PIN code first, then inside each bag they're arranged by street. Finding all letters for one PIN is instant. Finding every letter from one sender would mean opening every bag. In DynamoDB the partition key is the PIN code bag, the sort key is the street order, and a secondary index is a second sorting of the same letters by a different label.

Watch it happen

Orders table: PK = customerId, SK = orderDate

C1 | 2026-01-04
0
C1 | 2026-02-11
1
C1 | 2026-03-20
2
C2 | 2026-01-09
3
C2 | 2026-03-02
4
C3 | 2026-02-15
5

1/5Each item lives in a partition chosen by customerId, sorted by orderDate inside it.

The big ideas

What you need to know

Partition key

Decides which partition stores the item. Pick something with many distinct values, like customerId, so load spreads evenly.

Sort key

Orders items within a partition and allows conditions like begins_with, between, > and <.

GSI vs LSI

A global secondary index has its own partition and sort key and can be added any time. A local secondary index keeps the same partition key and must be created with the table.

Access patterns first

Write down every query the app needs, such as 'orders for a customer, newest first', before creating anything.

How it works

Designing a DynamoDB table

  1. 1

    List access patterns

    Every screen and API call, written as a question with its inputs.

  2. 2

    Choose the base keys

    Make the most frequent pattern a single Query on the table.

  3. 3

    Add GSIs for the rest

    One index per extra pattern, projecting only the attributes you need.

  4. 4

    Test with real shapes

    Load sample items and run each Query. If you reach for Scan, revisit the design.

See it in code

Walk through the code

bash
1aws dynamodb create-table \2  --table-name Orders \3  --attribute-definitions \4      AttributeName=customerId,AttributeType=S \5      AttributeName=orderDate,AttributeType=S \6      AttributeName=status,AttributeType=S \7  --key-schema \8      AttributeName=customerId,KeyType=HASH \9      AttributeName=orderDate,KeyType=RANGE \10  --global-secondary-indexes '[{11      "IndexName": "status-orderDate-index",12      "KeySchema": [13        {"AttributeName": "status", "KeyType": "HASH"},14        {"AttributeName": "orderDate", "KeyType": "RANGE"}],15      "Projection": {"ProjectionType": "ALL"}}]' \16  --billing-mode PAY_PER_REQUEST17 18# All of C1's orders since 1 Feb, newest first19aws dynamodb query \20  --table-name Orders \21  --key-condition-expression "customerId = :c AND orderDate >= :d" \22  --expression-attribute-values '{":c":{"S":"C1"},":d":{"S":"2026-02-01"}}' \23  --no-scan-index-forward

1/5Declare only the attributes used as keys in the table or its indexes.

AWS CLI v2. Only key attributes go in attribute definitions. Every other attribute can be added freely on each item.

Save yourself the pain

Common mistakes

Oops

Using Scan with a filter to find items, because it works on small test data.

Do this instead

Scan reads the whole table and you pay for all of it. Design keys or a GSI so the question becomes a Query.

Oops

Choosing a partition key with few values, like status or country.

Do this instead

That creates hot partitions. Use a high-cardinality key, and move low-cardinality fields into a GSI if needed.

Oops

Expecting a GSI to show a write instantly.

Do this instead

GSIs are eventually consistent. Read from the base table when you need strongly consistent results.

Your mission

Model a food delivery app

~30 min

Quick check

You forgot an index on a live table and need to query by restaurantId. What can you add now?

Go deeper

  1. 1Create a table for orders with customerId as partition key and orderDate as sort key.
  2. 2Query all orders for one customer in a date range with the CLI or boto3.
  3. 3Design the access patterns first, then the keys, using the AWS DynamoDB best practices guide.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge