Tablux

Tablux › Guides › DynamoDB single-table design

Guide

DynamoDB single-table design, with a worked example

DynamoDB single-table design stores several entity types, such as customers, orders and order lines, in one table with generic keys like PK and SK. Items you read together share a partition key, so one Query returns them all. You design from the access patterns, not the entities, and add overloaded global secondary indexes for the rest. It trades query flexibility for fast, predictable reads.

Tablux on iPhone listing the entity types of a single-table DynamoDB design: LINE, ORDER, PRODUCT, CUSTOMER and REFUND, each found by its sort-key prefix, with ORDER's attributes expanded
The trailhead table from this guide, with entity types recovered from the sort-key prefixes of a sample of items.

Why one table

DynamoDB has no joins. If customers, orders and order lines live in three tables, the order page needs three requests and your code stitches the results together. Single-table design avoids that by putting related items side by side.

It relies on the item collection: all items with the same partition key. DynamoDB stores them together, sorted by sort key, and a single Query reads a whole collection or a sorted slice of it. Give a customer and their orders the same partition key and the account page is one request.

The AWS developer guide lists the benefits: data locality for queries across entity types, lower read cost and latency, fewer tables to permission, monitor and encrypt, and smoother traffic that suits provisioned capacity. Its general advice is to keep as few tables as possible.

Start from access patterns

In a relational database you normalise first and write queries later. DynamoDB reverses that. AWS's guidance is not to design the schema until you know the questions it must answer. So the first artefact is a list of reads and writes, with how often each runs.

The worked example is Trailhead Outfitters, an outdoor shop with customers, orders, order lines, refunds and products. Its access patterns:

  1. Get a customer by ID.
  2. Show a customer's profile and order history together.
  3. List a customer's orders in a date range, newest first.
  4. Get the lines and refunds of an order.
  5. Find orders by status, oldest first (for example, pending orders that never shipped).
  6. List the products in a category, A to Z.
  7. Get a product by ID.

The keys

The table has a string partition key PK and a string sort key SK. The names are generic on purpose: what they hold depends on the item. Each value starts with an uppercase prefix naming the entity.

Entity     PK               SK                                 GSI1PK           GSI1SK
Customer   CUSTOMER#1042    CUSTOMER#1042                      -                -
Order      CUSTOMER#1042    ORDER#2026-10-14T09:31:00#58213    STATUS#pending   2026-10-14T09:31:00
Line       ORDER#58213      LINE#17                            -                -
Refund     ORDER#58213      REFUND#912                         -                -
Product    PRODUCT#17       PRODUCT#17                         CATEGORY#Tents   PRODUCT#Ridgeline 2P tent

Three choices carry the design:

  • Orders sit in the customer's collection. The customer item and their orders share CUSTOMER#1042. CUSTOMER# sorts before ORDER#, so the profile comes first.
  • The order sort key starts with the timestamp. ISO 8601 strings sort chronologically, so date ranges and newest-first are sort-key operations. The order ID at the end keeps two orders in the same second distinct.
  • GSI1 is overloaded. One index, two meanings: on orders GSI1PK is the status, on products it is the category. Customers, lines and refunds have no GSI1PK, so they never enter the index. Global secondary indexes are sparse by default, which keeps this one small.

Each access pattern, served

#Access patternOperation
1Customer by IDGetItem PK = CUSTOMER#1042, SK = CUSTOMER#1042
2Profile and order historyQuery PK = CUSTOMER#1042
3Orders in a date range, newest firstQuery PK = CUSTOMER#1042 AND SK BETWEEN ORDER#2026-10 AND ORDER#2026-11, descending
4Lines and refunds of an orderQuery PK = ORDER#58213
5Orders by status, oldest firstQuery GSI1 GSI1PK = STATUS#pending, ascending
6Products in a categoryQuery GSI1 GSI1PK = CATEGORY#Tents
7Product by IDGetItem PK = PRODUCT#17, SK = PRODUCT#17

Pattern 3 as a Query from the AWS CLI:

aws dynamodb query --table-name trailhead \
  --key-condition-expression "PK = :c AND SK BETWEEN :from AND :to" \
  --expression-attribute-values '{":c":{"S":"CUSTOMER#1042"},":from":{"S":"ORDER#2026-10"},":to":{"S":"ORDER#2026-11"}}' \
  --no-scan-index-forward

The same thing in PartiQL, which DynamoDB runs as a Query because the partition key is pinned:

SELECT * FROM "trailhead"
WHERE "PK" = 'CUSTOMER#1042' AND "SK" BETWEEN 'ORDER#2026-10' AND 'ORDER#2026-11'
ORDER BY "SK" DESC

Pattern 5, stale pending orders from the index:

SELECT "PK", "orderId", "total", "orderedAt" FROM "trailhead"."GSI1"
WHERE "GSI1PK" = 'STATUS#pending' AND "GSI1SK" < '2026-10-01'

More statements against this table are in the PartiQL guide.

Denormalisation and writes

