Amazon DynamoDB Tutorial 0/30 lessons ~6 min read Lesson 19

    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.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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_IMAGES lets 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:

    bash
    aws dynamodb update-table \
    --table-name ShopTable \
    --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES
    aws 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):

    json
    {
    "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

    1Stream pipeline
    1 / 5

    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.

    2 questions
    1IntermediateQuestionWhat ordering guarantees do DynamoDB Streams provide?+

    Answer

    Records for a given item appear in the order the modifications were made. There is no ordering guarantee across different items or partitions.
    2AdvancedQuestionStreams vs Kinesis Data Streams for DynamoDB?+

    Answer

    DynamoDB Streams retains records for 24 hours and suits one or two consumers such as Lambda. Kinesis Data Streams supports longer retention, more consumers, and Kinesis tooling, but adds cost and does not guarantee the same per-item de-duplication, so consumers must handle duplicates.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.