Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 37 ยท Data Modeling Patterns

Design From Access Patterns

Learn the DynamoDB way to model data: list every question your app asks, then shape keys and indexes to answer each one with a single Query call.

In a relational database you normalize first and ask questions later. SQL joins, ad hoc WHERE clauses and indexes added after the fact make almost any question answerable. DynamoDB works the other way round. It has no joins, a Query can only read one partition by its exact key, and a Scan reads everything and bills you for it. If you design the tables first and think about questions later, you will end up with a design that needs scans, or a rewrite.

So the first step of DynamoDB modeling is not drawing tables. It is writing down your access patterns: every read and write your application performs, with how often it runs and how fast it must be. This lesson teaches a repeatable process for turning that list into keys and indexes. The following lessons (single-table design, relationships, hot partitions) build on it.

What an access pattern looks like

An access pattern is one sentence with a verb, an input and an expected result. Good ones are specific:

  • "Get a customer by customer ID."
  • "List a customer's orders, newest first, 20 per page."
  • "Get one order together with its line items."
  • "Find all orders in status SHIPPED for one warehouse, oldest first."
  • "Record that an order was paid, but only if it is still PENDING."

Bad ones are vague: "show orders", "search products". Vague patterns hide whether you need a key lookup, a range, a filter or a full-text engine. DynamoDB is excellent at the first two and poor at the rest.

For each pattern also write down: how many items it returns, how often it runs (per second), whether the read must be strongly consistent, and whether the data changes often. These numbers drive the capacity choices from capacity modes.

The process in six steps

  1. List entities (customer, order, product) and how they relate.
  2. List access patterns with the numbers above.
  3. Choose the primary key for the main table so that the most important patterns are one GetItem or one Query.
  4. Add sort-key structure so related items sit together in one partition and a range of the sort key returns them in a useful order.
  5. Cover leftover patterns with global or local secondary indexes.
  6. Re-check every pattern against the design. Each must map to exactly one operation, a key condition, and an index (or the table).

Model in the right order

Entities Customers, orders, items
Access patterns Every read and write
Keys PK and SK shape the table
Indexes Cover what is left

Questions first, tables second.

1 / 2

A worked example

Take a small shop. Entities: customers and orders. The patterns we need:

# Pattern Operation
1 Get a customer profile GetItem on PK=CUSTOMER#c1, SK=PROFILE
2 List a customer's orders, newest first Query on PK=CUSTOMER#c1, SK begins_with ORDER#, ScanIndexForward=false
3 Get one order GetItem on PK=CUSTOMER#c1, SK=ORDER#2026-03-01#o17
4 List shipped orders for a warehouse Query on a GSI with GSI1PK=WAREHOUSE#w2#SHIPPED

Look at how the sort key does the work in pattern 2. It starts with the literal ORDER#, followed by an ISO date. Because DynamoDB stores items of one partition sorted by sort key, all orders of one customer are contiguous and chronological. The newest come first when you read backwards. No index is needed.

Here is the request for pattern 2 as it looks in the AWS CLI (read-only here, because DynamoDB is not available in your browser):

Terminal
aws dynamodb query \
  --table-name Shop \
  --key-condition-expression "PK = :pk AND begins_with(SK, :prefix)" \
  --expression-attribute-values '{":pk":{"S":"CUSTOMER#c1"},":prefix":{"S":"ORDER#"}}' \
  --no-scan-index-forward \
  --limit 20

Try it: map patterns to keys with a stand-in store

DynamoDB itself cannot run on this site. The editor below is a tiny stand-in written in Python: a dictionary of partitions, each holding items sorted by sort key. It is not the real service, but the key rules are the same, so you can test whether a design answers each pattern.

Python Does the design answer the patterns?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that every pattern is answered by one call that names a full partition key. That is the test every design must pass.

Spotting a pattern that does not fit

Pattern 4 above (shipped orders per warehouse) cannot be served by the main table, because the orders live under customers. The fix is a global secondary index whose partition key is built from the warehouse and status. The need for it appeared during step 6 of the process, not after launch.

Python Check patterns against key candidates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Keep the pattern list in the repository (a markdown table next to the table definition). Every pull request that adds a query should add a row. Reviewers can then check the key design instead of discovering a scan in production.

Common mistakes

  • Copying the relational schema. One DynamoDB table per SQL table, joined in application code, means many round trips and none of the benefits.
  • Designing for flexibility. "We might need to filter by anything later" leads to scans. Choose the patterns you really have and add indexes when new ones appear.
  • Ignoring item size and rate. An item cannot exceed 400 KB, and one partition serves a limited amount of traffic. See hot partitions.
  • Using FilterExpression as a query. A filter runs after the read and you pay for everything read. It trims results; it does not find them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What should you do first when designing a DynamoDB table?

    DynamoDB has no joins and Query reads one partition, so keys must be chosen to answer your specific questions. Patterns come first.
  2. 2. Which operation should serve a frequent, latency-sensitive access pattern?

    GetItem and Query touch only the data you name. A Scan reads and bills the whole table, filters notwithstanding.
  3. 3. Why does a sort key such as ORDER#2026-03-09#o21 help?

    Sort-key order is physical order inside the partition, which makes range reads cheap and ordered.
  4. 4. A pattern needs free-text search across notes. What is the right conclusion?

    DynamoDB answers key-based questions. Search is usually served by an additional service fed from a stream.

Summary

  • Write the access patterns first, with frequency, size and consistency needs.
  • Every important pattern should map to one GetItem or one Query on a table or index.
  • The sort key keeps related items together in order; indexes cover the remaining patterns.
  • A FilterExpression trims results after reading and never replaces a key condition.
  • Re-check the finished design against the list before writing any code.

Next: Single-Table Design