Denormalization in DynamoDB
Coming from SQL, denormalization sounds like a sin: duplicated data, no single source of truth. In DynamoDB it's the whole point. There are no joins, so you copy related data onto the item that needs it and read it back in one shot.
What is denormalization in DynamoDB?
Denormalization in DynamoDB means copying related data onto the item that reads it, so a single request returns everything a screen needs. Because DynamoDB has no joins, you pre-join at write time instead of stitching tables together at read time.
- Two flavors. Embed nested data in a complex attribute on one item, or duplicate a value across many items.
- The footgun is staleness. When the source changes, every copy is wrong until you fan out the update. Only duplicate values that rarely change.
- It buys reads, and you pay in writes. You trade more (and more careful) writes for cheap, single-request reads.
Why there are no joins to fall back on
A relational JOIN reassembles normalized rows at read time. DynamoDB has no
join: a Query reads one item collection and hands back exactly
what's stored there. Nothing stitches two tables together for you on the
production read path.
So the data has to already be shaped for the read. If a screen needs a post and its author's name, that name must live somewhere the post read already touches.
Pattern 1: embed with a complex attribute
DynamoDB attributes can hold nested maps and lists, not just scalars. So one common form of denormalization is putting a child object directly inside its parent item instead of giving it its own item.
A post with its tags and a small author snapshot, all on one item:
| PK | SK | author | tags |
|---|---|---|---|
| POST#9f3 | META | {id: USER#12, name: "Mara Vance"} | ["dynamodb","aws"] |
One GetItem returns the post, the tags, and the author block together. No
second read. This fits data that's owned by the parent and bounded in
size: a handful of tags, one author snapshot.
A single DynamoDB item maxes out at 400 KB, attribute names and values included (DynamoDB item size limit). Embed an unbounded list (every comment on a viral post) and you'll blow past it.
Store unbounded children as their own items in the parent's collection instead
(PK = POST#9f3, SK = COMMENT#<timestamp>) and read them a page at a time with
Query + Limit, continuing from LastEvaluatedKey. See
one-to-many relationships.
If the post screen needs a preview, keep a bounded "latest 3" snapshot on the parent. For one large value (a long body, an image), store it in S3 and keep the object key on the item.
No transaction spans S3 and DynamoDB, so order the writes: upload the object first, then write the item that points to it. A failed item write then leaves an orphaned object, never an item pointing at nothing.
Put the item's primary key in the object's S3 metadata and clear orphans with a
periodic sweep. Skip objects younger than your slowest upload plus item write
(S3 LastModified, say an hour; for a multipart upload it is when the upload
started), or the sweep can delete an object whose item is still being written.
Delete an object only when a strongly consistent GetItem (ConsistentRead: true)
on the key in its metadata finds no item, or an item that points at a different
object (a replaced body). The default eventually consistent read can miss a fresh
item (AWS: storing large attributes in S3,
read consistency).
Pattern 2: duplicate a value across items
The blog case is the textbook one. You list posts and want each row to show the author's display name, without a second read per post to fetch it.
So you write the author's name onto each post item when the post is created:
| PK | SK | authorId | authorName | title |
|---|---|---|---|---|
| POST#9f3 | META | USER#12 | "Mara Vance" | "Modeling 1:N" |
| POST#a71 | META | USER#12 | "Mara Vance" | "Sparse GSIs" |
| POST#b04 | META | USER#88 | "Lio Tan" | "Query vs Scan" |
A GSI over posts (GSI1PK = "POST") renders the list, title and author,
with no per-row lookup, and a second GSI keyed by author serves the fan-out below. A Query returns up to 1 MB per
page, so a long list paginates.
A constant GSI1PK sends every post's index write to one key. That's fine for a
blog; at high post volume, shard it as
hot partitions shows.
The author name is denormalized: the canonical copy lives on USER#12, and
every post carries its own copy. You've turned an N+1 read into one read, at the
cost of holding "Mara Vance" in N+1 places.
Embed vs. duplicate: which one
| Embed (complex attribute) | Duplicate (copy across items) | |
|---|---|---|
| Shape | child nested inside parent | same value on many items |
| Best for | bounded, parent-owned data | a shared value many items display |
| Read | one GetItem | one Query |
| Update cost | rewrite the one parent item | fan out to every copy |
| Size risk | 400 KB item cap | none per item |
Reach for embed when the child only ever appears with its parent. Reach for duplicate when many independent items need to show the same shared value.
The footgun: stale copies
Mara renames herself to "Mara V." You update USER#12. Every post item still
says "Mara Vance" until you go fix them.
So updating a duplicated value is a fan-out write: you query every affected item and rewrite each one, guarded so you only touch rows that still hold the old value:
UPDATE PK = POST#9f3, SK = META
SET authorName = "Mara V."
WHERE authorName = "Mara Vance"
The key needs both PK and SK. A rerun fails its condition with
ConditionalCheckFailedException, so the loop is safe to repeat. You can compose
that conditional SET in the
Expression Builder and copy the generated
UpdateExpression and ConditionExpression into your code.
Fan-out is a write per item. Query the author-keyed GSI for that author's posts, then issue the updates. The sequence:
GSIs are updated asynchronously, so the fan-out query can miss a post written seconds earlier, and a post created mid-rename can copy the old name. Re-run the fan-out query after a short delay to catch stragglers.
In production, move the loop off the request path: a Lambda function reading the
table's DynamoDB stream sees the change to USER#12 and
applies the post updates after the source write. By default Lambda retries a failed
batch until the stream record expires (24 hours), so a fan-out that dies half-way is
retried, not lost, and the guarded update makes the retry safe.
A record that never succeeds blocks its shard for that whole day and is then
discarded. Cap the retries (MaximumRetryAttempts, MaximumRecordAgeInSeconds), turn
on BisectBatchOnFunctionError to isolate the bad record, and add an on-failure
destination for what still fails: SQS or SNS gets the shard and sequence numbers to
re-read while the stream still holds them, and S3 keeps the records themselves.
Handle consumer failures has the
command.
In DynoTable, a fan-out you stage lands in the staging area first, as a per-attribute diff per item you review before committing (editing and staging are on paid plans).
The rule holds: a display name, a plan tier or a category label is fine to copy. A live counter or a frequently edited field is not, because the fan-out costs a write per copy on every change.
Fan-out write cost
Each copy you update is a separate billed write, sized on the whole item (the
larger of before and after), not on the attribute you change. Every GSI that
projects authorName takes one more write per post.
With both GSIs above projecting it, updating 50 posts of ≤ 1 KB costs 50 × (1 + 2) = 150 write request units on-demand, about $0.00009 at us-east-1 list price. A guarded write whose condition fails is still billed.
The read side stays one Query; the write side scales with copies × indexes.
Enter the writes per second times (1 + GSI count) in the
pricing calculator to see the monthly cost.
When normalization still wins
If a value changes often, or one item is read by unpredictable patterns, keep it normalized. Denormalization is an optimization for known, read-heavy access patterns.
The extra read doesn't have to be N+1. After the list Query, collect the distinct
authorIds on that page and fetch them in one BatchGetItem (up to 100 keys; retry
any UnprocessedKeys). That's two requests, and the volatile field lives in one
place. See batch operations.
To decide where these duplicated attributes live, model the access patterns first: see single-table design and, for the read side of the trade, Query vs Scan.
Download DynoTable to inspect a denormalized table and run the fan-out
update against your own data. To spot drift, its
SQL Workbench (paid plans) can
GROUP BY authorId, authorName over the posts: an author listed under two names has stale copies.