iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

DynamoDB

DynamoDB is AWS’s fully-managed NoSQL key-value + document database. Single-digit-millisecond latency at any scale, predictable cost, and zero ops. Pay per request or per provisioned capacity.

Tables, queries, single-table design

EXAMPLE
# 1) Create a table — partition key + optional sort key
aws dynamodb create-table \
    --table-name users \
    --attribute-definitions AttributeName=pk,AttributeType=S AttributeName=sk,AttributeType=S \
    --key-schema AttributeName=pk,KeyType=HASH AttributeName=sk,KeyType=RANGE \
    --billing-mode PAY_PER_REQUEST

# 2) PutItem / GetItem
aws dynamodb put-item --table-name users --item '{
    "pk":    {"S": "USER#42"},
    "sk":    {"S": "PROFILE"},
    "name":  {"S": "Ada"},
    "email": {"S": "ada@example.com"}
}'

aws dynamodb get-item --table-name users \
    --key '{"pk":{"S":"USER#42"},"sk":{"S":"PROFILE"}}'

# 3) Query — same partition, many sort keys
# Single-table design: store user + their posts under the same pk
# pk = 'USER#42'
# sk = 'PROFILE'                  → profile doc
# sk = 'POST#2026-06-07#abc'      → post sorted by date
aws dynamodb query --table-name users \
    --key-condition-expression 'pk = :p AND begins_with(sk, :s)' \
    --expression-attribute-values '{":p":{"S":"USER#42"},":s":{"S":"POST#"}}'

# 4) Node SDK v3
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, GetCommand, QueryCommand, PutCommand } from '@aws-sdk/lib-dynamodb';

const doc = DynamoDBDocumentClient.from(new DynamoDBClient({ region: 'us-east-1' }));

await doc.send(new PutCommand({
    TableName: 'users',
    Item: { pk: 'USER#42', sk: 'PROFILE', name: 'Ada' },
}));

const { Items } = await doc.send(new QueryCommand({
    TableName: 'users',
    KeyConditionExpression: 'pk = :p AND begins_with(sk, :s)',
    ExpressionAttributeValues: { ':p': 'USER#42', ':s': 'POST#' },
}));

Why it matters

Single-table design is the unlock. One table holds many entity types keyed by pk + sk patterns — lets you fetch a user’s profile + most-recent posts in ONE query, zero joins.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Serverless NoSQL — single-digit ms reads at any scale.
# Design with access patterns first.
Try it Yourself »

Discussion

Loading…