cs.oldnet

All sheets / Data

DynamoDB

Key design, access patterns and capacity for DynamoDB.

Core concepts

  • Partition key (PK) → hashed to a partition. Sort key (SK) orders items within a PK.
  • Item ≤ 400 KB. Partition limits: ~3000 RCU / 1000 WCU per partition.
  • Query = one PK (+ SK condition), efficient. Scan = whole table, avoid.
  • Design from access patterns first, not entities.

Capacity

ModeUse
On-demandunpredictable/spiky, pay per request
Provisioned + auto scalingsteady, cheaper at scale
  • 1 RCU = 1 strongly consistent read/s of 4 KB (or 2 eventually consistent).
  • 1 WCU = 1 write/s of 1 KB. Transactions cost 2x.
  • Hot partitions: skewed PKs throttle. Fix with high-cardinality keys or write sharding (PK#rand(0..9)).

Indexes

GSILSI
Keysany PK + SKsame PK, different SK
Whenanytimetable creation only
Consistencyeventual onlystrong or eventual
Limit20 per table5; 10 GB per PK

Sparse index: only items with the GSI attribute appear in it, great for "status = pending" lists.

Single-table design

PKSKData
USER#123PROFILEname, email
USER#123ORDER#2024-05-01#987total, status
ORDER#987ITEM#1sku, qty
  • Query PK = USER#123 AND begins_with(SK, 'ORDER#') → user's orders, sorted by date.
  • Overload GSIs (GSI1PK, GSI1SK) for inverted/alternate patterns.

SDK v3 (JS)

import { DynamoDBDocumentClient, QueryCommand, PutCommand, UpdateCommand } from '@aws-sdk/lib-dynamodb';
const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({}));

await ddb.send(new QueryCommand({
  TableName: 'app',
  KeyConditionExpression: 'PK = :pk AND begins_with(SK, :sk)',
  ExpressionAttributeValues: { ':pk': 'USER#123', ':sk': 'ORDER#' },
  ScanIndexForward: false, Limit: 20,
}));

// idempotent create
await ddb.send(new PutCommand({ TableName: 'app', Item: item,
  ConditionExpression: 'attribute_not_exists(PK)' }));

// atomic counter + optimistic locking
await ddb.send(new UpdateCommand({ TableName: 'app', Key: { PK, SK },
  UpdateExpression: 'SET #c = #c + :one, version = :v2',
  ConditionExpression: 'version = :v1',
  ExpressionAttributeNames: { '#c': 'count' },
  ExpressionAttributeValues: { ':one': 1, ':v1': 3, ':v2': 4 } }));

Features

  • Streams → Lambda for CDC, event publishing, aggregations (24 h retention).
  • TTL attribute auto-deletes expired items (free, within ~48 h).
  • Transactions: TransactWriteItems up to 100 items, all-or-nothing.
  • PITR backups, global tables (multi-region active-active), DAX cache (microsecond reads).
  • Pagination via LastEvaluatedKey → ExclusiveStartKey; 1 MB per page.

Performance tips

  • Use ProjectionExpression to fetch fewer attributes.
  • BatchGetItem (100) / BatchWriteItem (25); retry UnprocessedItems.
  • Avoid large items; store blobs in S3 with a pointer.
  • Monitor ThrottledRequests, ConsumedReadCapacityUnits, hot keys via Contributor Insights.

DynamoDB vs relational

Pick DynamoDB for known access patterns, massive scale, predictable single-digit ms latency. Pick Postgres for ad-hoc queries, joins, complex transactions and evolving access patterns.