Quick revision
The short answer
Early paging choices turn into contracts that clients depend on, which is why they are expensive to change once data has grown a hundredfold. I would change the standard so that new endpoints use keyset pagination with a restricted set of indexed sort keys and no exact totals for large collections, and migrate existing endpoints in order of their database cost, offering a new cursor-based version alongside the old one with a clear deprecation path. Design reviews should then ask how each query behaves at ten and one hundred times today's data.
What to remember
In this answer, we saw that offset paging, open sorting and exact totals all scale with the data, and that the early choices became contracts that clients depend on. The new standard is keyset pagination, restricted indexed sort keys and approximate or no totals for large collections; existing endpoints are migrated in order of database cost with guardrails, a new version and a deprecation path; and design reviews ask how each query behaves at ten and one hundred times the data.
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.
Early paging choices turn into contracts that clients depend on, which is why they are expensive to change once data has grown a hundredfold. I would change the standard so that new endpoints use keyset pagination with a restricted set of indexed sort keys and no exact totals for large collections, and migrate existing endpoints in order of their database cost, offering a new cursor-based version alongside the old one with a clear deprecation path. Design reviews should then ask how each query behaves at ten and one hundred times today's data.
Why this question is asked
Systems rarely fail because of one bad decision. They fail because reasonable decisions made at a small scale stay in place while the data grows. Offset paging, sorting on any field and exact totals were all fine when the orders table had 200,000 rows; at twenty million they are the top entries in the slow query log. Interviewers ask this at the most senior level because the fix is not a clever query but a change in standards, a migration plan that respects existing clients and a habit that prevents the next round. In this answer, we will see why each early choice degrades with growth, why they are hard to change, what the new standard should be, how to migrate existing endpoints in a sensible order, and how to build growth thinking into design reviews.
1Why the Early Choices Degrade
Offset paging asks the database to skip rows. At 200,000 rows, the deepest page skips at most a few thousand; at twenty million, a crawler or an export job asking for page 40,000 makes the database read and discard 800,000 rows per request. The cost grows with the page number and with the data.
Sorting on any field was cheap when every table fitted in memory; a sort of a few thousand rows takes milliseconds. A hundred times more data means the unindexed sorts now spill to disk, and each sortable field needs its own index to stay fast, which slows every write.
Exact totals, the 1,873,442 results line, need a count of every matching row on every request. At small scale the count is invisible; at large scale it is often the slowest query an endpoint runs, and it runs even when nobody looks at the number.
| Choice | At 200,000 rows | At 20 million rows |
|---|---|---|
| Offset paging, deep pages | A few ms | Seconds; grows with page number |
| Sort on any field | In-memory sort, fast | Disk sorts, or an index per field |
| Exact total on every page | A few ms | Often the slowest query of the endpoint |
| Keyset pagination, indexed sort | A few ms | A few ms |
2Why They Are Hard to Change
The difficulty is not technical. Once an endpoint returns page numbers and totals, clients build on them: a mobile app shows Page 3 of 94, a partner's integration loops through page=1..N every night, a back office screen sorts by any column. These behaviours have become a contract, even if nobody wrote it down. Changing them breaks someone, and every client has its own release cycle.
That is the lesson to carry forward: API and query choices made early are commitments, and the cost of changing them grows with both the data and the number of clients. The new standard has to be applied to new work immediately and to old work carefully.
3The New Standard for New Endpoints
For new endpoints over collections that can grow large, I would make three rules the default. Paging is keyset-based: the response carries a cursor for the next page and a hasNext flag, and the server builds it with Spring Data's Window and ScrollPosition or an equivalent where clause. Sorting is restricted to a few named keys, each backed by an index and ending with a unique tie-breaker such as id. And totals are not exact by default: either no total, or an approximate one such as more than 10,000, or a cached count refreshed every few minutes.
Small, bounded collections, such as the addresses of one customer, can still use simple paging. The rule is about collections whose size grows with the business.
4Removing or Approximating Totals
Totals deserve their own decision because they are often the cheapest win. Many screens show a total that nobody acts on. Options, in increasing cost: drop it; show whether there are more results; show a capped count, computed by counting at most 10,001 rows and displaying 10,000+; show a cached count per filter, refreshed periodically; or use the database's estimate, such as the row estimate PostgreSQL keeps in its statistics, for unfiltered lists. Exact counts remain for the few cases where the business truly needs them, such as a reconciliation report, and those can run asynchronously.
5Migrating Existing Endpoints in Order of Cost
Not every old endpoint needs to change. I would rank them by their actual database cost, using the slow query log or pg_stat_statements joined to endpoints through tracing: total time per day, not just the slowest single call. Typically a handful of endpoints cause most of the load, so the first few migrations bring most of the benefit.
For each endpoint, the migration is additive. Ship a new version with cursor paging and restricted sorts next to the old one, move our own clients first, announce a deprecation date to external clients with examples, measure usage of the old version and switch it off when usage reaches zero or the date arrives. In the meantime, protect the old endpoint: cap page size and page depth, remove the count where clients do not use it, and add indexes for the sorts that are really used.
- Rank endpoints by database timeSlow query log plus tracing
- Add guardrails to the old versionMax size, max depth, needed indexes
- Ship v2 with cursor pagingRestricted sort keys, no exact total
- Move internal clientsOur own apps first
- Deprecate v1 with a dateHeaders, docs, usage tracking
- Old version still used?YesContact remaining clientsNoRemove v1
6 steps in order. Play them, or step through them one at a time.
Each step is reversible until the old version is removed.
6Planning for Growth in Design Reviews
The last change is a habit. In every design review that adds a query or an endpoint, ask how it behaves at ten and one hundred times today's data. For a list endpoint, that means: what is the deepest page someone can request, which sorts are allowed and are they indexed, is there a count, and what happens when the result has a million rows. Concrete numbers help: if the table grows by two million rows a year, where will it be in five years?
A load test with a scaled-up copy of the data, ten or a hundred times production, is the most convincing check, and it turns a review question into evidence. Teams that do this once usually keep doing it, because it finds problems while they are still cheap.
7Follow-up questions
What if a client really needs to jump to an arbitrary page?
Keep offset paging for that client, but bound it: a maximum depth, an indexed sort and no exact count. Most real needs to jump are better served by filters, such as a date range or a search, which narrow the data so the remaining pages are shallow.
How do you get external clients to move to the new version?
Make the new version clearly better for them, faster and stable under inserts, give them examples and a test environment, announce the deprecation well in advance with dates, send deprecation headers on the old version, and track usage per client so you can contact the last few directly.
Is keyset pagination always the answer?
For large, growing collections read in order, it is the best default. It does not support jumping to page numbers, and each sort needs a matching index. For small or bounded lists, offset paging is simpler and perfectly fine.
8Conclusion
In this answer, we saw that offset paging, open sorting and exact totals all scale with the data, and that the early choices became contracts that clients depend on. The new standard is keyset pagination, restricted indexed sort keys and approximate or no totals for large collections; existing endpoints are migrated in order of database cost with guardrails, a new version and a deprecation path; and design reviews ask how each query behaves at ten and one hundred times the data.
Open with
The first thing I would say is that these choices were reasonable at the time, but they became contracts. Clients built on page numbers, totals and sorting by any column, so changing them now breaks someone, and that is why it has to be handled as a standards change plus a migration, not just a query fix.
Then, if they want more
For new endpoints over large, growing collections, I would make keyset pagination the standard: the response returns a cursor and hasNext, built with Window and ScrollPosition, with a few named sort keys, each indexed and ending in id as a tie-breaker.
Exact totals go away by default; we show nothing, a capped count like ten thousand plus, or a cached or estimated number, and keep exact counts for the few business reports that need them, ideally run asynchronously.
For existing endpoints, I would rank them by actual database time from the slow query log and tracing, because a handful usually cause most of the load. For each one, I first add guardrails to the old version, capped size and depth, indexes for the sorts people really use, and no count where nobody uses it.
Then I ship a new version with cursor paging, move our own clients first, and deprecate the old one with dates, headers and usage tracking. Finally, I would build this into design reviews: for every new query, ask how it behaves at ten and a hundred times today's data, and back that with load tests on scaled-up data.
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 Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "Your data volumes grew a hundredfold over eight years, and paging and sorting choices...": https://scrivio-interview-library.netlify.app/databases/spring-data-jpa/89a0b53eba/