Currently Available: Need a skilled Software Developer for your next project?
Categories
APIs

Comparing Cursor, Offset, and Link Header Pagination

When a client requests a collection of records from an API, the server often returns only part of the set at a time. You use pagination so the client can ask for the rest in smaller batches. Offset and cursor pagination control how the server moves through those records, while an HTTP Link header gives the client the exact URLs to follow next. An API can pair a Link header with either offset or cursor pagination.

Have the client send an offset when users need numbered pages and your database can serve those positions efficiently. Have the client send a cursor when it mainly steps forward or backward through an ordered list. Add Link headers when you want the server to supply the navigation URLs for the client to follow. None of these setups stops data from shifting while a client pages through results.

How offset and cursor pagination move through results

When a client uses offset pagination, it asks the server to skip a set number of results and return the next batch. The limit tells the server how many records to return, and the offset tells it how many to skip. Because the client can calculate the next offset or jump straight to a known position, this approach works cleanly for numbered pages.

To make those pages reliable, the server must put the records in a predictable, unique order on every request. PostgreSQL’s documentation warns that if a query lacks an ORDER BY clause that establishes a unique order, the database returns unpredictable rows. It also notes that the database still has to compute the rows it skips, so large offsets can slow a query down. PostgreSQL gives no universal cutoff where an offset becomes too costly; the impact depends on your query and how the database executes it, so you need to test your own setup. See PostgreSQL’s LIMIT and OFFSET guidance.

Because the client asks for a numeric position in the current results, changes to the dataset can shift what sits at that spot. If the server inserts or deletes records before the requested offset, the client’s next request can repeat records it already saw or miss records completely. Offset pagination fits well when clients jump to a specific page, though a full scan can drift if the underlying collection changes.

When a client uses cursor pagination, it sends a marker from the previous response and asks the server to continue from that point in the ordered results. That marker might be an object ID, an encoded position, or an opaque token that the client must not try to read. For example, Stripe’s webhook-endpoint API uses object IDs with starting_after and ending_before parameters. Other APIs use tokens with different rules, so clients should follow each API’s documentation rather than assume cursors work the same way everywhere.

With a cursor, the client can keep moving through results without asking the server to skip a growing number of rows. Speed still depends on how the server orders and fetches the records. A cursor also does not freeze the dataset into a fixed snapshot. Shopify documents that if a record’s sort value changes while a client pages through its GraphQL API, the server can skip that record or return it again (changing a node’s sort value). For a complete scan in that API, Shopify recommends sorting by a key that does not change in its GraphQL pagination guidance.

Link headers tell clients where to go next

When a server includes an HTTP Link header in a response, it gives the client URLs for related resources. The server can label a URL as the next or previous page, so the client follows the address it receives instead of building a query string on its own. RFC 8288 defines this web-link model and header format. It leaves offset pagination, cursor pagination, page sizes, and consistency rules up to the API.

The server can put an offset, a cursor, filters, sort settings, or other request details inside a Link header URL. The header tells the client where to go next, while the URL parameters and the server’s rules determine how the server moves through the records.

GitHub’s REST API shows how this works in practice: the server can return next, prev, first, and last links in paginated responses. Some links may be missing, and the server may omit the header completely when all results fit on one page or when an endpoint does not support pagination. Clients should follow the links the server returns rather than assume every relation will be present. See GitHub’s REST pagination documentation.

Comparing the trade-offs

Concern Offset Cursor Link header
What it controls A position, measured by how many results the server skips A continuation point in an ordered result set How the server supplies navigation URLs in the response
Typical client action Increase the offset or request a numbered page Send back the returned cursor or token Follow the supplied URL
Jumping to a distant page Straightforward when the API supports page numbers or offsets Less natural when the client must advance from a known cursor Depends on which links the server supplies
Deep traversal Large offsets may force the database to compute skipped rows Cost depends on how the server designs and queries the cursor Does not determine the cost of the linked request
Changing data Insertions or removals before the requested position can shift results Behavior depends on cursor rules and sorting; a cursor is not automatically a snapshot Inherits the behavior of the pagination method in the URL
Main caution Set a unique sort order and watch the cost of large offsets Document what the cursor means and what consistency it provides Treat the header as navigation metadata, rather than a pagination algorithm

No universal performance ranking follows from these trade-offs. PostgreSQL flags a cost concern for large offsets, yet the reviewed API and standards documentation does not establish a general speed advantage for cursor pagination or a numeric crossover point where one beats the other.

Standards leave room for different designs

No single API standard requires every service to use offset or cursor pagination. RFC 8288 standardizes web links while leaving the pagination strategy to the API. JSON:API does the same, reserving the page query-parameter family for pagination conventions. Its v1.1 specification shows examples of both page-number and cursor parameters without requiring one underlying method.

Google’s AIP-158 gives stricter rules for APIs that follow Google’s design system. It defines page-size and page-token fields, requires the server to keep page tokens opaque, and states that a token must not act as authorization. Those rules apply to APIs following AIP-158 rather than every API. The AIP-158 pagination guidance also notes that page tokens and skip behavior can coexist.

Real-world APIs combine these pieces in different ways. Shopify’s legacy REST Admin API pairs cursor pagination with next-page links in response headers. Shopify marks that API as legacy and directs new integrations to its GraphQL Admin API, so the example shows how the mechanisms fit together rather than a current recommendation for new Shopify integrations. Microsoft Graph uses another pattern: the server places an opaque continuation token inside an @odata.nextLink URL. That gives the client a server-provided next URL without using an HTTP Link header.

Choose based on how clients need to navigate

Have clients send an offset and limit when they need numbered pages or direct jumps to known positions, and your database query can serve those positions efficiently. Set a deterministic, unique sort order, and test the query at the largest offsets you expect clients to request.

Have clients send a cursor when they mainly move forward or backward through an ordered collection. Document which fields set the order, what the cursor represents, and whether changes to records during a scan can repeat or skip items. Tell clients to return opaque tokens unchanged.

Add Link headers when you want the server to build navigation URLs and preserve filters or other request parameters. Tell clients to follow the URLs the server supplies and handle missing relations. A server can place an offset or a cursor inside a next link, and it can return a cursor without using a Link header at all. For more detail on how these mechanisms fit together, see how Link-header pagination works and how cursor tokens work.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *