INFRASTRUCTURE LEARNINGINFRASTRUCTURE LEARNING
インフラ大全インフラ大全IT INFRASTRUCTURE / LEARNING
Infrastructure learning guide
Find a lesson or lab/

Esc to close · Search terms stay on your device

LEARNING日本語

Topic lessonTopic: AWS databases: design, comparison and connectivity

DynamoDB fundamentals: keys, Query and read consistency

DynamoDB is a managed NoSQL database designed around keys and the access patterns an application needs. Follow a customer-order example to distinguish primary keys, Query, filters, indexes and freshness. This is not a direct conversion of a SQL Server table.

What you will learn

Choose keys from required reads and explain the difference between Query candidates, returned items, indexes and consistency.

Prerequisite

For readers familiar with tables, rows and primary keys. Work through four fictional orders without an AWS account or live data.

Start with tables, items and keys

An item is one record and an attribute is a value within it. This example uses a partition key and sort key together to identify an item. Flexible non-key attributes do not remove the need to design business data formats and consistent updates.

The two keys in this example
TermExampleRole
Partition key (PK)CUSTOMER#101Select orders belonging to the same customer
Sort key (SK)20261001#41Identify and order items within the PK, including date ranges
Other attributesstatus=PAID, points=1200Data to return; these alone do not define a Query on this base table

Decide how the application reads before storing

Map order-screen requirements to access methods
Required behaviorUsing this key design
List customer 101 orders for OctoberQuery PK=CUSTOMER#101 with a date range on SK
Fetch order 41 directlyGetItem with the complete PK and SK
List PAID orders across all customers by dateCustomer-based PKs do not directly support this; evaluate an index or another design
Read your order immediately after confirmationCheck the destination and consistency option against the freshness requirement

Do not assume SQL joins or arbitrary predicates transfer as equivalent operations. If a design duplicates data for reads, it must also maintain those copies during changes. Conversely, DynamoDB does support transactions. Evaluate relationships, update boundaries and reads together.

Example D1: select by customer and date from four orders

These are fictional orders. Points are teaching values, not AWS charges or performance measurements. Dates in SK use a fixed-width string format so they sort by date within the same PK.

Orders: primary keys and business attributes
OrderPKSKstatuspoints
41CUSTOMER#10120261001#41PAID1200
42CUSTOMER#10120261002#42PENDING800
43CUSTOMER#10120261003#43PAID600
44CUSTOMER#20220261001#44PAID400
Separate key-selected items from filtered results
Separate key-selected items from filtered results1: Customer 101, Oct 1–3 — Key-selected scope — Candidates: 41, 42, 43. 2: Candidates: 41, 42, 43 — Filter after reading — PAID: 41 and 4312Customer 101, Oct 1–3Candidates: 41, 42,43PAID: 41 and 43Separate key-selected items from filtered results1: Customer 101, Oct 1–3 — Key-selected scope — Candidates: 41, 42, 43. 2: Candidates: 41, 42, 43 — Filter after reading — PAID: 41 and 4312Customer 101,Oct 1–3Candidates: 41,42, 43PAID: 41 and 43
  1. Customer 101, Oct 1–3 → Candidates: 41, 42, 43Key-selected scope
  2. Candidates: 41, 42, 43 → PAID: 41 and 43Filter after reading
Customer 101, Oct 1–3
Supply PK and SK conditions.
Candidates: 41, 42, 43
44 belongs to another customer.
PAID: 41 and 43
A status filter removes 42.

A Query on this base table specifies one PK value, optionally narrowed by an SK condition. Orders 41, 42 and 43 match D1; filtering for PAID returns 41 and 43. Their points total 1200 + 600 = 1800. Do not include order 44 from another customer.

Filters and pagination do not replace key conditions

Example D2: evaluate one item per page for Oct 2–3
PageItem evaluated by the key conditionItem returned after PAID filterContinuation
1Order 42, PENDINGNoneA continuation key exists, so keep going
2Order 43, PAIDOrder 43Assume this response still contains a continuation key
3No remaining itemNoneNo continuation key; stop here

D2 deliberately evaluates one item per page and stipulates that page two still has a continuation key. This is neither the AWS page-size limit nor a guarantee of exactly three pages. When a real response contains LastEvaluatedKey, use it to request the continuation. A filtered empty page alone does not prove that no matching items remain. A continuation key also does not guarantee that another matching item exists.

Design alternative access and freshness separately

A global secondary index (GSI) supports access through keys different from the base table. For PAID orders across customers, evaluate an access design based on status and date. Also inspect concentration on a small number of status values, projected attributes, write overhead and storage cost. A GSI is not an automatic accelerator for every query.

Read destinations and consistency in this lesson
DestinationCheck
Base tableChoose default eventual consistency or supported strongly consistent reads according to requirements
Local secondary index (LSI)Supports strong consistency; do not conflate it with a GSI
GSIEventually consistent; an immediate read need not reflect a just-completed base-table update

Extend the small example into operational decisions

  1. List reads, writes and freshness needs for each screen or process.
  2. Map PK/SK and indexes to those access patterns.
  3. Plan consistent updates for duplicated attributes or multiple items.
  4. Estimate skew, item sizes, capacity mode, storage/index/network costs.
  5. Define IAM and data-level access, recovery and business-result checks.

This example teaches key and result semantics, not measured performance or cost. Before replacing SQL Server, compare existing SQL, data relationships and redesign costs with the RDS comparison lesson.

Check the keys and Query results

Choose an option to see the result and explanation.

In D1, which customer 101 PAID orders are returned, and what is their points total?
Read the answer and reason

Answer: 41 and 43; 1800. · Filter PENDING order 42 from customer 101 candidates.

Query read three candidates and returned two after filtering. Were only two read?
Read the answer and reason

Answer: No; distinguish returned and evaluated items. · Fewer returned items alone do not establish reduced read charges.

D2 first returns no items but has LastEvaluatedKey. Stop?
Read the answer and reason

Answer: No; request the continuation using that key. · PAID order 43 remains on the next page.

Need a guaranteed current order through a GSI immediately after confirmation. Set ConsistentRead=true?
Read the answer and reason

Answer: No; a GSI is eventually consistent, so reconsider the read path. · Match authoritative keys and write-result handling to the requirement.

Check, then explain in your own words

  1. Distinguish PK/SK from non-key attributes.
  2. Explain D1 three candidates versus two returned items.
  3. Explain why an empty page can require continuation.
  4. Explain GSI access and eventual consistency.
Check your understanding: Need PAID orders across all customers. Is scanning the base table and filtering a complete design?

No. Inspect volume, frequency, ordering and freshness, then evaluate indexes or another access design. Include concentrated status keys, index propagation and storage/update cost. Being able to scan does not establish that the design meets requirements.

A readable access path must still be assessed against frequency, scale, freshness and update requirements.

Sources and conditions checked

The official references used in this lesson are collected here with their scope, conditions and corresponding sections. Independent expert review and live AWS comparison have not been performed.