Modeling One-to-Many Relationships
Most applications have parent–child data: a customer has orders, an order has lines, a product has reviews.
Introduction
Most applications have parent–child data: a customer has orders, an order has lines, a product has reviews. DynamoDB gives you four main ways to model one-to-many relationships, and the right choice depends on how many children there are, how often they change, and how they are read.
Purpose of this lesson
You will compare denormalised lists, item collections, composite sort keys, and secondary-index relationships, and implement the two most common ones.
Understanding the topic
The four strategies:
- Embedded list or map — children stored as an attribute of the parent. Simple, but limited by the 400 KB item size and rewritten on every change. Good for small, bounded lists (a few shipping addresses).
- Item collection — children share the parent's partition key with their own sort keys (
ORDER#o1 / LINE#001). Unbounded, individually updatable, fetched with one Query. - Composite sort key — hierarchical sort keys such as
LOCATION#US#WA#Seattleletbegins_withquery at any level. - Secondary index — children live under their own partition key and carry the parent id in an indexed attribute; used when children must also be looked up independently.
Informative example
Composite sort keys for hierarchical queries — store locations so you can list a country, a state, or a city:
import { QueryCommand } from "@aws-sdk/lib-dynamodb";import { ddb, TABLE } from "./ddb";// Items: PK = "BRAND#acme", SK = "STORE#<country>#<state>#<city>#<storeId>"// STORE#US#WA#Seattle#s1, STORE#US#WA#Spokane#s2, STORE#US#OR#Portland#s3async function storesUnder(prefix: string) {const { Items = [] } = await ddb.send(new QueryCommand({TableName: TABLE,KeyConditionExpression: "PK = :pk AND begins_with(SK, :prefix)",ExpressionAttributeValues: { ":pk": "BRAND#acme", ":prefix": `STORE#${prefix}` },}));return Items.map((i) => i.SK);}console.log(await storesUnder("US#WA#"));console.log(await storesUnder("US#"));
Output
[ 'STORE#US#WA#Seattle#s1', 'STORE#US#WA#Spokane#s2' ] [ 'STORE#US#OR#Portland#s3', 'STORE#US#WA#Seattle#s1', 'STORE#US#WA#Spokane#s2' ]
End each level with the delimiter in the prefix (US#WA#) so WA does not also match a hypothetical WAX region.
Decision framework
- Bounded and always read with the parent → embedded list or map.
- Unbounded, read with the parent, updated individually → item collection.
- Hierarchical filtering by level → composite sort key.
- Children also queried on their own, or the parent partition would grow too large → secondary index.
Best practices
- Estimate the maximum number of children before choosing a strategy.
- Keep sort-key segments in a fixed order and pad numbers (
LINE#001) so they sort correctly. - Page through large collections with
LimitandExclusiveStartKey.
Common mistakes
- Embedding unbounded arrays until items hit the 400 KB limit.
- Unpadded numbers in sort keys (
LINE#10sorts beforeLINE#2). - Updating many duplicated copies of parent data on every parent change.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhy not always embed children in the parent item?+
Answer
2IntermediateQuestionHow do composite sort keys enable hierarchical queries?+
Answer
Hands-on exercise
Hands-on: model product reviews as an item collection under PRODUCT#<id> with sort keys REVIEW#<ISO timestamp>#<reviewId>. Write a function that returns the 5 newest reviews and a cursor for the next page.
Summary
Choose between embedding, item collections, composite sort keys, and secondary indexes based on child count, update frequency, and how children are read.