Key-Value Stores and DynamoDB

Use key-value data stores like Redis as a primary store and DynamoDB for serverless-scale workloads.

Intermediate · about 5 hours · difficulty 3/5

Learn first: NoSQL Families and When to Use Them, Server-Side Caching with Redis

Key-value databases trade query flexibility for predictable speed: you fetch or write by key, and the system can spread keys across many machines.

Redis beyond caching

You met Redis as a cache. It also works as a primary store for data with simple access patterns, using its data structures directly:

  • Hashes for objects, sorted sets for leaderboards and time-ordered feeds, sets for membership, streams for event logs.
  • Atomic commands like INCR and Lua scripts for counters and rate limiters.

Understand its persistence options (RDB snapshots, AOF logs) before trusting it with data you cannot lose.

DynamoDB

Amazon's managed key-value and document database scales to huge throughput with no servers to run. Its model is strict:

  • Every item has a partition key (and optionally a sort key). Queries must specify the partition key; scanning the whole table is slow and expensive.
  • Global secondary indexes give you additional access patterns.
  • Single-table design stores several entity types in one table with carefully designed keys like PK = USER#42, SK = ORDER#2024-05-01.
  • Capacity is on-demand or provisioned, and hot partitions throttle.

When they fit

Session stores, shopping carts, feature flags, user profiles fetched by id, IoT device state and serverless apps where you want zero database administration.

Pitfalls

  • Choosing DynamoDB before knowing your queries, then needing ad hoc filtering it cannot do efficiently.
  • Low-cardinality partition keys (like status) that create hot partitions.

You are ready to move on when you can design a DynamoDB key schema for users and their orders that answers "orders for user X, newest first" without a scan.

Key concepts

  • Redis data structures as primary data
  • Redis persistence: RDB and AOF
  • Partition and sort keys
  • Global secondary indexes
  • Single-table design
  • Hot partitions and capacity modes

Mini project

Build a leaderboard with Redis sorted sets and an order history table in DynamoDB Local with a key design that supports two different query patterns.

Check yourself

Why does DynamoDB punish you for not knowing your access patterns up front?

Free resources