Amazon DynamoDB
DynamoDB as a serverless NoSQL database, tables, items, attributes and primary keys, data types, table classes, and on-demand vs provisioned capacity.
What it is
Amazon DynamoDB is a fully managed, serverless NoSQL database. There are no servers or instances to choose: create a table and start reading and writing.
- Highly available: data is replicated across three AZs in the Region.
- Massive scale: millions of requests per second, trillions of items, hundreds of TB, with single-digit millisecond performance whether there are ten users or a hundred million.
- Not relational: no joins and no SQL schema, but it does support transactions.
- Security through IAM: who can read or write which table (or even which items) is an IAM policy.
- No maintenance or patching, always available. Low cost, with automatic scaling.
AWS built DynamoDB for its own shopping cart: a workload that needs every request to be fast, at any scale.
Tables, items and keys
- DynamoDB is made of tables. Each table holds any number of items (like rows).
- Each item has attributes (like columns). Attributes can be added at any time, and items don't all need the same ones. So the schema can change quickly: new items just carry new attributes.
- An item can be at most 400 KB.
- Every table has a primary key, chosen when the table is created:
- Partition key only: each item has a unique value, such as
User_ID. DynamoDB hashes it to decide where the item is stored. - Partition key + sort key (a composite key): items with the same partition key are stored together, sorted by the sort key. In the example, one user (
873e0634) has many games, each identified byGame_ID.
- Partition key only: each item has a unique value, such as
- Pick a partition key with many different values that are used evenly, so the load spreads across the table.
Data types:
- Scalar: String, Number, Binary, Boolean, Null.
- Document: List, Map (nested JSON-like data).
- Set: String Set, Number Set, Binary Set.
Capacity modes
How DynamoDB handles read and write throughput is chosen per table. Modes can be switched later.
On-demand mode
- Reads and writes scale up and down automatically with the traffic. No capacity planning.
- Billing is per request (read request units and write request units). If nothing happens, throughput costs nothing.
- Great for unpredictable workloads, sudden steep spikes, and new apps where the traffic isn't known yet.
- It costs more per request than well-used provisioned capacity.
Provisioned mode
- The table's supported reads and writes per second are specified, in read capacity units (RCU) and write capacity units (WCU), and billed by the hour whether they're used or not.
- Capacity must be planned ahead. Add auto scaling to raise and lower RCU and WCU to keep usage near a target percentage.
- Requests above the capacity are throttled.
- Cheaper for steady, predictable traffic.
Roughly: 1 WCU = one write per second of up to 1 KB; 1 RCU = one strongly consistent read per second of up to 4 KB, or two eventually consistent reads.
Updated since the slides
The slides say provisioned mode is the default. AWS now makes on-demand the default and recommended mode for most tables, after cutting on-demand prices. The exam logic is unchanged: unpredictable traffic → on-demand; steady, planned traffic → provisioned with auto scaling.
Table classes
- DynamoDB Standard: the default, for most tables.
- DynamoDB Standard-Infrequent Access (Standard-IA): cheaper storage, more expensive reads and writes. For tables where storage is most of the cost, such as old orders, logs or past game history.
Real-world examples
- A mobile game stores each player's profile and game results in DynamoDB, keyed by player ID and game ID. Lookups take a few milliseconds even with millions of players.
- A ticket site gets almost no traffic, then a million requests in a minute when tickets go on sale. On-demand mode absorbs the spike without planning.
- An IoT company writes a steady 5,000 readings a second around the clock. Provisioned capacity with auto scaling is the cheapest fit.
Exam tips
- "Serverless NoSQL", "key-value", "single-digit millisecond", "any scale" → DynamoDB.
- "Schema changes often", "flexible attributes" → DynamoDB.
- "Unpredictable traffic", "sudden spikes", "no capacity planning" → on-demand mode.
- "Steady, predictable traffic, lowest cost" → provisioned mode with auto scaling.
- "Store session data", "shopping cart" → DynamoDB (with TTL), as an alternative to ElastiCache.
- "Mostly stored, rarely read table, reduce cost" → Standard-IA table class.
- "Items larger than 400 KB" → store the object in S3 and its key in DynamoDB.
Common traps
- "DynamoDB supports joins and complex SQL queries." It doesn't; design the keys around how the data is read.
- "Change the primary key later." The key is fixed at creation; changing it needs a new table.
- "Provisioned mode for an unpredictable workload." It throttles above capacity; on-demand handles spikes.
Hands-on lab: a table with a composite key
Goal: create a DynamoDB table with a partition key and sort key, add items with different attributes, and query them.
Costs money
An on-demand table that stores a few items costs less than a cent: you pay per request and per GB stored. Any Region works.
Create the table
DynamoDB → Tables → Create table. Name saa-games, partition key User_ID (String), sort key Game_ID (String). Keep Default settings (on-demand). Tag Project=saa-lab, create.
Add items
Open the table → Explore table items → Create item. Add User_ID = u1, Game_ID = 4421, Score = 92 (Number), Result = Win. Add a second item u1 / 1894 with only Score = 14. Add a third u2 / 4521 with a new attribute Level = 7. No schema change was needed.
Query by partition key
Choose Query, User_ID = u1, run. You get both of u1's games, sorted by Game_ID. Try a Scan too: it reads every item, which is slow and costly on a big table.
Expected result: items with different attributes in one table, and a query that returns one user's items in sort-key order.
Clean up: delete the saa-games table (untick creating a backup).
AWS documentation
Amazon ElastiCache
Managed in-memory caches with Valkey, Redis OSS or Memcached, how they compare, cache security, and the lazy loading, write-through, session store and leaderboard patterns.
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.