Skip to content

Beginner · Networking and databases: VPC and RDS · topic 17 of 50 · 30 min

Choosing a database: DynamoDB, caches and warehouses

Explanation

Not every app needs a relational database. Key-value stores like DynamoDB give fast, predictable lookups at any scale when you know your access patterns; in-memory caches like ElastiCache make repeated reads very fast; warehouses like Redshift run big analytical queries. Pick the database by how the data is read and written, not by habit.

Why more than one kind of database?

A relational database is a great default: tables, joins, transactions and SQL. But some jobs fit other shapes better. A shopping cart read millions of times a minute, a leaderboard that changes every second, or a report over three years of sales each strain a single relational database in different ways. Cloud providers offer purpose-built databases for each shape.

TypeAWSGood for
RelationalRDS, AuroraOrders, payments, anything with joins and transactions
Key-valueDynamoDBSessions, carts, user profiles, IoT data at huge scale
DocumentDocumentDB (MongoDB compatible), DynamoDBJSON-shaped records with flexible fields
In-memoryElastiCache (Valkey, Redis OSS, Memcached)Caching, leaderboards, rate limits
WarehouseRedshiftAnalytics over large historical data with SQL

DynamoDB: the key ideas

Amazon DynamoDB is a fully managed, serverless key-value and document database. There are no servers or versions to manage, and reads and writes take single-digit milliseconds at almost any scale. In exchange, you design the table around how you will query it.

  • Item: one record, like a row, holding attributes. An item can be up to 400 KB.
  • Partition key: the attribute DynamoDB hashes to decide where an item is stored. Every query must give one exact partition key value.
  • Sort key (optional): orders items that share a partition key, and lets you ask for ranges, such as orders between two dates or keys that begin with "2026-03".
  • Primary key: the partition key, or partition key plus sort key. It must be unique; writing the same key again replaces the item.
  • Query reads one partition key efficiently. Scan reads the whole table and is slow and costly on big tables. Add a Global Secondary Index (GSI) when you need another access pattern.
customer_id (partition key)order_date (sort key)totalstatus
C-10012026-03-02#O-5511299DELIVERED
C-10012026-03-18#O-602450SHIPPED
C-20402026-03-05#O-5608999PLACED

With this design, "all orders for C-1001 in March 2026, newest first" is a single fast Query. Putting the order ID after the date in the sort key keeps keys unique when a customer orders twice on the same day.

Capacity modes and consistency

On-demand mode charges per request and scales by itself, which suits new or spiky apps. Provisioned mode lets you set read and write capacity units (optionally with auto scaling) and is cheaper for steady, predictable traffic. One write capacity unit (WCU) is one write per second of up to 1 KB. One read capacity unit (RCU) is one strongly consistent read per second of up to 4 KB, or two eventually consistent reads. Eventually consistent reads (the default) may return data a moment old; strongly consistent reads always return the latest write.

Global tables replicate a table to several regions, and every region can accept writes. DynamoDB also offers TTL (automatic deletion of expired items), Streams (a change feed for triggers) and point-in-time recovery.

Caches and warehouses

Amazon ElastiCache runs Valkey, Redis OSS or Memcached in memory. The common pattern is cache-aside: the app checks the cache first; on a miss it reads the database and stores the result with a TTL (time to live). This cuts database load and response times for data that many users read, like a product page. Amazon Redshift is a columnar data warehouse: it stores each column together so queries like "total sales by state for three years" scan only the columns they need.

Equivalents on other clouds

  • Azure: Azure SQL Database and Azure Database for PostgreSQL/MySQL (relational), Cosmos DB (key-value and document), Azure Managed Redis (cache), Azure Synapse Analytics and Microsoft Fabric (warehouse).
  • Google Cloud: Cloud SQL, AlloyDB and Spanner (relational), Firestore and Bigtable (NoSQL), Memorystore (cache), BigQuery (warehouse).
Careful: Common mistakes: (1) Designing DynamoDB tables like SQL tables and then using Scan for every query; list your access patterns first. (2) A partition key with few values, such as status, so all traffic hits a few partitions (a "hot partition"); use high-cardinality keys like customer_id. (3) Caching without a TTL, so users see stale prices forever. (4) Running heavy analytics on the production OLTP database instead of a warehouse or a replica. (5) Choosing NoSQL "for scale" when the app needs joins and transactions and has modest traffic.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Choosing a database: DynamoDB, caches and warehouses · 1/7

