DynamoDB Streams
Captures events in DynamoDB tables in near real-time, in the order they occurred. Each DynamoDB event is represented by a stream record.
Monitored events:
AWS docs
- A new item is added to the table
- An item is updated
- An item is deleted from the table
Core components
A table consists of items, where each item has a primary key (a unique identifier) that distinguishes it from all other items. In addition to the primary key, each item can have its own attributes.
AWS docs
Writing Data
- PutItem/DeleteItem - Write or delete a single item. The primary key is the only required attribute.
- BatchWriteItem - Write or delete up to 25 items in one request.
- UpdateItem - Modify one or more attributes in an item. The primary key is the only required attribute.
Partitions
A partition is an allocation of storage for a table, backed by solid-state drives (SSDs) and automatically replicated across multiple Availability Zones within an AWS Region. You don't have to manage partitions; DynamoDB handles it automatically.
AWS docs
Read Consistency
DynamoDB provides read-committed isolation, ensuring that read operations always return committed values for an item. Reads will never show a view of an item from a write operation that did not ultimately succeed. However, read-committed isolation does not prevent modifications to the item immediately after the read operation.
AWS docs
- Eventually consistent reads (default for DynamoDB) - The results of a recently completed operation may not be guaranteed. Over a short period, the response will eventually reflect the most recent data.
- Strongly consistent reads (supported on tables and LSI) - Returns the most up-to-date data.
Transactions
You can group up to 100 actions in one call and submit them as a single all-or-nothing operation.
AWS docs
- TransactionWriteItems operation can have - Put, Update, Delete and ConditionCheck actions.
- TransactionGetItems operation consists of Get actions.
DynamoDB Accelerator
DynamoDB Accelerator is an in-memory caching service designed to improve DynamoDB performance. DAX addresses three core use cases:
AWS docs
- Reduces response times for eventually consistent reads.
- Simplifies operations and application complexity with a fully managed, API-compatible service.
- For read-heavy or bursty workloads, DAX improves throughput and reduces costs by decreasing the need to overprovision read capacity units.
- A DAX cluster handles
GetItem
,BatchGetItem
,Query
, andScan
operations.
Backup and Restore
DynamoDB offers two types of backup:
AWS docs
- Point-in-time recovery: Fully managed recovery points for up to 35 days, with per-second granularity. Costs are based on table size.
- On-demand backups: Full table backups for long-term retention and archiving.
Throughput Capacity
On-Demand Capacity Mode
AWS docs
- Pay only for what you use.
- Set maximum read and write throughput per table to control costs.
- Throttling may occur if throughput exceeds twice the previous peak within a 30-minute window.
- Pay for provisioned throughput.
- Use auto-scaling to adjust the provisioned level based on utilization.
- Reserved capacity offers discounts for 1 or 3-year upfront commitments on reads and writes.
- Proactively scale read and write capacity to prevent throttling.
- Available for reads, writes, or both.
- Applies to on-demand and provisioned tables and incurs an additional charge.
Service Quotas
- Read Capacity Unit (RCU): 1 strongly consistent read per second, or 2 eventually consistent reads per second, for items up to 4 KB in size.
- Write Capacity Unit (WCU): 1 write per second for items up to 1 KB.
- Table Size: No limit.
- Number of Tables: Initial quota of 2500 tables per AWS Region.
- Page Size for Query or Scan: 1 MB per page; if exceeded, the `LastEvaluatedKey` property is returned for the next page.
- Local Secondary Indexes (LSI): Up to 5 per table.
- Global Secondary Indexes (GSI): Default quota of 20 per table.
- Item Size: Maximum of 400 KB.
Expressions
Use expressions to specify which attributes to read from an item, write data when a condition is met, specify how to update an item, define queries, and filter the results of a query:
AWS documentation
- Projection expression: Specify which attributes to retrieve using
GetItem
,Query
, orScan
. - Condition expression: Use built-in functions and logical evaluations to specify which items to update with
PutItem
,UpdateItem
, andDeleteItem
operations. - Filter expression: Filter the items returned by a
Query
operation.
Atomic Counter
The
AWS documentation
UpdateItem
operation can be used to implement an atomic counter—a numeric value that is incremented or decremented with each UpdateItem
call.
Key points:
- Non-idempotent updates: If an
UpdateItem
operation fails, the counter might still be updated, resulting in slight over-counting or under-counting. - Increment flexibility: You can specify a positive or negative increment value with each call.
Primary key
A primary key is a unique attribute required for each item in a table. There are two types of primary keys:
AWS docs
- Partition key - A simple primary key that consists of a single attribute.
- Partition key and sort key - A composite primary key consisting of two attributes. The combination of the partition key and sort key must be unique across the table. The partition key determines the physical partition where the item will be stored, while the sort key provides additional flexibility for retrieving items.
Secondary indexes
One or more secondary indexes can be created on a table, offering more flexibility when querying data. AWS creates a copy of the table data to build the index. There are two types of secondary indexes:
AWS docs
Global Tables
Automatically replicates tables across multiple AWS Regions.
AWS docs
- TTL (Time-to-Live) is automatically replicated to other regions.
- Replication is completed within seconds.
- Each region's table has an independent stream.
Global Secondary Index
A global secondary index includes a subset of attributes from the base table, organized by a different primary key. The index key does not need to include any of the table's key attributes and can have a different schema.
Attribute projections:
AWS docs
- KEYS_ONLY: Each index item contains only the table's partition and sort key values, plus the index key values.
- INCLUDE: In addition to the attributes in KEYS_ONLY, the index includes specific non-key attributes.
- ALL: The index includes all attributes from the source table.
Local Secondary Index
A local secondary index allows an additional sort key for the same partition key.
Attribute projections:
AWS docs
- KEYS_ONLY: Each index item contains only the table's partition and sort key values, plus the index key values.
- INCLUDE: Includes specific non-key attributes in addition to KEYS_ONLY attributes.
- ALL: The index includes all attributes from the source table.
LSI vs GSI
- Require strongly consistent reads.
- Want to avoid additional charges (writes to a GSI consume the write capacity units of both the base table and the GSI).
- Plan to use the same partition key.