Order lines copy productName and category from the product so an order page never needs a second lookup. For orders that is usually what you want: the line records what was sold, even if the product is renamed later. Where copies must stay in step, write them together with TransactWriteItems (up to 100 actions) or update them asynchronously from DynamoDB Streams.

Status changes are two-attribute writes: status and GSI1PK move together. Changing an index key costs two index writes, one to remove the old entry and one to add the new.

Watch low-cardinality index keys. STATUS#delivered eventually holds nearly every order, and an index partition key with five values concentrates writes on a few partitions. At high volume, either shard the value (STATUS#delivered#0 to #9) or keep only open orders in the index by removing GSI1PK and GSI1SK once an order is delivered. AWS's sparse-index example is exactly this: an attribute that exists only while an order is open.

What this design can't answer

Write down the gaps too. Two show up at once:

  • An order by order ID alone. The order item lives under CUSTOMER#…, and ORDER#58213 only holds lines and refunds. Fix it by adding an order summary item to the ORDER#58213 collection, or a GSI keyed on the order ID. Either way, existing items need backfilling.
  • All refunds in July, or refunds by reason. Nothing indexes refunds by date, so this is a Scan with a filter, which reads and pays for every item in the table. A spike of zipper complaints on one tent is exactly the question this table is bad at.

Revenue by month has no answer at all inside DynamoDB, because there is no GROUP BY. Export the table to Amazon S3 (which needs point-in-time recovery switched on) and query it with Athena, or stream changes into an analytics store.

When not to use single-table design

The AWS developer guide presents single-table and multiple-table design as two valid foundations, and says multiple tables are "a good and sufficient approach" when your access patterns don't query entities together. Its listed disadvantages of one table, and Alex DeBrie's (author of The DynamoDB Book, in his 2020 post on single-table design), point to the same cases:

  • Access patterns are still moving. The table is tailored to today's questions. A new one can mean a new index and a backfill of every item. DeBrie suggests early-stage products that value developer agility may prefer simpler, multi-table models.
  • Analytics and ad-hoc queries. Overloaded keys and copied attributes are hard to read in a spreadsheet and must be unpacked before analysis. If people ask new questions every week, keep a relational or analytical copy.
  • A steep learning curve. AWS calls the design paradoxical compared with relational modelling. A small team that will maintain the table for years should weigh that.
  • Different requirements per entity. Backups are all-or-nothing per table, one encryption key covers every item, the Infrequent Access table class applies to the whole table, and the stream carries every entity's changes.
  • GraphQL. AWS notes single-table design is harder to implement with GraphQL, and DeBrie argues that resolvers fetching one entity at a time lose most of its benefit.

Type attributes vs key prefixes

There are two conventions for saying what an item is, and they work well together:

  • Key prefixes (ORDER#, LINE#) make keys self-describing, stop IDs of different entities colliding in one collection, and let begins_with select one entity type from a collection.
  • A type attribute (entityType: "ORDER") makes the type a plain value: easy to filter on in Lambda event filters for Streams, to group by in an S3 export, and what libraries use to map items to classes. DynamoDB Toolbox, for example, writes an _et attribute by default.

The Trailhead table uses prefixes only. Adding entityType costs a few bytes per item and pays off the first time someone exports the table.

How tools recover entity types from a sample

A single table hides its schema: DescribeTable reports only key attributes and indexes. A client has to read items and infer the rest. Tablux does it like this:

  1. Scan a sample of up to 1,000 items.
  2. Look for a type attribute: the first of entityType, EntityType, entity_type, type, Type, _type, __typename, kind, _et and entity that is a string on at least 80% of the sampled items. If one qualifies, its values name the entities.
  3. Otherwise, group items by the prefix of the sort key: leading letters, digits or underscores ended by #, : or |, so ORDER#2026-10-14… is an ORDER. Items whose sort key has no prefix fall back to the partition key's prefix.
  4. If only one kind of item turns up and there is no type attribute, it is treated as a plain table.

The sort key goes first because it names the item in a collection: CUSTOMER#1042 / ORDER#… is an order, not a customer. The 80% threshold keeps an ordinary attribute such as a product's type from being mistaken for the entity marker. Each entity then lists its own attributes and opens on its own. Sampling has a limit worth knowing: an entity type that is rare in the table may not appear in the first 1,000 items, so it won't be listed.

Questions

Does AWS recommend single-table design?

The developer guide advises keeping as few tables as possible, and lists single-table design's benefits. It also says multiple tables are a good and sufficient approach when access patterns don't query entity types together.

What is an item collection in DynamoDB?

All the items that share a partition key value. They are stored together and sorted by sort key, so one Query can return the whole collection or a range of it.

What is GSI overloading?

Using one global secondary index for several entity types by storing different kinds of values in the same generic index key attributes, such as a status on orders and a category on products.

How do I add a new access pattern to a single-table design?

Usually by adding a global secondary index or new key attributes, then backfilling existing items so they carry those attributes. Plan for the backfill; it touches every affected item.

Should I use a type attribute or key prefixes?

Both. Prefixes keep keys readable and let begins_with pick one entity type from a collection; a type attribute makes exports, stream filters and client libraries simpler.

Updated 2026-11-25