Bird
Raised Fist0
DynamoDBquery~10 mins

Scan pagination in DynamoDB - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Scan pagination
Start Scan
Fetch Page of Items
Check for LastEvaluatedKey
Yes No
Save LastEvaluatedKey
Use LastEvaluatedKey to Fetch Next Page
Fetch Page of Items
Scan reads items page by page. If more items exist, it returns a LastEvaluatedKey to fetch the next page.
Execution Sample
DynamoDB
response = table.scan(Limit=2)
items = response['Items']
last_key = response.get('LastEvaluatedKey')
if last_key:
  response = table.scan(Limit=2, ExclusiveStartKey=last_key)
Scan DynamoDB table with a limit of 2 items per page, then fetch next page if more items exist.
Execution Table
StepActionItems FetchedLastEvaluatedKey Present?Next Step
1Scan with Limit=2, no start keyItem1, Item2YesSave LastEvaluatedKey, fetch next page
2Scan with Limit=2, ExclusiveStartKey=last_keyItem3, Item4YesSave LastEvaluatedKey, fetch next page
3Scan with Limit=2, ExclusiveStartKey=last_keyItem5NoEnd scan, no more pages
💡 No LastEvaluatedKey returned at step 3, scan completed all items.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
items[][Item1, Item2][Item1, Item2, Item3, Item4][Item1, Item2, Item3, Item4, Item5]
last_keynullkey after step 1key after step 2null
Key Moments - 2 Insights
Why do we need to check for LastEvaluatedKey after each scan?
Because LastEvaluatedKey tells us if there are more items to fetch. Without it, we don't know if the scan is complete (see execution_table rows 1 and 2).
What happens if we don't use ExclusiveStartKey for the next scan?
The scan will start from the beginning again, causing repeated items and an infinite loop (refer to concept_flow loop back).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what items are fetched at step 2?
AItem3, Item4
BItem1, Item2
CItem5
DNo items
💡 Hint
Check the 'Items Fetched' column in execution_table row 2.
At which step does the scan end because no LastEvaluatedKey is returned?
AStep 2
BStep 3
CStep 1
DScan never ends
💡 Hint
Look at the 'LastEvaluatedKey Present?' column in execution_table.
If the Limit is increased to 5, how would the execution_table change?
AMore steps with fewer items per step
BSame number of steps and items
CFewer steps with more items per step
DScan would not return LastEvaluatedKey
💡 Hint
Increasing Limit fetches more items per scan, reducing total steps (see concept_flow).
Concept Snapshot
Scan pagination in DynamoDB:
- Scan returns items in pages limited by 'Limit'.
- If more items exist, 'LastEvaluatedKey' is returned.
- Use 'ExclusiveStartKey' with LastEvaluatedKey to fetch next page.
- Repeat until no LastEvaluatedKey is returned.
- Prevents loading all items at once, useful for large tables.
Full Transcript
Scan pagination in DynamoDB works by fetching items in small pages. Each scan request returns a limited number of items and may include a LastEvaluatedKey if more items remain. This key is used as ExclusiveStartKey in the next scan to continue from where the last scan ended. This process repeats until no LastEvaluatedKey is returned, indicating all items have been scanned. This method helps manage large datasets efficiently by loading data in chunks.

Practice

(1/5)
1. What is the main purpose of using Scan pagination in DynamoDB?
easy
A. To speed up writes to the database
B. To delete items in batches
C. To create indexes automatically
D. To break a large scan into smaller parts for easier processing

Solution

  1. Step 1: Understand Scan Pagination Concept

    Scan pagination is used to divide a large scan operation into smaller chunks to avoid timeouts and manage resource use.
  2. Step 2: Identify the Purpose

    The main goal is to process big scans in parts, not to speed writes or create indexes.
  3. Final Answer:

    To break a large scan into smaller parts for easier processing -> Option D
  4. Quick Check:

    Scan pagination = break large scan into parts [OK]
Hint: Scan pagination splits big scans into smaller chunks [OK]
Common Mistakes:
  • Thinking pagination speeds up writes
  • Confusing scan with index creation
  • Assuming pagination deletes items
2. Which of the following is the correct way to continue a paginated scan in DynamoDB?
easy
A. Use LastEvaluatedKey as the ExclusiveStartKey in the next scan
B. Use Limit as the ExclusiveStartKey
C. Use ScanIndexForward to continue scanning
D. Use StartKey to begin the scan

