Skip to main content
PreparationZone Logo
PrepZone

DynamoDB Access Patterns

Design tables around queries, capacity units, and why scans are the last resort.

Pass checkpoint below

Read these first

Why this matters

  • VaultCommerce order history needs "all orders for user X, newest first" — composite key PK=USER#X, SK=ORDER#timestamp satisfies that in one Query.
  • Table scans read every partition and burn RCUs — acceptable only for small admin exports, never for customer-facing APIs.
  • Hot partition keys (everyone writing PK=FLASH_SALE) throttle the whole shard regardless of table size.
Partition key: USER#42Routes to shard
Items in partition
ORDER#2026-001Sort key
ORDER#2026-002Sort key
PROFILESort key
Partition key routes to a shard. Sort key orders items within that partition.

Design queries first

VaultCommerce access patterns

  • Get order by user + order id — GetItem on PK=USER#42, SK=ORDER#2026-0042.
  • List recent orders for user — Query PK=USER#42, SK begins_with ORDER#, ScanIndexForward=false.
  • Record idempotency token — PutItem with condition attribute_not_exists(PK).
  • Admin export (rare) — Scan with filter (last resort, off-peak only).
Java
import boto3
from boto3.dynamodb.conditions import Key

table = boto3.resource("dynamodb").Table("VaultCommerceOrders")

def recent_orders(user_id: int, limit: int = 20):
    return table.query(
        KeyConditionExpression=Key("PK").eq(f"USER#{user_id}")
        & Key("SK").begins_with("ORDER#"),
        ScanIndexForward=False,
        Limit=limit,
    )["Items"]

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 2 sections

  • Query vs Scan
  • Capacity units

Test yourself

Answer these before moving on — recall is what makes it stick.