Quick revision
The short answer
The standard fixes one pagination model: cursor-based for large or changing collections, with offset allowed only for small admin lists. It fixes parameter names, a response envelope with items and a next cursor or links, and default and maximum page sizes. Sorting is allowed only on declared, indexed fields and unknown sort fields are rejected with 400; totals are optional and documented as possibly approximate. A shared Spring starter and shared OpenAPI components make it the easy path.
What to remember
In this answer, we wrote a pagination standard for web, mobile and partner clients. Cursor pagination is the default for large or changing collections, offset is allowed for small admin lists, and parameter names, the envelope, default and maximum page sizes are fixed. Sorting and filtering are limited to declared, indexed fields with 400 for anything else, totals are optional and may be approximate, and a shared starter plus OpenAPI components make the standard easier to follow than to avoid.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The standard fixes one pagination model: cursor-based for large or changing collections, with offset allowed only for small admin lists. It fixes parameter names, a response envelope with items and a next cursor or links, and default and maximum page sizes. Sorting is allowed only on declared, indexed fields and unknown sort fields are rejected with 400; totals are optional and documented as possibly approximate. A shared Spring starter and shared OpenAPI components make it the easy path.
Why this question is asked
Pagination looks like a detail until a partner integrates with ten of our APIs and finds ten different ways to ask for the next page. Web, mobile and partner clients all suffer when every team invents its own parameters, and the database suffers when every API allows sorting on any column. Interviewers ask this design question to see whether a candidate can choose rules that fit very different clients and back them with reasons. In this answer, we will choose the pagination model, define the parameters and envelope, set rules for sorting and filtering, decide what to promise about totals, and see how to make the standard easy to adopt.
1Choosing One Model, With One Exception
There are two common ways to paginate. Offset pagination (page and size, or offset and limit) lets clients jump to any page but gets slower as pages get deeper and shifts rows when data changes. Cursor pagination returns an opaque token pointing just after the last row; it costs the same on every page and is stable under concurrent writes, but clients can only move forward and backward.
Most of our clients are mobile feeds, web lists with infinite scroll and partners synchronising data, and all of them move forward page by page through collections that grow. So the standard makes cursor pagination the default. Offset is allowed for small, bounded admin lists, say under 10,000 rows, where jumping to page 7 in a back-office table is genuinely useful and cheap.
| Collection | Model | Reason |
|---|---|---|
| Large or growing (orders, events) | cursor | constant cost, stable under writes |
| Partner synchronisation | cursor | no duplicates or gaps while data changes |
| Small bounded admin list | offset allowed | random page access is cheap and useful |
| Search results ranked by score | cursor or capped offset | deep pages are rarely useful |
2Fixed Parameters and Envelope
Every API uses the same names, so clients and SDKs can share code. The standard fixes them precisely.
Notice the design choices. The cursor is opaque, so teams can change its encoding without breaking clients. The next link repeats the filters and sort, so a client can follow it without rebuilding the query. A default page size of 20 and a maximum of 100 keep responses small on mobile networks and protect databases; a request above the maximum is rejected rather than silently trimmed, so clients do not misread a short page as the end.
3Sorting and Filtering Rules
Sorting is where a public API can hurt its own database. If any column is sortable, a client can sort 50 million orders by an unindexed note field and trigger a full sort on every request. So each endpoint declares its sortable fields in the OpenAPI spec, each backed by an index together with a unique tiebreaker, and the sort syntax is fixed: sort=-createdAt for descending, sort=name for ascending.
An unknown sort or filter field is rejected with 400 and a problem details body naming the field. Ignoring it would be worse: the client would believe its list is sorted by price when it is not. Filters use plain query parameters named after fields (status=PAID, createdAfter=2026-10-01T00:00:00Z) and are also declared per endpoint, so the server never builds queries from arbitrary field names.
4Totals, Honestly Described
Exact totals require counting every matching row, which on large filtered collections can cost more than the page itself. The standard therefore makes totals optional: an endpoint may return totalEstimate when it is cheap, and the documentation says it may be approximate. Clients that need an exact number for a small collection can request it with includeTotal=true where the endpoint supports it. Promising little here keeps the door open as data grows, which is far cheaper than withdrawing a promise later.
5Making the Standard the Easy Path
A standard that each team reimplements will drift. So we ship it as code. A Spring Boot starter provides a CursorPage response record, a cursor encoder with signing, a resolver that parses limit, cursor and sort, validates them against the endpoint's declared fields and returns 400 for anything else, and helpers for keyset queries with Spring Data's ScrollPosition. Shared OpenAPI components define the parameters and the envelope once, and every spec references them, so documentation is consistent too. The API linter checks that list endpoints use those components.
- Guidelinerules and reasons, short
- Shared OpenAPI componentslimit, cursor, sort parameters and envelope schema
- Spring Boot starterresolver, validation, cursor codec,
CursorPage - Linter in CIlist endpoints must reference the components
- Service codedeclares sortable fields and its query
5 layers, from the top down. Step through them one at a time.
6Follow-up questions
How would you migrate an existing offset API to cursors without breaking clients?
We add cursor support to the same endpoint alongside the old parameters: if a request carries page and size, it keeps the old behaviour; if it carries cursor or omits page, it gets the cursor envelope. Responses to old-style requests include a next link with a cursor and a Deprecation header for the page parameter, so clients can switch one step at a time. We watch traffic using page, contact the remaining clients, and remove offset support after the published deprecation window.
Why limit and not size?
Either works; what matters is that every API uses the same name. We picked limit because it describes the maximum number of items without implying a page number, which fits cursors.
Should the cursor expire?
It can, if it carries data that becomes invalid, but most keyset cursors simply hold sort values and stay valid. We document that cursors are short-lived and opaque, which gives us room to expire them later if needed.
7Conclusion
In this answer, we wrote a pagination standard for web, mobile and partner clients. Cursor pagination is the default for large or changing collections, offset is allowed for small admin lists, and parameter names, the envelope, default and maximum page sizes are fixed. Sorting and filtering are limited to declared, indexed fields with 400 for anything else, totals are optional and may be approximate, and a shared starter plus OpenAPI components make the standard easier to follow than to avoid.
Open with
My standard would have one default model: cursor pagination for any collection that is large or keeps changing, because it costs the same on every page and does not duplicate or skip rows while data changes, which matters for mobile feeds and partner synchronisation.
Then, if they want more
Offset would be allowed only for small, bounded admin lists where jumping to a page number is useful. Parameter names are fixed, limit and cursor, with a default limit of 20 and a maximum of 100, and requests above the maximum get a 400. The response envelope is items, nextCursor and a next link that repeats the filters and sort.
Sorting is only allowed on fields each endpoint declares in its spec, each backed by an index with a unique tiebreaker, and unknown sort or filter fields are rejected with 400 rather than ignored, so clients never believe a list is sorted when it is not.
Totals are optional and documented as possibly approximate, because exact counts on big filtered sets are expensive and hard to withdraw later. And I would make it real with a Spring Boot starter that parses and validates the parameters and encodes cursors, plus shared OpenAPI components and a lint rule, so teams do not reimplement it.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Set a company standard for pagination, filtering and sorting in REST APIs used by web...": https://scrivio-interview-library.netlify.app/spring-boot/rest-api-design/dc087d2645/