DynamoDB
DynamoDB is a fully managed, serverless NoSQL database with consistent single-digit-millisecond performance at any scale.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 29, 2026
Related tutorials
Learning objectives#
- Design a table with partition and sort keys
- Write and read items
- Query by key
- Choose on-demand or provisioned capacity
Explanation#
Items are identified by a primary key: a partition key alone, or a partition key + sort key. The partition key spreads data across storage partitions.
Design tables around access patterns. Query reads items with the same partition key (optionally filtered by sort key); Scan reads the whole table and should be avoided.
Global secondary indexes (GSIs) support extra access patterns.
On-demand capacity bills per request and scales automatically; provisioned capacity is cheaper for steady traffic.
Code example: Orders by customer#
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, PutCommand, QueryCommand } from '@aws-sdk/lib-dynamodb';
const db = DynamoDBDocumentClient.from(new DynamoDBClient({ region: 'ap-south-1' }));
// Table "Orders": partition key customerId (S), sort key orderDate (S)
await db.send(new PutCommand({
TableName: 'Orders',
Item: { customerId: 'c-1', orderDate: '2026-10-29', orderId: 'o-101', total: 56400 },
}));
const { Items } = await db.send(new QueryCommand({
TableName: 'Orders',
KeyConditionExpression: 'customerId = :c AND begins_with(orderDate, :m)',
ExpressionAttributeValues: { ':c': 'c-1', ':m': '2026-10' },
}));
console.log(Items);
[ { customerId: 'c-1', orderDate: '2026-10-29', orderId: 'o-101', total: 56400 } ]This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- The document client converts plain JavaScript objects to DynamoDB types.
- PutCommand writes an item with the full primary key.
- The query gets one customer’s orders for October 2026 using the sort key prefix.
- No Scan is needed because the design matches the access pattern.
Real-world use case#
Shopping carts, sessions, leaderboards and order histories — workloads with known key-based access patterns and spiky traffic.
Common mistakes#
Start from queries, not entities. Joins do not exist.
Scan reads every item and gets slower and more expensive as data grows.
Best practices#
- List access patterns first
- Choose high-cardinality partition keys
- Use on-demand capacity until traffic is predictable
- Enable point-in-time recovery
Remember this#
Partition key = where. Sort key = order within.
Query good, Scan bad.
Design for access patterns, not tables.
Practice exercise#
Your task: Create the Orders table with on-demand capacity using the CLI.
Show solution
aws dynamodb create-table --table-name Orders \
--attribute-definitions AttributeName=customerId,AttributeType=S AttributeName=orderDate,AttributeType=S \
--key-schema AttributeName=customerId,KeyType=HASH AttributeName=orderDate,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST
Mini quiz#
Which operation reads every item in a table?
Want more? Practise all AWS MCQs · AWS interview questions
My notes#
Saved in this browser.