AWS Learning Journey

DynamoDB Is Fast — If You Design It Right: NoSQL and Caching on AWS

Partition keys, sort keys, secondary indexes, capacity modes, streams, TTL, global tables, and transactions — plus ElastiCache, Valkey, and MemoryDB. Design for your access patterns, not your entities.

8 min read · 1510 words
AWSDynamoDBNoSQLRedisDatabases
DynamoDB Is Fast — If You Design It Right: NoSQL and Caching on AWS cover image

AWS from Zero to Production — Phase 5, Part 4 of 5 · See the full series roadmap


Here’s how most people’s first DynamoDB table goes:

They design it like a relational database — one table per entity, an id key, and plan to “query it later.” Then they need “all orders for a customer, newest first,” and discover DynamoDB can’t do that without a full table scan. The scan is slow, expensive, and gets worse every day.

DynamoDB is incredibly fast and scalable. Single-digit millisecond reads at almost any scale, no servers, no connection pools. But it rewards a completely different way of designing data: start from your access patterns, then design keys to serve them.

This article teaches that mindset — and covers caching with ElastiCache and MemoryDB, for when even DynamoDB isn’t fast enough.

What you’ll walk away with


What makes DynamoDB different

Amazon DynamoDB is a fully managed, serverless key-value and document database.

What it doesn’t do: joins, arbitrary ad hoc queries, or efficient queries on attributes that aren’t keys or indexed.


Keys: the most important design decision

Partition key

Every table has a partition key. DynamoDB hashes it to decide which internal partition stores the item.

Sort key

Add a sort key, and items with the same partition key are stored together, sorted. Now you can Query ranges:

Partition key: customerId    Sort key: sk  (format: orderDate#orderId)

customerId = C42
  2026-10-01#A17
  2026-10-14#B03
  2026-10-31#C88      <- Query: begins_with "2026-10", newest first

“All orders for customer C42 in October, newest first” is now one efficient Query:

import boto3
from boto3.dynamodb.conditions import Key

table = boto3.resource("dynamodb").Table("orders")
resp = table.query(
    KeyConditionExpression=Key("customerId").eq("C42")
    & Key("sk").begins_with("2026-10"),
    ScanIndexForward=False,
)

Query vs. Scan

Rule: If a core feature needs a Scan, your key design is missing an access pattern.

Design from access patterns

Write down every question the app will ask before creating the table:

1. Get a customer's profile               -> PK = CUSTOMER#C42, SK = PROFILE
2. List a customer's orders, newest first -> PK = CUSTOMER#C42, SK begins_with ORDER#
3. Get one order by ID                    -> GSI: PK = ORDER#A17

Advanced teams often use single-table design, storing several entity types in one table with generic key names (PK, SK). It’s powerful but harder to learn. Separate tables per service are fine to start.


Secondary indexes

Need another way to query? Add an index.

Global secondary index (GSI)

Example: the table is keyed by customerId; a GSI keyed by status and createdAt finds “all pending orders, oldest first.”

Local secondary index (LSI)

Default choice: Use GSIs. LSIs are only for specific cases, and they can’t be added later.


Capacity modes

On-demand

Provisioned

You can switch modes, with some limits on how often.


DynamoDB Streams

DynamoDB Streams captures every change — insert, update, delete — in order, per item, for 24 hours.

Use it to trigger actions on change:

orders table
  -> DynamoDB Stream
  -> Lambda (event source mapping)
  -> send confirmation email / update search index / publish event

You can choose what each record contains: keys only, the new image, the old image, or both.


Time to Live (TTL)

TTL deletes items automatically after a timestamp you store in an attribute:

{ "PK": "SESSION#9f2c", "userId": "C42", "expiresAt": 1767225600 }

Perfect for sessions, temporary tokens, and data with a retention limit.


Global tables

Global tables replicate a table across multiple Regions, with multi-active writes:


Transactions

DynamoDB supports ACID transactions across multiple items and tables:

Transfer stock:
  Update warehouse A: quantity -= 5   (condition: quantity >= 5)
  Update warehouse B: quantity += 5
  -> both happen, or neither

Transactions cost more per item than standard writes. Use conditional writes for simpler cases, like “create only if it doesn’t exist.”


Caching with Amazon ElastiCache

Sometimes you need microsecond reads, or need to protect a database from repeated identical queries. That’s a cache.

Amazon ElastiCache runs in-memory data stores:

ElastiCache comes in serverless form (no capacity planning) or node-based clusters.

Redis and Valkey concepts

Typical uses: session stores, leaderboards with sorted sets, rate limiting, and caching expensive queries.

Caching patterns

Cache-aside (lazy loading) — the most common:

1. App checks the cache
2. Hit  -> return it
3. Miss -> read from the database, write to the cache with a TTL, return it

Write-through: write to the cache whenever you write to the database. The cache is always fresh, at the cost of extra writes.

The hard part is invalidation. Always set a TTL, so stale data eventually expires — even if your invalidation logic has a bug.


Amazon MemoryDB

ElastiCache is a cache: if nodes fail, you may lose recent data, so the real data lives in a database.

Amazon MemoryDB is a durable, Valkey- and Redis OSS-compatible primary database:


Choosing a database

Many systems use several — each for what it’s best at.


Common mistakes


Key takeaways


Quick self-check

Test yourself before moving on:

  1. Why is status a poor partition key?
  2. Can you add a local secondary index to an existing table?
  3. Which capacity mode suits a new app with unpredictable traffic?
  4. In cache-aside, what happens on a cache miss?

Answers

  1. It has few distinct values, creating hot partitions.
  2. No. LSIs must be created with the table. Use a GSI.
  3. On-demand.
  4. The app reads from the database, writes the result to the cache with a TTL, and returns it.

What’s next

In Part 5, the Phase 5 lab: give the orders API a private PostgreSQL database on RDS, with credentials in Secrets Manager, automated backups, and an app that survives password rotation.

Single-table design: genius or overkill? I want to hear your take in the comments.


Share this article

LinkedIn post draft