AWS · Compute & Storage

DynamoDB

DynamoDB is a fully managed, serverless NoSQL database with consistent single-digit-millisecond performance at any scale.

Beginner 5 min Lesson 7 of 30
Before you start:IAM
0/30 in AWS
On this page · 5 min · Beginner

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#

JavaScriptorders.js Illustrative
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);
Expected output
[ { 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#

  1. The document client converts plain JavaScript objects to DynamoDB types.
  2. PutCommand writes an item with the full primary key.
  3. The query gets one customer’s orders for October 2026 using the sort key prefix.
  4. 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#

Designing tables like SQL

Start from queries, not entities. Joins do not exist.

Using Scan in production paths

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
Shell Illustrative
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#

DynamoDB quizMultiple choice Question 1 of 1

Which operation reads every item in a table?

Score so far: 0/0

Want more? Practise all AWS MCQs · AWS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.