How do you handle pagination in DynamoDB queries?
Learn how DynamoDB pagination works with LastEvaluatedKey and ExclusiveStartKey, the 1 MB result limit, cursor-based paging and SDK paginator examples.
Expected Interview Answer
DynamoDB paginates by returning a LastEvaluatedKey with each page of results; you feed that key back as ExclusiveStartKey on the next Query or Scan request to fetch the following page, repeating until no LastEvaluatedKey is returned.
A single Query or Scan returns at most 1 MB of data, and it can also stop early if you set a Limit, so results are naturally chunked into pages. Each response includes LastEvaluatedKey — a pointer to the last item read — and passing it as ExclusiveStartKey resumes exactly where you left off. Because the key is opaque and cursor-based (not an offset), there is no OFFSET/page-number style paging like SQL; you cannot jump directly to page 50 without walking the pages before it. SDK paginators automate this loop, and the Limit parameter caps items per page but is applied before filter expressions, so a filtered page can return fewer items than the limit.
- Cursor-based paging scales without expensive offset scans
- Handles the 1 MB per-request result cap gracefully
- LastEvaluatedKey resumes exactly where the last page stopped
- Limit controls page size for predictable responses
- SDK paginators automate the fetch loop
AI Mentor Explanation
Think of reading a long match commentary log where you can only take one page at a time. When you stop, you slip a bookmark on the last ball you read; next session you open to the bookmark and continue, never restarting from over one. DynamoDB's LastEvaluatedKey is that bookmark — you pass it back to resume exactly where the previous page ended.
Step-by-Step Explanation
Step 1
Run the initial Query
Issue a Query (or Scan), optionally with a Limit to cap items per page; DynamoDB returns up to 1 MB of data.
Step 2
Read LastEvaluatedKey
Check the response for LastEvaluatedKey; its presence means more results exist beyond this page.
Step 3
Pass it as ExclusiveStartKey
Send the same query again with ExclusiveStartKey set to the previous LastEvaluatedKey to fetch the next page.
Step 4
Repeat until exhausted
Continue looping until a response comes back without a LastEvaluatedKey, meaning you have read every matching item.
Step 5
Account for filters
Remember Limit applies before FilterExpression, so a page may contain fewer items than the limit even when more matches exist.
What Interviewer Expects
- Knows LastEvaluatedKey / ExclusiveStartKey is the pagination mechanism
- Understands the 1 MB per-request result limit
- Recognizes paging is cursor-based, not offset/page-number based
- Aware Limit is applied before FilterExpression
- Can mention SDK paginators that automate the loop
Common Mistakes
- Expecting SQL-style OFFSET or jumping directly to an arbitrary page
- Assuming a single Query returns all matching items regardless of size
- Thinking Limit filters results after a FilterExpression rather than before
- Forgetting to loop until LastEvaluatedKey is absent, missing later pages
- Reconstructing ExclusiveStartKey by hand instead of passing back the returned key
Best Answer (HR Friendly)
“DynamoDB gives back results one page at a time along with a bookmark that points to where the page ended. To get the next page you send the same request with that bookmark, and you keep going until no bookmark comes back, meaning you have read everything.”
Code Example
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
const client = DynamoDBDocumentClient.from(new DynamoDBClient({}));
async function getAllOrders(customerId) {
const items = [];
let ExclusiveStartKey;
do {
const res = await client.send(new QueryCommand({
TableName: "Orders",
KeyConditionExpression: "customerId = :c",
ExpressionAttributeValues: { ":c": customerId },
Limit: 100,
ExclusiveStartKey,
}));
items.push(...res.Items);
ExclusiveStartKey = res.LastEvaluatedKey; // undefined when done
} while (ExclusiveStartKey);
return items;
}Follow-up Questions
- Why can't you jump directly to an arbitrary page like SQL OFFSET?
- How does the 1 MB result limit affect pagination behavior?
- How does Limit interact with a FilterExpression?
- How would you implement forward and backward paging in a UI?
- What is the difference between paginating a Query and a Scan?
MCQ Practice
1. Which value do you pass to fetch the next page of a DynamoDB Query?
DynamoDB is cursor-based: pass the previous response's LastEvaluatedKey as ExclusiveStartKey to continue.
2. What is the maximum amount of data a single Query or Scan returns?
A single request returns at most 1 MB of data, after which you must paginate with LastEvaluatedKey.
3. How does Limit interact with a FilterExpression?
Limit caps items read before the filter is applied, so a filtered page can contain fewer items than the limit.
Flash Cards
What signals more pages exist in DynamoDB? — A LastEvaluatedKey in the response; its absence means you've read everything.
How do you fetch the next page? — Set ExclusiveStartKey to the previous LastEvaluatedKey and repeat the query.
Is DynamoDB paging offset-based? — No — it is cursor-based, so you can't jump directly to an arbitrary page.
When is Limit applied relative to a FilterExpression? — Before filtering, so a page may return fewer items than the limit.
Continue Learning
Related Interview Questions
What is the difference between Query and Scan operations in DynamoDB?
medium
What is the difference between a key condition and a filter expression, and why does it matter for cost?
medium
What is the difference between DynamoDB and a relational database?
medium
What is single-table design in DynamoDB and why is it recommended?
hard