Solution

  1. Step 1: Identify Pagination Parameters

    In DynamoDB, LastEvaluatedKey from the previous scan is used as ExclusiveStartKey to continue scanning.
  2. Step 2: Eliminate Incorrect Options

    Limit sets chunk size, not start key. ScanIndexForward is for queries, not scans. StartKey is not a valid parameter.
  3. Final Answer:

    Use LastEvaluatedKey as the ExclusiveStartKey in the next scan -> Option A
  4. Quick Check:

    Continue scan = ExclusiveStartKey = LastEvaluatedKey [OK]
Hint: Use LastEvaluatedKey as ExclusiveStartKey to continue scan [OK]
Common Mistakes:
  • Using Limit as ExclusiveStartKey
  • Confusing scan with query parameters
  • Using invalid parameter names
3. Given the following scan code snippet, what will be the output if the table has 15 items and Limit is set to 5?
response = table.scan(Limit=5)
items = response['Items']
last_key = response.get('LastEvaluatedKey')
print(len(items), last_key is not None)
medium
A. 5 False
B. 15 False
C. 5 True
D. 15 True

Solution

  1. Step 1: Understand Limit Effect on Scan

    Setting Limit=5 returns up to 5 items in one scan call, not all 15.
  2. Step 2: Check LastEvaluatedKey Presence

    Since there are more items after 5, LastEvaluatedKey will be present (not None), indicating more data.
  3. Final Answer:

    5 True -> Option C
  4. Quick Check:

    Limit=5 returns 5 items and LastEvaluatedKey exists [OK]
Hint: Limit controls items returned; LastEvaluatedKey shows more data [OK]
Common Mistakes:
  • Assuming all items return ignoring Limit
  • Expecting LastEvaluatedKey to be None always
  • Confusing item count with total table size
4. You wrote this code to paginate a scan but it returns only the first page repeatedly:
response = table.scan(Limit=3)
items = response['Items']
while 'LastEvaluatedKey' in response:
    response = table.scan(Limit=3)
    items.extend(response['Items'])
print(len(items))

What is the error?
medium
A. Missing ExclusiveStartKey in the subsequent scan calls
B. Limit should be increased to get all items
C. Items list should be reset inside the loop
D. LastEvaluatedKey should be deleted after each scan

Solution

  1. Step 1: Analyze Pagination Loop

    The loop calls scan repeatedly but does not pass ExclusiveStartKey, so it always scans from start.
  2. Step 2: Identify Missing Parameter

    To continue scanning, ExclusiveStartKey must be set to previous LastEvaluatedKey in each call.
  3. Final Answer:

    Missing ExclusiveStartKey in the subsequent scan calls -> Option A
  4. Quick Check:

    Pagination needs ExclusiveStartKey to continue [OK]
Hint: Pass ExclusiveStartKey to continue scan pages [OK]
Common Mistakes:
  • Not passing ExclusiveStartKey in loop
  • Increasing Limit instead of fixing pagination
  • Resetting items list inside loop
5. You want to scan a DynamoDB table with 1000 items but only want to process 100 items at a time to avoid timeouts. Which approach correctly implements scan pagination to achieve this?
hard
A. Set ExclusiveStartKey to null and scan with Limit=100 once
B. Use a loop with Limit=100 and pass ExclusiveStartKey from LastEvaluatedKey until no more keys
C. Use ScanIndexForward=true with Limit=100 to paginate
D. Scan once with Limit=1000 and split results in code

Solution

  1. Step 1: Understand Pagination Requirements

    To process 100 items at a time, scan must be done in chunks with Limit=100 and continue using ExclusiveStartKey.
  2. Step 2: Evaluate Options

    Use a loop with Limit=100 and pass ExclusiveStartKey from LastEvaluatedKey until no more keys correctly loops with Limit=100 and uses ExclusiveStartKey from LastEvaluatedKey. Scan once with Limit=1000 and split results in code fetches all at once, risking timeout. Use ScanIndexForward=true with Limit=100 to paginate uses wrong parameter for scan. Set ExclusiveStartKey to null and scan with Limit=100 once scans only once without continuation.
  3. Final Answer:

    Use a loop with Limit=100 and pass ExclusiveStartKey from LastEvaluatedKey until no more keys -> Option B
  4. Quick Check:

    Paginate with Limit and ExclusiveStartKey loop [OK]
Hint: Loop with Limit and ExclusiveStartKey until no LastEvaluatedKey [OK]
Common Mistakes:
  • Fetching all items at once risking timeout
  • Using query parameters for scan
  • Not looping to continue scan