Amazon DynamoDB for Beginners: NoSQL Tables, Keys, Capacity, Indexes, Backups and Common Patterns
SQL vs NoSQL
| Relational (RDS) | DynamoDB (NoSQL) |
| Data shape | Tables with fixed columns, joins | Items with flexible attributes, no joins |
| Scaling | Mostly vertical, read replicas | Horizontal, almost unlimited |
| Speed | Depends on queries and size | Single-digit millisecond at any scale |
| Management | Instances, patches, backups | Serverless, no servers |
| Best for | Complex queries and transactions | Key lookups, high traffic, sessions, carts, IoT, leaderboards |
Core Ideas
| Term | Meaning |
| Table | Collection of items |
| Item | One record (like a row), up to 400 KB |
| Attribute | A field in an item (like a column), can differ between items |
| Partition key | Required key. DynamoDB hashes it to decide where data is stored |
| Sort key | Optional second key to keep items with the same partition key in order |
| Primary key | Partition key alone, or partition key + sort key. Must be unique per item |
json
{
"UserId": "u-100",
"OrderId": "2026-11-17#0001",
"Total": 499,
"Items": [{"sku": "A1", "qty": 2}],
"Status": "SHIPPED"
}
Here UserId is the partition key and OrderId is the sort key, so you can quickly fetch all orders of one user in date order.
Create a Table and Use It
bash
aws dynamodb create-table --table-name Orders \
--attribute-definitions AttributeName=UserId,AttributeType=S AttributeName=OrderId,AttributeType=S \
--key-schema AttributeName=UserId,KeyType=HASH AttributeName=OrderId,KeyType=RANGE --billing-mode PAY_PER_REQUEST
aws dynamodb put-item --table-name Orders --item '{"UserId":{"S":"u-100"},"OrderId":{"S":"2026-11-17#0001"},"Total":{"N":"499"},"Status":{"S":"NEW"}}'
aws dynamodb get-item --table-name Orders --key '{"UserId":{"S":"u-100"},"OrderId":{"S":"2026-11-17#0001"}}'
aws dynamodb query --table-name Orders --key-condition-expression "UserId = :u" --expression-attribute-values '{":u":{"S":"u-100"}}'
aws dynamodb update-item --table-name Orders --key '{"UserId":{"S":"u-100"},"OrderId":{"S":"2026-11-17#0001"}}' \
--update-expression "SET #s = :v" --expression-attribute-names '{"#s":"Status"}' --expression-attribute-values '{":v":{"S":"SHIPPED"}}'
aws dynamodb delete-item --table-name Orders --key '{"UserId":{"S":"u-100"},"OrderId":{"S":"2026-11-17#0001"}}'
python
import boto3
from boto3.dynamodb.conditions import Key
table = boto3.resource("dynamodb").Table("Orders")
table.put_item(Item={"UserId": "u-100", "OrderId": "2026-11-17#0002", "Total": 250, "Status": "NEW"})
resp = table.query(KeyConditionExpression=Key("UserId").eq("u-100"))
for item in resp["Items"]:
print(item["OrderId"], item["Status"])
Query vs Scan
- Query: fetch items by partition key (and optionally sort key conditions). Fast and cheap
- Scan: reads the whole table. Slow and costly on big tables. Avoid in normal app paths
- Design your keys around your access patterns (the questions your app asks)
Capacity Modes
| Mode | How it works | Choose when |
| On-demand | Pay per read and write request. No planning. Handles spikes | New or unpredictable traffic, learning |
| Provisioned | You set read and write capacity (RCU/WCU). Can use auto scaling | Steady, predictable traffic (cheaper) |
- RCU: read capacity unit. WCU: write capacity unit
- Too many requests to one partition can cause throttling (
ProvisionedThroughputExceededException). Pick keys with many different values to spread load
Secondary Indexes
| Index | Meaning |
| GSI (Global Secondary Index) | A different partition and sort key for another access pattern. Has its own capacity. Can be added any time |
| LSI (Local Secondary Index) | Same partition key, different sort key. Must be created with the table |
Example: the Orders table is by UserId. To search orders by Status, create a GSI with Status as partition key.
Helpful Features
- TTL (time to live): automatically deletes expired items (sessions, temporary data) for free
- DynamoDB Streams: ordered log of item changes. Trigger Lambda functions
- Point-in-time recovery (PITR) restores to any second in the last 35 days, plus on-demand backups
- Global tables: multi-Region, multi-active replication
- DAX: in-memory cache that gives microsecond reads
- Transactions: all-or-nothing writes across items
- Encryption at rest is always on (KMS)
- Access control with IAM, including fine-grained access per item
- PartiQL: SQL-like queries
When to Choose DynamoDB
| Great for | Not ideal for |
| Shopping carts, sessions, user profiles, gaming scores, IoT data, serverless apps | Complex joins, ad hoc reporting, many unknown query shapes |
Practice
- Create the Orders table in on-demand mode and add three items
- Query all orders for one user and then try a scan
- Add a GSI on
Status and query by it
- Turn on TTL with an
expiresAt attribute
- Enable PITR and read how restore works
Conclusion
DynamoDB is a fully managed, serverless NoSQL database. Choose partition and sort keys from your access patterns, use query instead of scan, pick on-demand or provisioned capacity, add indexes for extra patterns, and use TTL, streams, PITR and global tables as needed.
Quick Summary: SQL vs NoSQL; core ideas; table and CRUD; query vs scan; capacity; indexes; features; fit.