DynamoDB Streams: Change Data Capture
DynamoDB Streams records every item-level change — insert, modify, remove — in near real time, in the order the changes happened for each item.
Introduction
DynamoDB Streams records every item-level change — insert, modify, remove — in near real time, in the order the changes happened for each item. Records are kept for 24 hours. Streams are how DynamoDB tables feed search indexes, caches, analytics, notifications, and replicas without changing the write path.
Purpose of this lesson
You will enable a stream, choose a view type, and read stream records so you understand exactly what consumers receive.
Understanding the topic
Stream fundamentals:
- View types:
KEYS_ONLY,NEW_IMAGE,OLD_IMAGE,NEW_AND_OLD_IMAGES. Choose the smallest one your consumers need;NEW_AND_OLD_IMAGESlets consumers compute diffs. - Event names:
INSERT,MODIFY,REMOVE. - Ordering: changes to the same item appear in order; there is no global order across items.
- Exactly once in the stream: each change appears once, but consumers may process a record more than once on retries, so consumers must be idempotent.
- Shards: the stream is split into shards that follow table partitions; AWS recommends no more than two concurrent readers per shard.
- Kinesis Data Streams for DynamoDB is an alternative destination with longer retention and more consumers.
Syntax reference
Enable a stream on ShopTable:
aws dynamodb update-table \--table-name ShopTable \--stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGESaws dynamodb describe-table --table-name ShopTable \--query "Table.LatestStreamArn" --output text
Informative example
What a MODIFY record looks like (an order moving from PLACED to SHIPPED):
{"eventID": "4b6f…","eventName": "MODIFY","eventSource": "aws:dynamodb","awsRegion": "us-east-1","dynamodb": {"Keys": { "PK": { "S": "ORDER#o1" }, "SK": { "S": "HEADER" } },"OldImage": { "PK": { "S": "ORDER#o1" }, "SK": { "S": "HEADER" }, "status": { "S": "PLACED" }, "total": { "N": "59" } },"NewImage": { "PK": { "S": "ORDER#o1" }, "SK": { "S": "HEADER" }, "status": { "S": "SHIPPED" }, "total": { "N": "59" } },"SequenceNumber": "111100000000000123456789","SizeBytes": 142,"StreamViewType": "NEW_AND_OLD_IMAGES"}}
Images use the low-level attribute-value format. Convert them with unmarshall from @aws-sdk/util-dynamodb before using them in application code.
Execution workflow
Write
The application writes to ShopTable as usual; the write path does not change.
Real-world use
Common uses: keep an OpenSearch index in sync for full-text product search, publish OrderShipped events to EventBridge, maintain per-customer order counts, invalidate caches, and audit changes to sensitive items.
Best practices
- Pick the smallest view type that satisfies all consumers.
- Make consumers idempotent using the item key plus a version or the record's sequence number.
- Alarm on consumer iterator age so you notice before the 24-hour retention window is at risk.
Common mistakes
- Assuming global ordering across different items.
- Attaching many independent consumers to one stream — use EventBridge Pipes, a fan-out function, or Kinesis Data Streams instead.
- Letting a consumer fall more than 24 hours behind, which loses records.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1IntermediateQuestionWhat ordering guarantees do DynamoDB Streams provide?+
Answer
2AdvancedQuestionStreams vs Kinesis Data Streams for DynamoDB?+
Answer
Hands-on exercise
Hands-on: enable a NEW_AND_OLD_IMAGES stream, update one order's status twice, and use aws dynamodbstreams get-shard-iterator and get-records to read the records. Identify the old and new status values in each.
Summary
Streams capture item-level changes for 24 hours with per-item ordering. Choose a minimal view type, build idempotent consumers, and watch iterator age.