DynamoDB Streams is an optional feature that captures data modification events in DynamoDB tables. The data about these events appear in the stream in near-real time, and in the order that the events occurred.
Each event is represented by a stream record. If you enable a stream on a table, DynamoDB Streams writes a stream record whenever one of the following events occurs:
A new item is added to the table: The stream captures an image of the entire item, including all of its attributes.
An item is updated: The stream captures the "before" and "after" image of any attributes that were modified in the item.
An item is deleted from the table: The stream captures an image of the entire item before it was deleted.
Each stream record also contains the name of the table, the event timestamp, and other metadata. Stream records have a lifetime of 24 hours; after that, they are automatically removed from the stream.
You can use DynamoDB Streams together with AWS Lambda to create a trigger—code that runs automatically whenever an event of interest appears in a stream.
For example, consider a Customers table that contains customer information for a company. Suppose that you want to send a "welcome" email to each new customer. You could enable a stream on that table, and then associate the stream with a Lambda function. The Lambda function would run whenever a new stream record appears, but only process new items added to the Customers table. For any item that has an EmailAddress attribute, the Lambda function would invoke Amazon Simple Email Service (Amazon SES) to send an email to that address.
Key Features and Characteristics
Change Capture: Streams capture changes to items in a DynamoDB table, recording the item-level modifications.
Time-Ordered Sequence: Changes are recorded in a time-ordered sequence, which preserves the order in which the changes occurred.
Retention Period: The data in the stream is retained for up to 24 hours.
Encryption: Logs are encrypted at rest.
Access: Streams are accessed using a dedicated endpoint.
Event Source for Lambda:
DynamoDB Streams can be configured as an event source for AWS Lambda. Lambda functions can be triggered to execute code in response to changes recorded in the stream.
Lambda polls the DynamoDB stream and processes the events as they occur.
Stream Specification:
The StreamSpecification parameter in the CreateTable or UpdateTable API operations is used to enable or modify a stream. It includes the following:
StreamEnabled: Specifies whether a stream is enabled (true) or disabled (false) for the table.
StreamViewType: Specifies the type of information written to the stream whenever an item is modified. The options are:
KEYS_ONLY: Only the primary key attributes of the modified item.
NEW_IMAGE: The entire item, as it appears after it was modified.
OLD_IMAGE: The entire item, as it appeared before it was modified.
NEW_AND_OLD_IMAGES: Both the new and the old images of the item.
Usage Scenarios
Replication: Use DynamoDB Streams to replicate data changes across multiple tables or regions.
Auditing: Track and log all changes to items in a table for audit purposes.
Materialized Views: Create and maintain materialized views by applying the item-level changes recorded in the stream to another data store.
Event-Driven Processing: Trigger actions based on changes to items in a table. For example, send notifications, update search indexes, or process business logic whenever an item is modified.
Real-Time Analytics: Analyze data changes in real-time by processing the change events through Lambda functions or other stream processing services.