Amazon DynamoDB

DynamoDB is a fully managed NoSQL key-value database that delivers single-digit-millisecond performance at any scale.

Amazon DynamoDB is a serverless NoSQL database. There are no servers to manage and it scales seamlessly to handle huge request volumes with consistent low latency.

Data model

  • Data lives in tables made of items (rows) and attributes (fields).
  • Every table has a primary key: either a partition key alone, or a partition key + sort key.
  • The schema is flexible — items in one table can have different attributes.

Capacity modes

  • On-demand — pay per request, scales automatically. Great for spiky or unpredictable traffic.
  • Provisioned — you set read/write capacity units; cheaper for steady, predictable load.

RDS vs DynamoDB

Choose RDS when you need joins, transactions and SQL. Choose DynamoDB for massive scale, simple access patterns and a flexible schema.

Example

Example · bash
# Create an on-demand table keyed by userId
aws dynamodb create-table \
  --table-name Users \
  --attribute-definitions AttributeName=userId,AttributeType=S \
  --key-schema AttributeName=userId,KeyType=HASH \
  --billing-mode PAY_PER_REQUEST

# Put an item
aws dynamodb put-item --table-name Users \
  --item '{"userId":{"S":"u-1001"},"name":{"S":"Alice"},"plan":{"S":"pro"}}'

When to use it

  • A gaming leaderboard uses DynamoDB to store and retrieve millions of player scores per second with consistent single-digit millisecond latency.
  • An IoT platform writes sensor readings from 100,000 devices to a DynamoDB table using the device ID as the partition key for even data distribution.
  • A shopping cart service uses DynamoDB with TTL attributes to automatically delete abandoned cart records after 24 hours without a cleanup job.

More examples

Create a DynamoDB Table

Creates a DynamoDB table with a composite key (userId + sessionId) using on-demand billing for unpredictable traffic.

Example · bash
aws dynamodb create-table \
  --table-name UserSessions \
  --attribute-definitions \
    AttributeName=userId,AttributeType=S \
    AttributeName=sessionId,AttributeType=S \
  --key-schema \
    AttributeName=userId,KeyType=HASH \
    AttributeName=sessionId,KeyType=RANGE \
  --billing-mode PAY_PER_REQUEST

Put and Get an Item

Writes a single item to DynamoDB and retrieves it using its full primary key, demonstrating the basic read/write pattern.

Example · bash
# Write an item
aws dynamodb put-item \
  --table-name UserSessions \
  --item '{"userId":{"S":"u123"},"sessionId":{"S":"s456"},"createdAt":{"N":"1704067200"}}'

# Read it back
aws dynamodb get-item \
  --table-name UserSessions \
  --key '{"userId":{"S":"u123"},"sessionId":{"S":"s456"}}'

Enable TTL for Auto-Expiry

Enables TTL on the table so DynamoDB automatically deletes items when the Unix timestamp in expiresAt is passed.

Example · bash
aws dynamodb update-time-to-live \
  --table-name UserSessions \
  --time-to-live-specification \
    Enabled=true,AttributeName=expiresAt

Discussion

  • Be the first to comment on this lesson.