Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 37 ยท Capacity, Consistency and Transactions

Consistency Models

Learn how DynamoDB replicates data across Availability Zones, when eventually consistent reads return stale data, and when to choose strong consistency.

Every DynamoDB write is stored on multiple storage nodes in three Availability Zones of one region before it is acknowledged. That is what makes the service durable and highly available. But it also means that for a short moment after a write, one of the copies can still be behind. When you read, you choose whether you accept a possibly stale answer in return for lower cost, or insist on the latest one.

This lesson explains the three read behaviours (eventually consistent, strongly consistent and transactional), how they differ in cost and availability, and how to decide between them. Cost units were introduced in capacity modes.

Eventually consistent reads (the default)

By default GetItem, Query, Scan and BatchGetItem are eventually consistent: the read can be served by any replica of the data, including one that has not yet applied the latest write. Replicas usually converge in well under a second, but there is no guarantee for any single read.

  • Costs half a read unit per 4 KB.
  • Highest availability and lowest latency.
  • Right for feeds, catalogs, dashboards, anything that can tolerate "a moment ago".

Strongly consistent reads

Setting ConsistentRead=true returns the result of all writes that succeeded before the read started. The request is served by the leader replica for the partition.

  • Costs a full read unit per 4 KB (double an eventual read).
  • Slightly higher latency, and can fail if the leader is not reachable (an eventually consistent read could still succeed from another replica).
  • Available on the base table and on local secondary indexes, but never on global secondary indexes or streams; GSIs are always eventually consistent.
Python
# boto3 (read-only here: DynamoDB is not available on this page)
resp = table.get_item(Key={"PK": "ORDER#o21", "SK": "META"}, ConsistentRead=True)

Transactional reads

TransactGetItems reads several items as one serializable snapshot, costing two units per 4 KB. Use it when you need a mutually consistent view of several items. The next lessons cover transactions in full.

Three read flavors

Eventual Default, 0.5 RCU, may be stale
Strong ConsistentRead, 1 RCU
Transactional 2 RCU, serializable snapshot

Cost doubles at each step.

1 / 2

Try it: replicas that lag

The stand-in below keeps three copies. A write reaches the leader immediately and the followers a moment later (simulated by a tick). A normal read picks a random replica; a strong read uses the leader. It is a teaching model, not DynamoDB itself.

Python Stale reads from lagging replicas

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Some eventual reads returned the old 10 until replication caught up, while every strong read returned 9. This is exactly the behavior you plan for.

Choosing the right level

Need Choice
Show a product list, a profile on a page Eventual
Read-after-write: the user just saved settings and the next screen must show them Strong
Check stock or a balance before deciding something Strong, or better: a conditional write that cannot go wrong
Read two items that must agree Transactional read
A GSI lookup right after a write Cannot be strong; design for delay or read the base table by key

A common mistake is to use a strong read to protect a read-modify-write cycle. Even a strong read followed by a write has a gap in which someone else may write. The right tool for that is a conditional write, which checks and writes atomically on the server.

Read-after-write on indexes

A write to the base table updates its GSIs asynchronously; there is no way to wait. If the user creates an item and then immediately lists a GSI view, the new item can be missing. Options:

  1. Return the created item from the write call and merge it into the UI.
  2. Read from the base table with a key instead of the GSI.
  3. Accept the delay and refresh (typical delay is milliseconds to a second or two).
Python Add the new item locally until the index catches up

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Global tables

With global tables (multi-Region replication) a write in one Region reaches the others typically within a second, asynchronously. Strongly consistent reads are only guaranteed within the Region where the write was made. With the default multi-Region eventual consistency mode, conflicting writes in two Regions resolve by "last writer wins" based on timestamps. See backups and global tables.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a strongly consistent read guarantee?

    Strong reads are served by the leader and see every earlier successful write. They cost twice as much and are not available on GSIs.
  2. 2. How many RCU does a strongly consistent read of a 4 KB item use?

    One RCU is a strongly consistent read of up to 4 KB. Eventual is 0.5 and transactional is 2.
  3. 3. A user saves a new order and immediately queries a GSI. What may happen?

    GSI updates are eventually consistent and cannot be read strongly. Merge the returned item client-side or read the base table.
  4. 4. What is the correct tool to protect a read-modify-write from concurrent changes?

    Between a read and a write another client can write. A condition expression performs the check and the write as one atomic step.

Summary

  • Writes are replicated across three Availability Zones; replicas may briefly lag.
  • Reads are eventually consistent by default (0.5 RCU); ConsistentRead=true gives strong reads (1 RCU); transactional reads cost 2 RCU.
  • GSIs and streams cannot be read strongly.
  • Use strong reads for read-after-write needs, but use conditional writes to guard updates.
  • Global tables are strongly consistent only in the Region of the write.

Next: Conditional Writes and Optimistic Locking