Chapter 6: Advanced DynamoDB Features
Introduction
Beyond basic CRUD operations, DynamoDB provides powerful advanced features that enable sophisticated application architectures. This chapter explores DynamoDB Streams, Transactions, Global Tables, TTL, and other advanced capabilities that differentiate DynamoDB from traditional databases. These features did not ship with the original DynamoDB launch in January 2012. They were added incrementally over nearly a decade, each responding to real production pain points that Amazon’s own teams and external customers encountered. DynamoDB Streams arrived in 2014, transactions in November 2018 (at re:Invent), and Global Tables in 2017 with a major V2 overhaul in 2019. Understanding this timeline matters because it reveals how DynamoDB evolved from a simple key-value store into a full-featured database platform — and why some of these features carry design constraints that reflect their bolt-on origins. In modern architectures, these advanced features are what make DynamoDB competitive with systems like Google Cloud Spanner or CockroachDB. Streams enable event-driven architectures that were previously the domain of dedicated message brokers like Kafka. Transactions closed the gap with relational databases for workloads that demand atomicity. Global Tables made DynamoDB a serious contender for multi-region active-active deployments, a pattern that companies like Airbnb, Lyft, and Samsung rely on for global availability.DynamoDB Streams
Overview
DynamoDB Streams captures time-ordered sequences of item-level modifications (create, update, delete) and stores this information for up to 24 hours. The concept of change data capture (CDC) is not unique to DynamoDB — PostgreSQL has logical replication slots, MySQL has the binlog, and MongoDB has the oplog. What makes DynamoDB Streams distinctive is its tight integration with AWS Lambda, which enables fully serverless event-driven pipelines with no infrastructure to manage. Before Streams launched in 2014, teams that needed to react to DynamoDB changes had to implement polling patterns that were both expensive (burning read capacity) and unreliable (missing changes between poll intervals). Streams eliminated an entire category of workarounds.Stream View Types
Lambda Stream Processing
Stream Use Cases
1. Cross-Region ReplicationDynamoDB Transactions
ACID Transactions
DynamoDB supports atomic, consistent, isolated, and durable transactions across multiple items and tables. When AWS announced DynamoDB transactions at re:Invent 2018, it was one of the most requested features in the service’s history. For years, teams had worked around the lack of multi-item atomicity using patterns like the “saga pattern” or application-level two-phase commits — approaches that were error-prone, difficult to debug, and required significant boilerplate. The absence of transactions was the single biggest reason many workloads stayed on relational databases despite DynamoDB’s scaling advantages. The transaction implementation uses a variant of two-phase commit with optimistic concurrency control, which means it trades the possibility of conflict-based retries for the ability to avoid distributed locks entirely — a design choice that preserves DynamoDB’s low-latency characteristics even under transactional workloads.Deep Dive: Transactional Isolation and RDBMS Comparison
While DynamoDB provides ACID transactions, the underlying implementation and isolation guarantees differ from traditional SQL databases like PostgreSQL or MySQL.1. Isolation Levels
In a traditional RDBMS, you can choose betweenRead Uncommitted, Read Committed, Repeatable Read, and Serializable.
- DynamoDB Guarantee: DynamoDB provides Serializability for all items within a single transaction.
- The Catch: This serializability is achieved via Optimistic Concurrency Control (OCC) at the partition level. If two transactions attempt to modify the same item simultaneously, one will succeed and the other will be rejected with a
TransactionCanceledException.
2. The 2-Phase Commit (2PC) under the Hood
DynamoDB uses a specialized 2-Phase Commit protocol managed by a “Transaction Coordinator.”- Phase 1 (Prepare): The coordinator validates conditions for all items in the transaction and “locks” them.
- Phase 2 (Commit): If all conditions pass, the coordinator writes the changes. If any fail, it rolls back all changes.
- Latency Cost: Because it involves multiple round-trips between the coordinator and storage nodes, transactions have higher latency than standard
PutItemorUpdateItemoperations.
3. Key Differences from RDBMS
| Feature | DynamoDB Transactions | Traditional RDBMS (SQL) |
|---|---|---|
| Concurrency | Optimistic (Fail fast on conflict) | Pessimistic (Wait for locks) |
| Deadlocks | Impossible (Uses timestamps/OCC) | Possible (Requires deadlock detection) |
| Rollback | Automatic on condition failure | Manual or automatic |
| Scope | Up to 100 items | Entire database/tables |
| Throughput | Limited per partition | Scalable with hardware/sharding |
Transaction Constraints
Transaction Use Cases
Order ProcessingGlobal Tables
Multi-Region Replication
Global Tables provide managed multi-region, multi-active replication. Multi-region replication is one of the hardest problems in distributed databases. Google solved it with Spanner and TrueTime (using atomic clocks and GPS receivers for global ordering). DynamoDB took a fundamentally different approach: rather than pursuing strong global consistency, Global Tables embrace eventual consistency with last-writer-wins semantics. This makes them simpler to operate and lower-latency for writes (no cross-region coordination on the write path), but it shifts conflict handling to the application layer. In practice, most applications can tolerate this trade-off because true simultaneous writes to the same item from different regions are rare — and when they do occur, the “last write” semantics are often acceptable for use cases like user profiles, session state, or configuration data.Conflict Resolution
Deep Dive: Global Tables and Distributed Consistency
The “multi-active” nature of Global Tables introduces classic distributed systems challenges. While DynamoDB handles the replication, developers must understand the theoretical underpinnings.1. Last Writer Wins (LWW) vs. CRDTs
In the original 2007 Dynamo paper, conflict resolution was handled via Vector Clocks, allowing for complex merging or client-side reconciliation.- Modern DynamoDB Approach: For simplicity and performance, Global Tables use Last Writer Wins (LWW) based on a system-level wall clock (NTP-synchronized).
- The Trade-off: LWW is simpler but can lead to data loss in high-concurrency “write-write” conflict scenarios where the “losing” write is completely overwritten.
- CRDTs (Conflict-free Replicated Data Types): While DynamoDB doesn’t natively expose CRDTs, you can implement them at the application level (e.g., G-Counters or PN-Counters) using the
ADDoperation inUpdateItem, which is commutative.
2. Consistency Model: Eventual but Fast
Global Tables are Eventually Consistent across regions.- Replication Latency: Typically under 1 second globally.
- Theoretical Limit: Because it’s an asynchronous replication model, Global Tables fall into the AP (Availability / Partition Tolerance) category of the CAP theorem. They prioritize being able to write to any region over immediate global consistency.
3. Preventing Replication Loops
DynamoDB uses internal metadata to track the origin region of a write. This ensures that a write replicated from Region A to Region B does not get “re-replicated” back to Region A, preventing infinite loops.Time To Live (TTL)
Automatic Item Expiration
Point-in-Time Recovery (PITR)
PartiQL Support
SQL-Compatible Query Language
Contributor Insights
Identifying Hot Keys
Interview Questions and Answers
Question 1: How do DynamoDB Streams differ from Kinesis Data Streams?
Answer: DynamoDB Streams:- Captures item-level changes in DynamoDB tables
- Retention: 24 hours (fixed)
- Ordering: Per partition key only
- Shards: Managed automatically
- Cost: Included with table (no extra charge for reads)
- Use case: React to DynamoDB changes
- General-purpose streaming service
- Retention: 24 hours to 365 days (configurable)
- Ordering: Per partition key (shard key)
- Shards: Manual management required
- Cost: Per shard-hour + PUT payload units
- Use case: Real-time data ingestion, custom streaming
Question 2: When should you use transactions vs batch operations?
Answer: Use Transactions When:- Need ACID guarantees (all-or-nothing)
- Cross-item consistency required
- Conditional writes across multiple items
- Financial operations, inventory management
- Independent operations (no dependencies)
- Can tolerate partial failures
- Higher throughput needed
- Cost optimization (batch is cheaper)
Question 3: Explain conflict resolution in Global Tables.
Answer: Global Tables use last-writer-wins conflict resolution based on timestamps. How it works:- Each write includes an internal timestamp
- During concurrent writes to same item in different regions
- The write with the latest timestamp wins
- Losing write is discarded
Question 4: How do you implement audit logging with DynamoDB Streams?
Answer:Question 5: What are the limitations of DynamoDB transactions?
Answer: Hard Limits:- Max 100 items per TransactWriteItems
- Max 25 items per TransactGetItems
- Max 4 MB total transaction size
- All items must be in same AWS account/region
- Cannot mix transactions across tables with different billing modes (before 2020)
- Consumes 2x capacity units
- TransactWriteItems: 2 WCUs per KB per item
- TransactGetItems: 2 RCUs per 4KB per item
- Higher latency than non-transactional operations
- Limited throughput per partition
Question 6: How does TTL work and what are its limitations?
Answer: How TTL Works:- Enable TTL on a table attribute
- Set attribute to Unix timestamp (seconds since epoch)
- DynamoDB automatically deletes items within 48 hours after expiration
- Deletions are eventually consistent
- No additional cost for TTL deletions
- Deletion within 48 hours (not immediate)
- Cannot guarantee exact deletion time
- TTL attribute must be Number type (Unix timestamp in seconds)
- Deleted items still consume storage until actually deleted
- GSIs are updated after base table deletion
Question 7: Design a real-time leaderboard using DynamoDB Streams.
Answer: Architecture:- DynamoDB stores user scores
- Stream captures score updates
- Lambda updates Redis sorted set
- Application reads from Redis for rankings
- Sub-millisecond leaderboard queries
- Millions of score updates/sec
- Real-time ranking updates
- Scalable to billions of users
Summary
Advanced Features Overview:-
DynamoDB Streams:
- Captures item-level changes
- Powers real-time processing
- 24-hour retention
- Use for: replication, aggregations, notifications
-
Transactions:
- ACID guarantees
- Up to 100 items (writes) or 25 items (reads)
- 2x capacity cost
- Use for: financial operations, multi-item consistency
-
Global Tables:
- Multi-region, multi-active replication
- Sub-second replication latency
- Last-writer-wins conflict resolution
- Use for: global applications, disaster recovery
-
TTL:
- Automatic item expiration
- No additional cost
- Deletion within 48 hours
- Use for: sessions, temporary data, caching
-
PITR:
- 35-day recovery window
- Point-in-time restore
- Continuous backups
- Use for: disaster recovery, compliance