Lesson 5 of 6 Amazon DynamoDB

Reacting to Table Changes with DynamoDB Streams

This lesson explains how DynamoDB Streams records every item change in order, so other systems can react to inserts, updates and deletes without changing the code that writes the data.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how streams record changes.
  • Identify typical uses such as search synchronisation.
  • Choose between DynamoDB Streams and Kinesis Data Streams for DynamoDB.

How streams work

DynamoDB Streams records every change to the items in a table as an ordered sequence of change records for each item, kept for 24 hours. The stream's view type determines what each record contains: only the keys, the new version of the item, the old version, or both.

AWS Lambda consumes the stream through an event source mapping, invoking a function with batches of changes, so applications can react to data changes without polling the table.

The following diagram shows a stream feeding a Lambda function.

Reacting to changes with DynamoDB Streams. Every change to an item is recorded in the stream, ordered per item and kept for 24 hours, with the keys, new image, old image or both. Lambda reads the stream through an event source mapping and is invoked with batches of changes, for example to keep a search index in sync.
The search index is illustrative. Records expire after 24 hours, so a consumer must not fall that far behind.

Typical uses

Streams support many event-driven uses. A Lambda function can keep an Amazon OpenSearch Service index synchronised with the table, so items become searchable as soon as they change. It can notify customers when an order's status changes, maintain aggregate counters or materialised views that would be expensive to calculate on demand, or archive items to Amazon S3 as TTL deletes them.

Select all that apply

Uses of streams

Exam skills 2.1.1 · 2.1.3

Identify the event-driven uses of DynamoDB Streams.

Log in to try this activity Interactive activities are free with an account.

Streams or Kinesis Data Streams for DynamoDB?

DynamoDB can publish every change to a table in two ways. DynamoDB Streams is built in: it keeps changes for 24 hours, presents each change once and in order for each item, and supports a small number of simultaneous readers. It is the usual trigger for Lambda functions that react to changes, such as sending a confirmation when an order is written.

Kinesis Data Streams for DynamoDB sends the changes to a Kinesis data stream instead, where they can be kept for up to 365 days and read by many consumers, including Amazon Data Firehose and Managed Service for Apache Flink, which suits analytics pipelines. The following table summarises the differences.

DynamoDB Streams
Retention24 hours
ConsumersFew
Use forEvent-driven processing with Lambda
Kinesis Data Streams for DynamoDB
RetentionUp to 365 days
ConsumersMany, including Firehose and Flink
Use forAnalytics pipelines

24hours

DynamoDB Streams retention

365days

Maximum retention in Kinesis Data Streams

Sort into groups

Streams or Kinesis?

Exam skills 3.5.2

Decide which change feed fits each requirement.

Log in to try this activity Interactive activities are free with an account.
Worked example

Keeping search in sync

Products are stored in DynamoDB, and customers search them in OpenSearch. The search index must reflect changes within seconds, without changing the code that writes products.

ComponentRole
DynamoDB Streams with new and old imagesRecords every insert, update and delete
Lambda event source mappingReads the stream and updates the OpenSearch index
On-failure destinationCaptures records that repeatedly fail, so they do not block the stream

A zero-ETL integration from DynamoDB to OpenSearch Service is an alternative that needs no function at all.

Check yourself

How long does DynamoDB Streams keep changes?

Show answer

24 hours. Kinesis Data Streams for DynamoDB keeps changes for up to 365 days and supports more consumers.

Exam Tips

Remember for the exam

  • To react to item changes with Lambda, use DynamoDB Streams.
  • To keep a search index in sync with a table, use DynamoDB Streams with Lambda.
  • For longer retention and many consumers of table changes, use Kinesis Data Streams for DynamoDB.