Part 3 of the Serverless series.

Amazon DynamoDB is a fully managed key-value and document database with consistent single-digit-millisecond performance at any scale. There are no servers or connections to manage, which makes it the natural partner for Lambda.

The catch: DynamoDB rewards you for knowing how you’ll read your data before you store it. If you design it like a relational database, it fights you.

The mental model

A table holds items (up to 400 KB each), and each item is a set of attributes. Every item is identified by its primary key:

  • Partition key only, for example userId: a simple key-value lookup.
  • Partition key + sort key, for example userId + orderDate: many related items share a partition, stored in sort-key order.

DynamoDB hashes the partition key to decide where data lives, and spreads partitions across storage. That’s how it scales, and why a good partition key has many distinct values that are accessed evenly.

Query, don’t Scan

  • GetItem fetches one item by its full key.
  • Query fetches items in one partition, optionally filtered by sort key: “orders for user 42 in 2026”, “messages in game ABC after 12:00”. Fast and cheap.
  • Scan reads the entire table. Slow and expensive at scale; fine for small admin jobs, wrong for a request path.

If you find yourself needing a Scan to answer a common question, the key design is missing an access pattern.

Secondary indexes

A global secondary index (GSI) gives a table another partition key and sort key, so you can query by a different attribute: for example, “orders by status”. DynamoDB keeps the index updated for you (eventually consistent). Add a GSI per additional access pattern, rather than scanning.

Single-table design

Because DynamoDB has no joins, related data is often stored together, in one table, under generic key names:

PK SK Data
GAME#ABC META status, host, topic
GAME#ABC PLAYER#ana nickname, score
GAME#ABC PLAYER#ben nickname, score
GAME#ABC QUESTION#01 text, answers

One Query on PK = GAME#ABC returns the game, its players and its questions in a single request. Trivolt stores quizzes, games and history in a single table this way.

Single-table design isn’t mandatory. Separate tables are simpler to reason about while you’re learning; the key skill is designing keys around access patterns either way.

One table per service, not one table for everything

“Single table” means a single table per service, not one table shared by the whole system. In a microservice architecture, each service owns its data, and other services get at it only through that service’s API or events. Sharing a table quietly couples services together: one team’s key change breaks another’s queries, and neither can deploy independently.

Owning your data also means choosing the right database for the job. The Better Store uses DynamoDB for its Order service, where access patterns are known and latency matters, and Aurora Serverless v2 for Reports, where ad-hoc SQL queries over many orders are the point. Each service picks what fits its own access patterns.

Capacity modes

  • On-demand: pay per request, no capacity planning, scales instantly. The right default for new and spiky workloads.
  • Provisioned: set read and write capacity (with auto scaling). Cheaper for steady, predictable traffic.

Features you’ll use

  • Conditional writes (ConditionExpression) prevent lost updates: “only set the answer if the question is still open”.
  • Transactions update up to 100 items atomically.
  • TTL deletes items automatically after a timestamp; perfect for sessions and temporary game data.
  • DynamoDB Streams emit every change as an event, which Lambda or EventBridge Pipes can react to. This is the foundation of the outbox pattern.
  • Point-in-time recovery restores a table to any second in the recovery window. Turn it on for anything you care about.

Hands-on

GamesTable:
  Type: AWS::DynamoDB::Table
  Properties:
    BillingMode: PAY_PER_REQUEST
    AttributeDefinitions:
      - { AttributeName: PK, AttributeType: S }
      - { AttributeName: SK, AttributeType: S }
    KeySchema:
      - { AttributeName: PK, KeyType: HASH }
      - { AttributeName: SK, KeyType: RANGE }
    TimeToLiveSpecification: { AttributeName: expiresAt, Enabled: true }
    PointInTimeRecoverySpecification: { PointInTimeRecoveryEnabled: true }

Write a few items with aws dynamodb put-item, then compare a query on one partition with a scan of the table, and look at the consumed capacity each reports with --return-consumed-capacity TOTAL.

Gotchas

  • Write down access patterns first. List every question the app asks of its data, then design keys to answer each with a GetItem or Query.
  • Hot partitions. A key everyone writes to (a single “global counter” item, say) limits throughput. Spread writes across keys.
  • Filter expressions don’t save money. They’re applied after items are read, so you pay for everything read.

Next: EventBridge: the event router.

← Series overview