0
0
DynamodbConceptBeginner · 3 min read

DynamoDB Stream View Types: What They Are and How to Use Them

In DynamoDB, stream view types define what information about item changes is recorded in a stream. The four types are KEYS_ONLY, NEW_IMAGE, OLD_IMAGE, and NEW_AND_OLD_IMAGES, each capturing different parts of the changed data.
⚙️

How It Works

DynamoDB streams capture changes made to items in a table, like a diary that records every update. The stream view type decides what details about these changes are saved in the stream.

Think of it like taking photos during an event: KEYS_ONLY is like snapping just the name tags of people who changed, NEW_IMAGE is a photo after the change, OLD_IMAGE is a photo before the change, and NEW_AND_OLD_IMAGES is like having both photos side by side.

This helps applications react to changes efficiently by choosing how much data they need from the stream.

💻

Example

This example shows how to enable a DynamoDB stream with the NEW_AND_OLD_IMAGES view type using AWS CLI. This setting captures both the item before and after the change.

bash
aws dynamodb update-table \
  --table-name MusicCollection \
  --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES
Output
{ "TableDescription": { "TableName": "MusicCollection", "StreamSpecification": { "StreamEnabled": true, "StreamViewType": "NEW_AND_OLD_IMAGES" }, "LatestStreamArn": "arn:aws:dynamodb:region:account-id:table/MusicCollection/stream/timestamp" } }
🎯

When to Use

Choose KEYS_ONLY when you only need to know which items changed, saving bandwidth and processing time.

Use NEW_IMAGE if you want to react to the latest state of an item, like updating a cache or search index.

OLD_IMAGE is useful for auditing or rollback scenarios where you need the previous data.

NEW_AND_OLD_IMAGES is best when you want to compare before and after states, such as syncing data or triggering complex workflows.

Key Points

  • KEYS_ONLY: Only primary key attributes of changed items are recorded.
  • NEW_IMAGE: The entire item after the change is recorded.
  • OLD_IMAGE: The entire item before the change is recorded.
  • NEW_AND_OLD_IMAGES: Both before and after images of the item are recorded.
  • Choosing the right view type balances detail needed and resource use.

Key Takeaways

DynamoDB stream view types control what item data changes are captured in streams.
Use KEYS_ONLY to track changed items with minimal data.
NEW_IMAGE captures the updated item state after changes.
OLD_IMAGE captures the item state before changes for auditing.
NEW_AND_OLD_IMAGES provides full before-and-after data for detailed processing.