Relational Key-value Document In-memory Warehouse

Pick a database by how data is read and written. Relational is a good default, but not the only choice.

Prefer a video by a teacher? Find free videos on “dynamodb partition key sort key tutorial elasticache redshift when to use” on YouTube ↗

In real life: A food delivery app during IPL finals

A food delivery app stores live order status in DynamoDB with order_id as the partition key, because the app and the rider both poll it every few seconds and traffic jumps tenfold during an IPL final. Restaurant menus are cached in ElastiCache for Valkey with a 5 minute TTL. Payments and refunds live in Aurora PostgreSQL, where transactions matter. Every night order data is copied to Redshift so the business team can ask which dishes sold most in each city during the match.

bash
aws dynamodb query \
  --table-name Orders \
  --key-condition-expression "customer_id = :c AND begins_with(order_date, :m)" \
  --expression-attribute-values '{":c": {"S": "C-1001"}, ":m": {"S": "2026-03"}}' \
  --no-scan-index-forward
All March 2026 orders of one customer, newest first (--no-scan-index-forward reverses the sort order).

Practice (0/3 done)

Task 1 · Question

You need the 10 most recent orders of one customer from a DynamoDB table. Which key design makes that one Query?

Task 2 · Code · Python

Simulate a DynamoDB table with a partition key and a sort key. The table is a dict: partition key value -> dict of sort key value -> item. Write put_item(table, pk, sk, item) that stores the item (replacing any item with the same pk and sk), and query(table, pk, begins_with="", newest_first=False) that returns the items for that pk whose sort key starts with begins_with, sorted by sort key (descending if newest_first). An unknown pk returns [].

Task 3 · Code · Python

Write capacity_units(per_second, item_kb, kind) for DynamoDB provisioned mode. kind is "write", "strong" (strongly consistent read) or "eventual" (eventually consistent read). A write uses 1 WCU per 1 KB (rounded up). A strong read uses 1 RCU per 4 KB (rounded up). An eventual read costs half of a strong read; round the TOTAL up. Return the whole number of units needed.

Quick quiz

Question 1

1.What is the maximum size of a single DynamoDB item?

Question 2

2.Which DynamoDB capacity mode suits a brand-new app with unpredictable, spiky traffic?

Question 3

3.A product page is read thousands of times per minute and changes rarely. What reduces database load most?

Question 4

4.The business team wants sales totals by state across five years of data. Which service is designed for this?

Question 5

5.Which Google Cloud service is the closest match to Amazon Redshift?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What is the maximum size of a single DynamoDB item?

Answer: 400 KB

A DynamoDB item, including attribute names and values, can be up to 400 KB.

Which DynamoDB capacity mode suits a brand-new app with unpredictable, spiky traffic?

Answer: On-demand

On-demand scales automatically and bills per request, so you do not have to guess capacity.

A product page is read thousands of times per minute and changes rarely. What reduces database load most?

Answer: A cache in ElastiCache with a TTL

A cache serves repeated reads from memory; the TTL keeps the data reasonably fresh.

The business team wants sales totals by state across five years of data. Which service is designed for this?

Answer: Amazon Redshift

Redshift is a columnar data warehouse for large analytical SQL queries.

Which Google Cloud service is the closest match to Amazon Redshift?

Answer: BigQuery

BigQuery is Google Cloud's serverless data warehouse.

Which service runs your container, scales with traffic and can scale down to zero?Related

Answer: Cloud Run

Cloud Run is serverless containers with scale to zero.

From Core Google Cloud services

A product's price changed in the database. What should you do with its cache key?Related

Answer: Delete it, so the next read loads the new price

Deleting (invalidating) the key after the update makes sure the next read gets fresh data.

From Caching and Redis

Why do warehouses often store data in columns rather than rows?Related

Answer: A query summing one column reads only that column from disk

Analytics touches a few columns of many rows; columnar storage reads just those columns.

From Denormalisation, OLTP and OLAP

What gives a Lambda function permission to read from a DynamoDB table?Related

Answer: Its execution role

The execution role is the IAM role Lambda assumes when running your code. Its policy must allow the DynamoDB read.

From Serverless with AWS Lambda

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 1 more topic in “Networking and databases: VPC and RDS” before its exam: DNS and CDN: Route 53 and CloudFront.
  2. Module 4 exam: Networking and databases, VPC and RDS · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Serverless, monitoring and cost control. 50 topics left in the course before the final exam.

← Previous: Managed relational databases: RDS and Aurora