0
0
DynamodbHow-ToBeginner · 4 min read

How to Paginate Results in DynamoDB: Simple Guide

To paginate results in DynamoDB, use the LastEvaluatedKey from the previous query or scan response as the ExclusiveStartKey in the next request. This lets you fetch the next set of items in batches, avoiding large data loads at once.
📐

Syntax

Pagination in DynamoDB uses two main parameters:

  • LastEvaluatedKey: Returned by DynamoDB in the response when there are more items to fetch.
  • ExclusiveStartKey: Sent in the next request to start reading from where the last query ended.

Use these parameters with Query or Scan operations to control the page size and navigate through results.

javascript
const params = {
  TableName: 'YourTableName',
  Limit: 10, // number of items per page
  ExclusiveStartKey: lastEvaluatedKey // from previous response, or undefined for first page
};

const result = await dynamodb.query(params).promise();
const lastEvaluatedKey = result.LastEvaluatedKey; // use this for next page
💻

Example

This example shows how to paginate through a DynamoDB table using the AWS SDK for JavaScript (v2). It fetches 3 items per page and continues until no more items remain.

javascript
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB.DocumentClient();

async function paginateDynamoDB() {
  let lastEvaluatedKey = undefined;
  do {
    const params = {
      TableName: 'Movies',
      Limit: 3,
      ExclusiveStartKey: lastEvaluatedKey
    };

    const data = await dynamodb.scan(params).promise();
    console.log('Page items:', data.Items);

    lastEvaluatedKey = data.LastEvaluatedKey;
  } while (lastEvaluatedKey);
}

paginateDynamoDB();
Output
Page items: [ { title: 'Movie1', year: 2020 }, { title: 'Movie2', year: 2019 }, { title: 'Movie3', year: 2018 } ] Page items: [ { title: 'Movie4', year: 2017 }, { title: 'Movie5', year: 2016 }, { title: 'Movie6', year: 2015 } ] ... (continues until all items are fetched)
⚠️

Common Pitfalls

Common mistakes when paginating DynamoDB results include:

  • Not checking if LastEvaluatedKey exists before using it, which can cause errors.
  • Ignoring the Limit parameter, leading to large data loads and slow responses.
  • Assuming LastEvaluatedKey is always present; it is only returned if more data exists.
  • Using pagination with inconsistent query conditions, which can cause missing or repeated items.
javascript
/* Wrong way: Not checking LastEvaluatedKey */
const params = { TableName: 'Movies', Limit: 5 };
const data = await dynamodb.scan(params).promise();
// Using data.LastEvaluatedKey without checking if it exists
const nextParams = { TableName: 'Movies', Limit: 5, ExclusiveStartKey: data.LastEvaluatedKey };

/* Right way: Check if LastEvaluatedKey exists before next request */
if (data.LastEvaluatedKey) {
  const nextParams = { TableName: 'Movies', Limit: 5, ExclusiveStartKey: data.LastEvaluatedKey };
  // proceed with next query
}
📊

Quick Reference

Key points for DynamoDB pagination:

  • Limit: Controls how many items to return per page.
  • LastEvaluatedKey: Marker returned by DynamoDB to indicate more data.
  • ExclusiveStartKey: Use this key to start the next page.
  • Always check if LastEvaluatedKey is present before paginating.
  • Works with both Query and Scan operations.

Key Takeaways

Use LastEvaluatedKey from the response as ExclusiveStartKey in the next request to paginate.
Set Limit to control the number of items per page and avoid large data loads.
Always check if LastEvaluatedKey exists before making the next paginated request.
Pagination works with both Query and Scan operations in DynamoDB.
Incorrect handling of pagination keys can cause missing or repeated data.