Return big lists a page at a time, and let clients narrow and order them.
Pagination, filtering and sorting are the three tools that let an API return a small, useful slice of a big list instead of everything at once.
Imagine an endpoint GET /orders on a shop with 10 million orders. Sending all of them would take a long time, use a lot of memory on the server and on the phone, and almost nobody wants to read them all. So the API returns one page and tells the client how to get the next one.
There are two common ways to say "give me the next page":
?offset=40&limit=20 means "skip the first 40 rows and give me 20". It is simple, and the client can jump to any page.?after=8841&limit=20 means "give me 20 rows that come after the row I saw last". The server often wraps this value in an opaque string called a cursor.| Style | Request looks like | Good at | Weak at |
|---|---|---|---|
| Offset / limit | ?offset=40&limit=20 | Simple, easy to jump to any page | Slow on deep pages. Rows can repeat or be skipped when data changes. |
| Page number | ?page=3&per_page=20 | Friendly for numbered page links in a UI | Same weaknesses as offset (it is offset in disguise). |
| Cursor / keyset | ?after=8841&limit=20 | Fast at any depth. Stable while data changes. | No jumping to page 50. Needs a unique sort key and an index. |
GET /orders?status=paid&sort=-created_at&limit=2
{
"data": [
{ "id": 8842, "total": 30 },
{ "id": 8841, "total": 12 }
],
"next_cursor": "eyJpZCI6ODg0MX0",
"has_more": true
}Some APIs put the next link in a Link header instead of the body. Either works. The important part is that the client never has to build the cursor itself.
| Topic | Good practice | Why |
|---|---|---|
| Filter fields | Allow only a fixed list of fields, such as status, created_from, created_to | A free-for-all filter can force slow queries on columns with no index. |
| Operators | Keep them simple (equals, range, in a list) and document them | Clear rules are easier to validate and to optimise. |
| Sort fields | Allow only a fixed list of sortable fields | The database needs an index to sort a big table quickly. |
| Tie-breaker | Always add the unique id as the last sort key | Rows with the same date would otherwise come back in a random order, and pages would overlap. |
| Total count | Offer it only when needed, or give an approximate count | COUNT(*) on a huge filtered table is slow. |
| Maximum page size | Set a cap (for example, 100) and a default | Stops a client from asking for a million rows. |
A simplified model with a fixed table of 40 rows and no random numbers. The cost numbers are illustrative: they assume the database reads about 1,000 rows per millisecond and that the cursor query can use an index on the sort column. Real speeds depend on your hardware, data and indexes.
WHERE (created_at, id) < (?, ?) ORDER BY created_at DESC, id DESC LIMIT 20 is fast only when there is an index on (created_at, id). Add the index first, then expose the sort field.| Term | Meaning in easy words |
|---|---|
| Pagination | Cutting a long list into pages |
| Page size / limit | How many rows one response holds |
| Offset | How many rows to skip from the start of the list |
| Cursor | A marker that says where the last page ended |
| Keyset pagination | A cursor made from the sort column values, used in a WHERE clause |
| Filter | A condition that keeps only some rows |
| Sort order | The order the rows are returned in |
| Tie-breaker | A unique extra sort key (usually the id) that makes the order the same every time |
| Opaque cursor | A cursor whose inside the client does not read or build |
| Total count | The number of rows that match the filter in all pages |
| Allow-list | A fixed list of fields that clients may filter or sort by |
| Choice | We gain | We lose |
|---|---|---|
| Offset | Simple, can jump to any page | Slow when deep, unstable when data changes |
| Cursor | Fast and stable at any depth | No random page access, more design work |
| Return a total count | Nice page numbers in the UI | An extra, sometimes slow query |
| Allow any filter | Very flexible for clients | Slow queries and a bigger attack surface |
| Allow-listed filters | Predictable speed and safety | Clients cannot ask for every combination |
| Big page size | Fewer round trips | Slower responses and more memory |
/orders reads the whole table and takes the service down.created_at lets rows with the same time swap places between pages. Add the id.OFFSET 5000000 still reads and throws away five million rows.COUNT(*) on a big filtered table can cost more than the page itself.