Single-Table Design and Item Collections
Single-table design stores several entity types in one table, using generic key attributes (PK, SK) whose values carry the entity type.
Introduction
Single-table design stores several entity types in one table, using generic key attributes (PK, SK) whose values carry the entity type. Items that share a partition key form an item collection, and one Query can return the whole collection — the DynamoDB equivalent of a join, done at write time.
This lesson implements the core of ShopTable.
Purpose of this lesson
You will learn when single-table design pays off, how to lay out entity keys, and how to fetch related entities in one request.
Understanding the topic
ShopTable layout:
- Every item has an
entityTypeattribute so code (and Streams consumers) can tell items apart. - Sort keys that start with a type prefix let
begins_withselect one entity type from a collection. - ISO-8601 timestamps in sort keys sort chronologically as strings.
- Duplicating small, rarely changing attributes (an order summary under the customer) trades storage for single-request reads.
| Entity | PK | SK |
|---|---|---|
| Customer | CUSTOMER#c1 | PROFILE |
| Order summary (under customer) | CUSTOMER#c1 | ORDER#2026-11-19T10:00:00Z#o1 |
| Order header | ORDER#o1 | HEADER |
| Order line | ORDER#o1 | LINE#001 |
| Product | PRODUCT#p9 | PRODUCT |
Informative example
Write an order and read it back with its lines in one Query:
import { BatchWriteCommand, QueryCommand } from "@aws-sdk/lib-dynamodb";import { ddb, TABLE } from "./ddb";await ddb.send(new BatchWriteCommand({RequestItems: {[TABLE]: [{ PutRequest: { Item: { PK: "ORDER#o1", SK: "HEADER", entityType: "Order", customerId: "c1", status: "PLACED", total: 59 } } },{ PutRequest: { Item: { PK: "ORDER#o1", SK: "LINE#001", entityType: "OrderLine", sku: "BOOK-1", qty: 1, price: 39 } } },{ PutRequest: { Item: { PK: "ORDER#o1", SK: "LINE#002", entityType: "OrderLine", sku: "PEN-4", qty: 2, price: 10 } } },],},}));const { Items = [] } = await ddb.send(new QueryCommand({TableName: TABLE,KeyConditionExpression: "PK = :pk",ExpressionAttributeValues: { ":pk": "ORDER#o1" },}));for (const item of Items) console.log(item.SK, item.entityType);
Output
HEADER Order LINE#001 OrderLine LINE#002 OrderLine
Items come back sorted by sort key (UTF-8 byte order), so HEADER sorts before LINE#…. To read only lines, add AND begins_with(SK, :line) to the key condition.
Trade-offs
- Fewer round trips and lower latency vs. a schema that is harder to read without documentation.
- One table to operate vs. shared throughput and backup scope across entities.
- Pre-joined data vs. duplicated attributes that must be kept in sync (often with Streams).
Decision framework
- Use single-table design when several entities are read together on hot paths and the access patterns are stable.
- Use separate tables when entities have different lifecycles, retention, security boundaries, or teams — or when the workload is simple CRUD per entity.
- Do not force unrelated data into one table just to follow the pattern.
Best practices
- Document the key layout (like the table above) in the repository.
- Add an
entityTypeattribute to every item. - Keep item collections small enough to page through quickly; large collections belong under their own partition key.
Common mistakes
- Putting unbounded child lists (all events for a popular product) under one partition key.
- Overloading keys so much that new engineers cannot tell what an item is.
- Duplicating attributes that change often, creating expensive fan-out updates.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhat is an item collection?+
Answer
2IntermediateQuestionWhen would you avoid single-table design?+
Answer
Hands-on exercise
Hands-on: add a CUSTOMER#c1 / PROFILE item and two order summaries under the customer with sort keys ORDER#<ISO timestamp>#<orderId>. Write listCustomerOrders(customerId) that returns summaries newest first using ScanIndexForward: false and begins_with(SK, "ORDER#").
Summary
Single-table design groups related entities under shared partition keys so one Query returns what a relational join would. It works best for stable, hot access patterns and needs clear documentation.