Skip to content
Quest 15Advanced15 min

Scrolling the Pokegear (Pagination)

The Johto Pokédex has 100 new Pokémon. Loading them all at once is slow and expensive. Instead, we fetch the first 10. DynamoDB returns a LastEvaluatedKey, telling us exactly where it stopped.
Start the lesson

What you'll learn

  • Each Query or Scan call reads at most 1 MB (before any filter), so you must paginate even without a Limit.
  • Limit controls items evaluated, not necessarily items returned after filtering.
  • Pass the LastEvaluatedKey back as the ExclusiveStartKey to get the next page.

Section 01

Scrolling the Pokegear

When you scan or query, Limit caps the items DynamoDB evaluates before applying a filter. The returned item count can therefore be smaller or even zero. Continue while LastEvaluatedKey is present; only an absent or empty key establishes that traversal is finished. To get the next page, you start exactly where you left off by passing that key as the ExclusiveStartKey.

Pagination RequestJavaScript
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, ScanCommand } from "@aws-sdk/lib-dynamodb";

const client = new DynamoDBClient({ region: "ap-south-1" });
const docClient = DynamoDBDocumentClient.from(client);

// Fetching Page 2
await docClient.send(new ScanCommand({
  TableName: "JohtoPokemon",
  Limit: 10,
  ExclusiveStartKey: { Name: "Meganium" } // This came from the previous request's LastEvaluatedKey
}));
Your appAWS SDK v3DynamoDBtableP0P1P2P3CONSUMED0

Ready · 5 steps · Sprig

This animation plays when you scroll to it. Press Play to start it now.

All steps as text
  1. Your app sends Scan to the JohtoPokemon table. Limit 3, no ExclusiveStartKey.
  2. P1 returns Chikorita, Bayleef, Meganium.
  3. This call consumed 0.5 RCU.
  4. The page holds 3 items and ends with LastEvaluatedKey {"Name":"Meganium"}. Pass it as ExclusiveStartKey to continue.
  5. DynamoDB responds with success: the same first page again, so the list never advances.

Practice

Your turn

Page 1 stopped at { Name: "Meganium" }. Pass that key back as the ExclusiveStartKey so the next request resumes exactly where the last one ended.
Add ExclusiveStartKey: { Name: "Meganium" } to fetch the next page.
Tab inserts two spaces. Press Escape, then Tab, to leave the editor. Control or Command plus Enter runs the code.
Simulated — no AWS calls

Check

Check your understanding

Quick Quiz · one question, as many tries as you need.

Which parameter do you use to tell DynamoDB where to start reading the next page of results?

Choose one answer.