Skip to content
IT glossary
BackendUpdated December 3, 2026

Pagination

Пагинация

A way to return a long list in smaller batches instead of sending everything at once. The client can request a batch by page number, offset, or a continuation token called a cursor.

Also known as: cursor pagination, offset

What pagination does

When you open an order history, you probably need the latest 20 orders, not every order ever placed. A paginated API sends one batch at a time and gives the client a way to ask for more. That keeps responses smaller and lets the client display results without waiting for the entire list. A page is just a batch of results: numbered buttons, a “Load more” button, and infinite scroll can all use pagination.

Offset vs. cursor pagination

Offset pagination tells the server how many results to skip. GET /orders?limit=20&offset=40 skips the first 40 orders in the chosen sort order and returns up to 20. The client can calculate the offset for any numbered page, which makes this a good fit for a small admin table. Deep offsets tend to get slower because the database still has to walk past the skipped rows. Positions can also shift: if a new order arrives at the top after you load page one, page two can repeat the last order you already saw. Deleting a row near the top can shift an unseen order onto a page you've already passed.

Cursor pagination gives the client a continuation token to send back with its next request. The client treats the token as opaque. In a common implementation called keyset pagination, the token identifies the last returned row's sort key, so the next query starts after that key rather than skipping a fixed number of rows. A suitable index can make this efficient even deep into the list. The trade-off is that a cursor normally lets you continue from a known position, not jump straight to page 100.

Example: paging through recent orders

Suppose a PostgreSQL API sorts orders by creation time, newest first. Creation times can tie, so it uses the unique id as a tie-breaker. On the first request, omit the WHERE clause. On subsequent requests, bind the previous page's final returned timestamp and ID as $1 and $2:

SELECT id, created_at
FROM orders
WHERE (created_at, id) < ($1, $2)
ORDER BY created_at DESC, id DESC
LIMIT 21;

For a page size of 20, the extra row tells the server whether there is more to fetch. If it finds 21 rows, it sends 20 and builds next_cursor from row 20, not row 21. Otherwise row 21 would be skipped on the next request. A composite index on (created_at, id) can support both the comparison and the ordering.

Pitfalls and interview questions

Both approaches need an unambiguous sort order. Adding a cursor does not freeze the result set across requests: a row whose sort key changes can move across the boundary and be missed or seen twice. Start over when the filters or sort order change. Cap the page size on the server so that one request cannot accidentally fetch the whole collection.

In an interview, ask whether users need to jump to a numbered page, how often the data changes, and whether the API must return an exact total. A feed usually needs only a next cursor, while a back-office table may need page numbers. An exact count of all matching rows is separate from fetching a page and can be more expensive than the page query itself.