DynamoDB advanced
DAX caching, DynamoDB Streams and Kinesis Data Streams, global tables, time to live, point-in-time recovery and on-demand backups, and export to and import from S3.
DynamoDB Accelerator (DAX)
DAX is a fully managed, highly available in-memory cache for DynamoDB.
- It solves read congestion: when many requests read the same items ("hot keys"), DAX answers from memory.
- Microsecond latency for cached data, instead of single-digit milliseconds.
- No application logic changes: DAX speaks the same API as DynamoDB. The DynamoDB client is swapped for the DAX client and pointed at the cluster.
- It caches items (
GetItem) and query and scan results (Query,Scan). The default TTL is 5 minutes. - Writes go through DAX to DynamoDB, then into the cache.
- A DAX cluster has one or more nodes in the VPC.
DAX serves eventually consistent reads. A strongly consistent read passes straight through to DynamoDB, so DAX doesn't help apps that need strong consistency.
DAX vs ElastiCache
- DAX: caches DynamoDB items, queries and scans, with no code changes.
- ElastiCache: stores things the application computes, such as the result of aggregating many items. Use both together if needed.
Stream processing
A stream is an ordered record of item-level changes (create, update, delete) in a table. Use cases:
- React in real time, for example send a welcome email when a new user item appears.
- Real-time usage analytics.
- Insert into derived tables, such as a summary table.
- Cross-Region replication (global tables use this idea).
- Invoke Lambda on every change.
DynamoDB offers two kinds of stream:
- Records kept for 24 hours.
- A limited number of consumers.
- Processed with Lambda triggers, or the DynamoDB Streams Kinesis Adapter (with the Kinesis Client Library).
- The stream view type sets what each record holds: keys only, the new item, the old item, or both.
Kinesis Data Streams for DynamoDB (newer):
- Records kept up to 1 year.
- Many consumers.
- Processed with Lambda, Managed Service for Apache Flink, Data Firehose (to S3, Redshift, OpenSearch) and Glue streaming ETL.
See Kinesis and Data Firehose.
Global tables
A global table keeps replicas of a table in several Regions.
- Low latency for users around the world: each reads and writes the replica in their Region.
- Active-active (multi-active) replication: apps can read and write in any Region.
- Changes replicate to the other Regions, usually within a second.
- If the same item is changed in two Regions at once, the last writer wins.
Global tables also offer multi-Region strong consistency (MRSC): a write is confirmed only when it's safe in more than one Region, so a strongly consistent read in any Region sees the latest data. It needs exactly three Regions (or two replicas and a "witness"). The default is multi-Region eventual consistency.
Updated since the slides
The slides say to enable DynamoDB Streams before creating a global table. In the current version of global tables, DynamoDB manages replication itself. The new multi-Region strong consistency mode is also newer than the slides.
Time to live (TTL)
TTL deletes items automatically after an expiry time.
- An attribute is chosen (for example
ExpTime) that holds the expiry as a Unix epoch timestamp in seconds (a Number). - A background process finds expired items and deletes them, typically within a few days of expiry. Until then they can still appear in reads, so filter them out if it matters.
- TTL deletes don't use write capacity and cost nothing.
- Deletions appear in DynamoDB Streams, so expired items can be archived.
Use cases: keep only current items to save storage, meet data-retention rules, and expire web sessions.
Backups for disaster recovery
- Point-in-time recovery (PITR): optional continuous backups. Restore the table to any second in the recovery window, which is 35 days by default (configurable from 1 to 35). The restore creates a new table.
- On-demand backups: full backups kept until deleted, for long-term retention. They don't affect performance or latency. Manage them in AWS Backup to copy them to other Regions or accounts. Restoring also creates a new table.
Integration with Amazon S3
- Export to S3 (needs PITR on):
- from any point in time in the PITR window, as a full or incremental export;
- doesn't use read capacity or affect the table;
- in DynamoDB JSON or Amazon Ion format;
- then query it with Athena, keep snapshots for auditing, or transform it (ETL) and import it back.
- Import from S3:
- from CSV, DynamoDB JSON or Ion files;
- creates a new table and doesn't use write capacity;
- import errors are logged in CloudWatch Logs.
Real-world examples
- A product catalogue gets the same few hundred items read millions of times a day. DAX answers in microseconds and DynamoDB read costs drop.
- A sign-up service writes a user item; DynamoDB Streams triggers a Lambda function that sends the welcome email.
- A global chat app uses a global table in three Regions, so every user writes to a nearby Region.
- A session table sets TTL on each session item, so old sessions delete themselves for free.
- An analytics team exports a table to S3 every night and queries it with Athena, without touching the live table's capacity.
Exam tips
- "Microsecond reads for DynamoDB", "hot items", "no code changes" → DAX.
- "Cache aggregation results computed by the app" → ElastiCache.
- "React to item changes", "trigger Lambda on insert" → DynamoDB Streams.
- "Keep change records longer than 24 hours", "many consumers" → Kinesis Data Streams for DynamoDB.
- "Multi-Region, active-active, low latency reads and writes everywhere" → global tables.
- "Delete old items automatically", "expire sessions" → TTL.
- "Restore to a point in time in the last 35 days" → PITR. "Keep backups for years" → on-demand backups (AWS Backup for cross-Region copies).
- "Analyse DynamoDB data with SQL without using capacity" → export to S3, query with Athena.
- "Load a large CSV into a new DynamoDB table cheaply" → import from S3.
Common traps
- "DAX speeds up strongly consistent reads." Strongly consistent reads pass through to DynamoDB.
- "TTL deletes items the second they expire." It typically takes up to a few days.
- "Restore a DynamoDB backup over the existing table." Restores always create a new table.
- "Export to S3 without PITR." Export needs point-in-time recovery turned on.
Hands-on lab: expire items with TTL and watch a stream
Goal: turn on a stream and TTL for a table, and see a change record appear.
Costs money
A few on-demand requests and stream reads cost less than a cent. Any Region works.
Before you start: create the saa-games table from the DynamoDB lab.
Turn on the stream
Open saa-games → Exports and streams → DynamoDB stream details → Turn on, view type New and old images.
Turn on TTL
Additional settings → Time to Live (TTL) → Turn on, attribute name ExpTime. Use Run preview with the current time to see which items would expire.
Add an expiring item
Get the current epoch time (in CloudShell, date +%s), subtract 60, and create an item u9 / 1 with ExpTime set to that Number. The item is already expired; DynamoDB will delete it in the background, typically within a few days.
See the change record
In CloudShell:
ARN=$(aws dynamodb describe-table --table-name saa-games --query Table.LatestStreamArn --output text)
SHARD=$(aws dynamodbstreams describe-stream --stream-arn $ARN --query 'StreamDescription.Shards[0].ShardId' --output text)
IT=$(aws dynamodbstreams get-shard-iterator --stream-arn $ARN --shard-id $SHARD --shard-iterator-type TRIM_HORIZON --query ShardIterator --output text)
aws dynamodbstreams get-records --shard-iterator $ITYou see an INSERT record for the item you created, with its new image.
Expected result: every change to the table shows up as an ordered record in the stream, and expired items are queued for free deletion.
Clean up: delete the saa-games table. Its stream goes with it.
AWS documentation
Amazon DynamoDB
DynamoDB as a serverless NoSQL database, tables, items, attributes and primary keys, data types, table classes, and on-demand vs provisioned capacity.
DocumentDB, Neptune and Keyspaces
Amazon DocumentDB for MongoDB workloads, Amazon Neptune for graphs and Neptune Streams, and Amazon Keyspaces for Apache Cassandra.