Pagination
- In Turkish
- Sayfalama
In short
Pagination is the practice of splitting a large set of results into smaller pages, so an API or website returns a manageable number of items per request.
What is pagination?
Pagination divides a long list of results, such as products, search hits, or messages, into smaller chunks called pages. Instead of returning a million rows at once, which would be slow and use a huge amount of memory, an API returns, say, 20 items along with a way to ask for the next 20. The same idea powers numbered page links, 'Load more' buttons, and infinite scrolling in user interfaces.
The two most common techniques are offset and cursor pagination. Offset pagination uses parameters like ?limit=20&offset=40 or ?page=3 and maps directly to SQL's LIMIT and OFFSET, which makes it easy to build and lets users jump to any page. Cursor pagination returns a token that points to the last item seen, like ?cursor=abc123, and the next request continues right after that item.
Offset pagination has two drawbacks on large or fast-changing data: the database still has to read and skip every row before the offset, so deep pages get slow, and items can be skipped or repeated when rows are added or deleted between requests. Cursor pagination, called keyset pagination when the cursor is a column value such as an ID or timestamp, avoids both problems by using an index to jump straight to the right place. Its trade-off is that you can't jump directly to page 50.
A good analogy is reading a long book: offset pagination is like saying 'go to page 50', while cursor pagination is like placing a bookmark and continuing from it. APIs usually describe how to get the next page in the response, with a nextCursor field, a hasMore flag, or a Link header, and they cap the page size so clients can't request everything at once.
Key takeaways
- Pagination returns large result sets in smaller pages.
- Offset pagination (
limitandoffset) is simple and allows jumping to any page. - Cursor pagination continues after the last item seen and stays fast on large tables.
- Offset pagination can skip or repeat items when data changes between requests.
- APIs should cap the page size and tell clients how to fetch the next page.
Example
-- Offset pagination: page 3 with 20 items per page (skips 40 rows)
SELECT id, title FROM posts
ORDER BY id
LIMIT 20 OFFSET 40;
-- Cursor (keyset) pagination: the next 20 items after the last id seen (1040)
SELECT id, title FROM posts
WHERE id > 1040
ORDER BY id
LIMIT 20;Readers ask
What is the difference between offset and cursor pagination?
Offset pagination asks for items by position, such as skip 40 and take 20, while cursor pagination asks for the items that come after a specific item. Offset is simpler and supports page numbers; cursor is faster on large datasets and doesn't skip or repeat items when data changes.
How many items should an API return per page?
Common defaults are 20 to 100 items, with a maximum the client cannot exceed. The right size balances response time and payload size against how many requests clients need to make.
Is infinite scroll a type of pagination?
Yes. Infinite scroll is a user interface that loads the next page automatically as the user nears the end of the list, and behind the scenes it usually relies on cursor pagination.
See also
- REST APIBackend & APIs, p. 38A REST API is a web API that exposes data as resources identified by URLs and lets clients read or change them using standard HTTP methods.
- APIBackend & APIs, p. 2An API is a set of rules that lets one piece of software request data or actions from another in a predictable, documented way.
- SQLDatabases, p. 40SQL is the standard language for working with relational databases, used to create tables and to insert, query, update, and delete the data stored in them.
- Database IndexDatabases, p. 7A database index is a data structure that helps a database find rows quickly without scanning a whole table, much like the index at the back of a book.
- GraphQLBackend & APIs, p. 19GraphQL is a query language and runtime for APIs that lets clients request exactly the data they need, often from a single endpoint in a single request.
- EndpointBackend & APIs, p. 12An endpoint is a specific URL, combined with an HTTP method, where an API receives requests and returns responses for one particular resource or action.
Spotted a mistake or something missing on this page?Suggest an edit