DynamoDB — NoSQL at Scale
Single-digit millisecond reads at any scale — tables, partition keys, GSIs, and when NoSQL is the right choice
Skills taught: aws.dynamodb, aws.partition-key, aws.sort-key, aws.gsi, aws.dynamodb-streams, aws.single-table-design
What DynamoDB is — and what it isn't
DynamoDB is AWS's managed NoSQL database. It stores data as items (like rows) with attributes (like columns), but without a fixed schema. Its killer feature: it scales to millions of requests per second with single-digit millisecond latency — at any scale. Amazon uses DynamoDB for its shopping cart, which must handle billions of items. The tradeoff: you lose the flexibility of SQL. Queries must go through predefined access patterns. DynamoDB forces you to think about how you'll read your data before you write it.
Core concepts
TABLE:
- Like a database table, but schema-less
- Only the partition key (and optional sort key) are required on all items
- Different items in the same table can have different attributes
ITEM:
- Like a row — a collection of attributes
- Max 400KB per item
PARTITION KEY (required):
- Determines which physical partition stores the item
- Must be unique per item (if no sort key)
- Choose high-cardinality values (user IDs, not status codes)
- Hot partitions = all reads/writes to one partition key = slow
SORT KEY (optional):
- Combined with partition key to form a unique composite key
- Items with same partition key are sorted by sort key
- Enables range queries: get all orders for user X between date A and B
Example — users + orders in one table (single-table design):
PK (partition) SK (sort) Other attributes
USER#u001 PROFILE name=Alice, email=...
USER#u001 ORDER#2026-01-15 total=99.99, status=shipped
USER#u001 ORDER#2026-02-20 total=45.00, status=pending
USER#u002 PROFILE name=Bob, email=...
PRODUCT#p001 DETAILS name=Widget, price=29.99
Basic operations with the SDK
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, PutCommand, GetCommand, QueryCommand, DeleteCommand } from '@aws-sdk/lib-dynamodb';
const client = new DynamoDBClient({ region: 'ap-south-1' });
const db = DynamoDBDocumentClient.from(client);
// PUT — create or replace an item
await db.send(new PutCommand({
TableName: 'MyAppTable',
Item: {
PK: 'USER#u001',
SK: 'PROFILE',
name: 'Alice',
email: 'alice@example.com',
createdAt: new Date().toISOString(),
},
}));
// GET — retrieve a specific item (must know PK + SK)
const response = await db.send(new GetCommand({
TableName: 'MyAppTable',
Key: { PK: 'USER#u001', SK: 'PROFILE' },
}));
console.log(response.Item?.name); // 'Alice'
// QUERY — get all items with the same partition key
// Get all orders for user u001
const orders = await db.send(new QueryCommand({
TableName: 'MyAppTable',
KeyConditionExpression: 'PK = :pk AND begins_with(SK, :sk)',
ExpressionAttributeValues: {
':pk': 'USER#u001',
':sk': 'ORDER#',
},
}));
console.log(orders.Items); // all order items for u001
// DELETE
await db.send(new DeleteCommand({
TableName: 'MyAppTable',
Key: { PK: 'USER#u001', SK: 'ORDER#2026-01-15' },
}));
Global Secondary Indexes (GSIs)
A table's primary key defines the only way to query by default. A GSI is a copy of your data with a different primary key — enabling different query patterns. If your table uses PK=userId, you can add a GSI with PK=email to look up users by email. GSIs come with extra cost (you pay for storage and capacity on the index) but they enable new access patterns without redesigning the table.
When to use DynamoDB vs RDS
Use DynamoDB when:
✓ Massive scale needed (millions of requests/second)
✓ Latency must be < 10ms at p99
✓ Access patterns are well-known and simple (key-value lookups)
✓ Flexible schema — items may have different shapes
✓ Serverless / event-driven architecture
Examples: shopping carts, user sessions, IoT telemetry, gaming leaderboards
Use RDS (PostgreSQL) when:
✓ Complex queries with JOINs across multiple tables
✓ Ad hoc queries unknown at design time
✓ ACID transactions across multiple tables
✓ Analytics, reporting, aggregations
✓ Scale < 10k requests/second (RDS handles this fine)
Examples: financial records, CMS, e-commerce catalog, user accounts
Hybrid: use both
DynamoDB for high-volume, low-latency operations (sessions, carts)
RDS for complex queries and reporting (orders history, analytics)
Tip: DynamoDB has no free queries — only free capacity. You pay per request (on-demand mode) or per provisioned capacity (provisioned mode). On-demand is $1.25 per million writes and $0.25 per million reads — great for unpredictable workloads. Provisioned is cheaper at scale but requires capacity planning. For a new project, start with on-demand mode to understand your traffic before committing to provisioned.
Frequently Asked Questions
What is single-table design?
In relational databases, you have separate tables for each entity (users, orders, products). In DynamoDB, a common pattern is to store all entities in ONE table, distinguished by their key patterns (USER#id, ORDER#id, PRODUCT#id). This allows you to fetch related data in a single query instead of multiple round trips. It requires careful upfront design but improves performance and reduces cost.