DynamoDB indexes, capacity and streams

Advanced · 17 min

Global and local secondary indexes, on-demand and provisioned capacity, request units, throttling, streams, TTL and global tables.

Before thisDynamoDB tables, keys and queries

Hello Interview · 20 min · from 2:44

DynamoDB indexes, capacity and streams

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Hello InterviewReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A global secondary index is a second copy keyed differently, updated asynchronously and read with eventual consistency.
  • A local secondary index shares the partition key, allows strong reads, and must be created with the table.
  • Only items that have the index key appear in it, so a sparse index is a cheap way to list just the open or flagged ones.
  • A read unit covers 4 KB strongly or 8 KB eventually consistent. A write unit covers 1 KB, and each affected index adds writes.
  • On-demand mode is the default and recommended choice. Provisioned with auto scaling suits steady, predictable load.
  • Streams deliver an ordered, 24-hour log of item changes for triggering other work, so consumers must be idempotent.

The second question

Keys answer the questions you designed them for. Then someone asks for all open orders across customers, and your table, organised by customer, can only answer by scanning. A secondary index is another arrangement of the same data, maintained for you.

Global and local indexes

Global (GSI) Local (LSI)
Partition key any attribute same as the table
Created any time only with the table
Reads eventually consistent eventual or strong
Default quota 20 per table 5 per table
Size limit none per key 10 GB per partition key value

Reach for a GSI almost always. It behaves like a separate table that DynamoDB feeds from yours. Its keys can be composed from several attributes. You choose which attributes to project into it; what you leave out you must fetch from the table.

Only items that have the index’s key attributes appear in it. That gives the sparse index trick. I gave just the open orders a GSI1PK, then shipped one by removing the attribute:

GSI byStatus = OPEN                Count=2  ScannedCount=2
after shipping order 3             Count=1  ScannedCount=1
consistent read on GSI: ValidationException

A word of caution: a GSI key with one value, like OPEN, is a single hot partition at high volume. Spread it over several suffixed values and query them all. And if a GSI can’t keep up with writes, it can throttle writes to the base table.

Capacity

Usage is measured in units. A strongly consistent read of up to 4 KB is one read unit, an eventually consistent read half, a transactional one two. A write of up to 1 KB is one write unit, two in a transaction. Sizes round up, so a 6 KB item costs 2 units to read strongly and 6 to write. A Query sums sizes before rounding: 1,000 items of 200 bytes read in queries cost about 25 eventual units, while 1,000 separate GetItem calls cost 500.

On-demand mode bills per request and scales for you. It is the default and what AWS recommends for most workloads. It instantly serves up to traffic levels the table has seen before and adapts beyond them; a sharp jump far past any previous peak can still throttle while it catches up. You can set maximum throughput to cap a runaway bill.

Provisioned mode reserves units per second, usually with auto scaling, and can be cheaper for steady load. Auto scaling reacts after the fact, so spikes throttle. Either way, SDKs retry throttled requests with backoff, and you should alarm on throttles. Even with plenty of capacity in total, one hot partition hits its own limit.

Streams

A stream is a log of item-level changes, kept for 24 hours. Each record carries the keys and, as you choose, the old image, the new image or both. Order is preserved per item. Updating my order produced a MODIFY record whose old image had GSI1PK and whose new one didn’t, which is exactly what a consumer needs to notice “this order just shipped”.

The usual consumer is Lambda: update a search index, publish an event, maintain a total. This avoids writing to two systems from your application and hoping both succeed; see change data capture. A failing batch is retried and blocks its shard, so configure retry limits, batch splitting and a failure destination, and make handlers idempotent. For longer retention or many consumers, send changes to Kinesis instead.

Expiry and other Regions

Time to live names an attribute holding an expiry in epoch seconds. Expired items are deleted in the background without using write capacity, typically within a few days, not on the second.

Global tables replicate a table across Regions with writes accepted in each. By default replication is asynchronous (multi-Region eventual consistency) and concurrent writes to one item resolve as last writer wins. A multi-Region strong consistency mode is available with its own constraints, and the mode can’t be changed later.

About the video

The embed is a clip from Hello Interview’s “DynamoDB Deep Dive” (January 2025), 2:44 to 22:22. The chapters in it cover indexing, how to use it, architecture and advanced features. It is framed for system design interviews, by a third party rather than AWS, and may predate newer features; trust the docs for limits. I judged it from chapters and description; its audio is flagged as original US English.

What was run: the index, sparse index and stream outputs ran with boto3 against moto 5.1.22, which doesn’t model asynchronous index updates, capacity or throttling. The unit arithmetic is a few lines of local Python. The rest comes from the DynamoDB developer guide, checked in November 2026.

Check yourself

  1. 01You write an item and immediately query a GSI for it, and it isn't there. Is something broken?

    No. Global indexes are updated asynchronously and support only eventually consistent reads. Read the table by key if you need the latest value.

  2. 02How many write units does a 6 KB item cost in an ordinary write and in a transaction?

    Six for a standard write, one per started KB, and twelve in a transaction. Writes to secondary indexes are charged on top.

  3. 03You want a list of unshipped orders without scanning millions of shipped ones. What do you build?

    A sparse GSI keyed on an attribute that only unshipped orders have, removing it when the order ships.

  4. 04A TTL timestamp passed an hour ago and the item is still returned. Why?

    TTL deletion is a background process that may take days. Filter out expired items in queries if they must not appear.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark