Amazon DynamoDB
Key design, capacity math, indexes, consistency, streams, global tables, backups and security for DynamoDB, with the limits the exam asks about.
Exam tasks: 2.5 and 4.3 (purpose-built data stores), 2.2 (multi-Region data), 3.2, 3.3
The decision: do you know your access patterns, and do you need single-digit millisecond reads and writes at any scale? If so, how should the keys, capacity mode and replication be set?
DynamoDB is a serverless key-value and document store. Tables scale by partitioning on the partition key, so the design question is always how your keys spread the load.
Designing keys
- Pick a high-cardinality partition key that spreads requests evenly, such as a customer or device ID. A key like
dateorstatusconcentrates traffic on one partition, a hot partition. - Adaptive capacity shifts throughput toward hot items and splits busy partitions, which softens skew. It does not rescue a key with one overwhelmingly popular value.
- To spread a single hot key, add a write-sharding suffix, such as
device#7, and read across the shards. - Model from the access patterns. List the queries first, then choose keys and indexes that answer each one with a
Query. Many teams put several entity types in one table (single-table design) to fetch related items in one call. - Query reads items under one partition key and is efficient. Scan reads the whole table, and a filter expression only drops results after reading, so it does not reduce consumed capacity.
Capacity math
| Operation | Rounding rule | Cost |
|---|---|---|
| Strongly consistent read | Item size rounded up to the next 4 KB | 1 RCU per 4 KB |
| Eventually consistent read | Same rounding | 0.5 RCU per 4 KB |
| Transactional read | Same rounding | 2 RCUs per 4 KB |
| Write | Item size rounded up to the next 1 KB | 1 WCU per 1 KB |
| Transactional write | Same rounding | 2 WCUs per 1 KB |
Worked examples:
- 100 strongly consistent reads per second of 6 KB items: 6 KB rounds up to 8 KB, which is 2 RCUs each, so 200 RCUs. Eventually consistent halves it to 100 RCUs. Transactional doubles it to 400 RCUs.
- 50 writes per second of 3.5 KB items: 3.5 KB rounds up to 4 KB, which is 4 WCUs each, so 200 WCUs.
Exam signal
Round the item size up first, then multiply by the rate. Exam questions hide the trap in an item size that is not a multiple of 4 KB or 1 KB.
Capacity modes, indexes and features
Capacity modes
| On-demand | Provisioned | |
|---|---|---|
| Billing | Per request | Per RCU and WCU per hour |
| Best for | Unknown, spiky or new workloads | Steady, predictable traffic |
| Scaling | Automatic | Auto scaling within limits you set |
| Cost lever | None beyond the mode | Reserved capacity for steady baselines |
You can switch modes on a table periodically, so start on-demand and move to provisioned once traffic is known.
Indexes
| Global secondary index | Local secondary index | |
|---|---|---|
| Keys | Any partition and sort key | Same partition key, different sort key |
| When created | Anytime | Only at table creation |
| Consistency | Eventually consistent | Strong or eventual |
| Capacity | Its own | Shares the table's |
| Size limit | None | 10 GB per partition key value |
| Per table | 20 by default | 5 |
Adding an LSI later
An LSI can't be added to an existing table. If a question needs a new query pattern on a live table, the answer is a GSI (or a new table and a migration).
Other features
- Streams: an ordered, 24-hour log of item changes, consumed by Lambda or the Kinesis Client Library adapter. Use it for triggers, audit and cross-system sync. Kinesis Data Streams for DynamoDB is the alternative for longer retention and more consumers.
- Global tables: multi-Region, multi-active. The default mode is eventually consistent with last writer wins. Multi-Region strong consistency (three Regions, or two plus a witness) gives an RPO of zero at higher write latency.
- TTL deletes expired items in the background for free, usually within a few days of expiry. Deletions appear in the stream, so you can archive them to S3.
- PITR restores to any second in the last 35 days (the window is configurable) into a new table. On-demand backups are kept until you delete them.
- Transactions give all-or-nothing writes across up to 100 items.
- Maximum item size is 400 KB. Store larger objects in S3 and keep a pointer.
Consistency and conditional writes
- Reads are eventually consistent by default. Ask for strong consistency per read on a table or LSI, not on a GSI and not across Regions in a global table's default mode.
- Conditional writes (
attribute_not_exists, a version number) give optimistic locking, so two writers do not overwrite each other. Atomic counters useUpdateItemwith an increment. BatchGetItemreads up to 100 items andBatchWriteItemwrites up to 25. Each item inside a batch still consumes its own capacity, and a throttled item is returned as unprocessed, so retry with backoff.
Table classes, import and export
- Standard is the default. Standard-IA lowers storage cost and raises request cost, for tables that are mostly at rest.
- Export to S3 (from PITR) writes a snapshot without consuming read capacity, which suits analytics with Athena. Import from S3 creates a new table from files.
Security
- Encryption at rest is always on, using an AWS owned key, an AWS managed key or a customer managed key.
- Fine-grained access control uses IAM conditions such as
dynamodb:LeadingKeysto limit a caller to items whose partition key matches their identity, which fits per-user data with web identity federation. - Use a gateway VPC endpoint so traffic stays off the internet, and an endpoint policy to limit which tables.
- Back up across accounts and Regions with AWS Backup.
DynamoDB has no read replicas
There is no read replica feature. Read scale comes from the table itself, DAX for cached reads, or global tables for local reads in other Regions. A question that offers a DynamoDB read replica is a distractor.
DAX
- An in-VPC, write-through cache that speaks the DynamoDB API. Swap the client library and keep your code.
- Caches items (
GetItem,BatchGetItem) and query or scan results separately. - Serves eventually consistent reads. Strongly consistent reads pass straight through to DynamoDB.
- Helps read-heavy, repeated-key workloads. It doesn't help write-heavy tables and doesn't raise write capacity.
- Run at least three nodes across AZs for production.
Scenarios
Five partition key values concentrate traffic on a few partitions, and the hot ones hit their per-partition limit while the table total looks idle. A high-cardinality key spreads load, and a GSI keeps the by-mode query. More table capacity does not change the per-partition limit, the table class only changes cost, and an LSI shares the same partition.
9 KB rounds up to 12 KB, which is 3 units of 4 KB. Strongly consistent reads cost 1 RCU per 4 KB, so each read is 3 RCUs, and 80 reads per second need 240. 160 uses 2 units per read, ignoring the rounding. 720 and 360 multiply by the wrong factors.
Export to S3 reads from the PITR data, so it does not consume read capacity and does not affect traffic. A Scan consumes capacity and may throttle production. DAX is a cache, not an export tool, and a global table is for replication.
Related
- Purpose-built databases: when DynamoDB is the right engine.
- Caching: DAX among the other cache layers.
- Disaster recovery: global tables as an active/active pattern.
Further reading
Purpose-built databases
Choosing between RDS, Aurora, DynamoDB, DocumentDB, Neptune, Keyspaces, Timestream, MemoryDB, Redshift and OpenSearch, and the DynamoDB and Aurora features the exam tests.
Amazon RDS and Aurora
RDS engines, Multi-AZ and read replicas, Aurora storage and Global Database, backups, encryption, RDS Proxy, Blue/Green deployments and the exam traps.