CLOUD CONCEPT

Pagination

Pagination divides a large result set into smaller responses that clients retrieve through successive requests.

Cloud Concepts Hub →

What is Pagination

Pagination lets applications consume or expose large collections without transferring every record in a single response. Correct implementations control resource usage while ensuring clients follow continuation information, handle failures, and understand how changing data affects completeness.

Pagination is a request-and-response pattern for retrieving a collection in manageable portions called pages. A request specifies a desired page size and, after the first request, a position from which to continue. The response contains records and information indicating how to request another page. Cloud APIs use pagination to bound response sizes, memory consumption, processing time, and network transfer per request.

Common approaches include page numbers, offsets, and continuation tokens or cursors. An offset-based API might accept limit=100 and offset=200 to request records after the first 200 results. A cursor-based API instead returns a position associated with the previous page. Offset pagination supports convenient navigation but can become expensive for deep database queries. Cursor-based pagination often scales better for sequential traversal, although arbitrary jumps to a particular page may not be supported.

When ingesting data from an API, request the first page, process its records, and follow the documented continuation mechanism until the API indicates completion. Do not assume that a short or empty page means the collection is exhausted. Some APIs apply filters after reading a bounded amount of data, so they can return no matching records while still providing continuation information. Keep filters, sorting, and other parameters consistent across requests as required by the API, and treat continuation tokens as opaque values rather than decoding or modifying them.

For example, an ingestion job can retrieve pages from a supplier API and write each page to object storage before requesting the next one. Persist progress only after the corresponding records are durably processed. A failure between writing records and saving progress can cause the same page to be processed again, so use idempotent writes or deduplication keys. Check whether tokens expire or are valid only within a particular session; a saved token is not necessarily a durable incremental-ingestion checkpoint.

When creating a data API, define a default page size, enforce a maximum, and document the continuation contract. Use deterministic ordering with a unique tie-breaker, such as an event timestamp followed by a record identifier. Validate tokens and bind them to relevant query parameters or authorization context where appropriate. A token must never replace authorization checks on each request. API gateways can expose and validate pagination parameters, but they do not automatically paginate arbitrary backend results.

Pagination does not guarantee a consistent snapshot. Concurrent inserts, updates, and deletes can cause records to move between pages, particularly with offsets. Keyset cursors reduce positional shifts but do not automatically provide snapshot isolation. Exports requiring a fixed view may need database snapshot support, versioned data, or an API-defined snapshot token. Pagination also differs from incremental ingestion: dividing results into pages does not determine which records changed since a previous run.

When calling AWS APIs from code, consult the operation's pagination contract or use an SDK paginator when available. For example, Boto3 provides client.get_paginator('list_objects_v2') for Amazon S3 object listings. A single low-level SDK call generally returns only one service response, whereas a paginator iterates through multiple responses. SDK page-size controls and total-item limits have different purposes, and support varies by operation. Larger pages reduce request overhead but increase per-response memory and retry work; smaller pages increase request count and possible throttling. Apply bounded retries with backoff, process pages incrementally, and monitor request counts, errors, and completed records.

Key aspects of Pagination

Reliable pagination combines bounded responses, correct continuation handling, predictable ordering, recoverable processing, and per-request access control.

Explicit continuation contract

Clients advance using the API's documented token, cursor, offset, or next-page link. Determine completion from that contract rather than assuming that a short response is the last page.

Bounded response size

Default and maximum page sizes limit resource consumption per request. A requested item limit may be constrained by byte limits or service-specific evaluation rules.

Ordering and consistency

Stable ordering with a unique tie-breaker makes traversal more predictable. Consistency across pages requires separate consideration because pagination alone does not freeze the underlying data.

Recoverable ingestion

Save continuation state after successfully processing its associated data, and make repeated processing safe. Confirm token lifetime and scope before relying on a token for restart recovery.

SDK-assisted traversal

SDK paginators automate supported continuation protocols but still issue multiple API requests. Process their output incrementally and distinguish per-request page size from a total retrieval limit.

Performance and access control

Balance request overhead against memory, latency, throttling, and retry costs. Enforce authorization for every page and prevent token tampering or reuse outside its intended query context.

Cloud services for Pagination

API Gateway can expose a data API backed by Lambda, with DynamoDB providing records through paginated queries. The function validates page-size inputs, checks authorization, and translates backend continuation state into the API's documented response contract. For ingestion, code can follow an external API's pages and write results to Amazon S3. SDK paginators simplify supported AWS listing operations, including S3 object discovery, but do not provide durable workflow checkpoints or deduplication. Across these services, engineers must preserve continuation state correctly, respect throttling and execution limits, and distinguish page-by-page retrieval from snapshot consistency or incremental change capture.

Amazon DynamoDB image Amazon DynamoDB

Query and Scan read up to their applicable limit or 1 MB before applying filter expressions. Supply a returned LastEvaluatedKey as ExclusiveStartKey for the next request; even an empty filtered page can require continuation.

Open Amazon DynamoDB →
Amazon Simple Storage Service image Amazon S3

ListObjectsV2 returns at most 1,000 objects per request and uses IsTruncated and NextContinuationToken to describe continuation. Pass the returned token as ContinuationToken, or use an SDK paginator to traverse the listing.

Open Amazon S3 →
Amazon API Gateway image Amazon API Gateway

API Gateway exposes custom data APIs whose request and response contracts can include page sizes and continuation tokens. The backend or integration must implement pagination; the gateway does not automatically divide arbitrary backend results into pages.

Open Amazon API Gateway →
AWS Lambda image AWS Lambda

Lambda functions can implement paginated API backends or consume paginated APIs through SDK calls. Long traversals must account for invocation time limits by persisting progress and continuing work across invocations when necessary.

Open AWS Lambda →