Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Ten years ago your platform's public API exposed offset pagination with exact total counts on every list endpoint. Now the counts dominate database load. How do you reason about the cost of that early decision and what do you do now?

Exact counts on large filtered collections require scanning every matching row, so their cost grows with the data while the API contract stays the same; the early decision promised something that only got more expensive. The lesson is that public contracts are expensive to reverse, so promise less at the start. Now we mitigate with caching, approximate or capped counts and cursor endpoints for new clients, plan a deprecation of exact counts based on data about who uses them, and record the decision so future reviews weigh such costs.

Why this question is asked

Ten years ago, returning totalCount on every list probably cost a few milliseconds and made a nice page-number widget possible. Today the data is a thousand times larger, and those counts may account for most of the database's work, yet every partner integration may depend on the field. Interviewers ask this leadership question to see whether a candidate can reason about how a small API decision ages, and then act responsibly when the contract cannot simply be changed. In this answer, we will see why counts get more expensive, what the episode teaches, how to reduce the load now, how to deprecate safely, and how to record the lesson.

1Why Exact Counts Grow Expensive

An exact count must find every row that matches the filter. In PostgreSQL, SELECT count(*) cannot read a stored row count, because of multi-version concurrency control (MVCC): different transactions may see different rows, so the database has to visit the rows or index entries and check which are visible. On a table of 10,000 rows that is nothing. On 500 million rows with a filter like status = 'OPEN' AND region = 'EU', it can be a scan of millions of entries for every single page request, often more work than fetching the page itself.

The important point is that the contract did not change while the cost did. The API still says every list returns totalCount. What changed is the data underneath, and contracts do not scale down with it.

Same contract, growing cost
YearRows in ordersTypical count queryShare of DB time
Year 1200 thousand5 mslow
Year 540 million400 msnoticeable
Year 10500 millionseveral secondsdominant
Illustrative numbers: the field never changed, the work behind it did.

2What the History Teaches

Two lessons come out of this. The first is that decisions in a public API contract are expensive to reverse, because we do not control the clients. A field we add today may be read by integrations nobody remembers in ten years. The second is that we should promise less early: cursors instead of page numbers, totals as optional and possibly approximate. A promise that is cheap today but scales with data is a debt that grows on its own.

It is worth saying that the original decision was not foolish. With the data and clients of the time, it was reasonable. The mistake was not having a way to weigh its long-term cost, which is what we will fix at the end.

3Reducing the Load Now

We cannot remove the field overnight, so we first make it cheaper while keeping the contract. The options can be combined, and we measure each against the database load.

SQL
-- 1. Capped count: stop counting after 10,000 and report 10000 as a floor
SELECT count(*) FROM (
SELECT 1 FROM orders WHERE status = 'OPEN' AND region = 'EU' LIMIT 10001
) t; -- 10001 means 'more than 10,000'
-- 2. Approximate count for an unfiltered table from planner statistics
SELECT reltuples::bigint FROM pg_class WHERE relname = 'orders'; -- about 498000000
// 3. Cached count per filter, refreshed at most once a minute
@Cacheable(cacheNames = "orderCounts", key = "#filter.cacheKey()")
public long countOrders(OrderFilter filter) { return repository.count(filter.toSpec()); }

Caching keeps the field exact enough for most uses while cutting repeated work; a count that is a minute old is fine for a page widget. Capping the countable range bounds the worst case. Approximate counts are cheap but must be documented if they replace exact ones. In parallel, we introduce cursor-based list endpoints without totals and steer new clients and our own SDKs to them, so the number of callers that depend on counts stops growing.

4Planning the Deprecation With Data

Before we can retire exact counts, we need to know who relies on them. The server cannot see whether a client reads a field, so we gather evidence in other ways: our SDKs can report usage, partner support teams can ask the largest integrators, and we can add an opt-out parameter such as includeTotal=false and watch which clients adopt it. Clients that never page beyond the first screen probably do not need the total at all.

With that data, we publish a plan: exact counts become opt-in (includeTotal=true) for a new API version or for new clients, existing clients get a long support window with Deprecation and Sunset signals, and the heaviest callers get direct help. The commercial impact is discussed with product and partner managers, because for some partners the total is a visible feature.

Step throughRetiring exact counts
  1. Measure count load per endpoint and filterfrom database and gateway metrics
  2. Mitigate under the same contractcache, cap, approximate
  3. Offer cursor endpoints without totalsSDKs and new clients use them
  4. Find who depends on countsSDK telemetry, partner contact, opt-out adoption
  5. Make counts opt-in and announce the sunsetDeprecation and Sunset headers
  6. Record the decision and its costdecision record for future reviews

6 steps in order. Play them, or step through them one at a time.

5Recording the Decision

Finally, we write down what happened in an architecture decision record: what was promised, why it made sense then, what it costs now in database load and engineering time, and what we decided. We add a question to the API review checklist: does this field or behaviour get more expensive as data grows, and could we withdraw it? The point is not blame. It is that the next team designing a public contract weighs ten-year costs as well as today's convenience.

6Follow-up questions

Why not just remove the totalCount field?

Because removing a field from a public API is a breaking change, and some clients would fail or display nonsense. We do not know which, so removing it without data and notice would damage trust with partners. Mitigation first, then a measured and announced deprecation, is slower but safe.

Is returning a cached or capped count a breaking change?

It changes the meaning of the field slightly, so we treat it carefully. A count that is up to a minute old is generally acceptable for display. A capped count that returns 10000 for anything larger should be signalled, for example with an extra field such as totalCountCapped: true, and documented.

How do you prevent the same mistake in new APIs?

The pagination standard makes cursors the default and totals optional and possibly approximate, and the API review asks explicitly about costs that grow with data.

7Conclusion

In this answer, we saw that exact counts cost little at first and grow with the data while the public contract stays fixed. The lesson is to promise less in public contracts. Today we reduce the load with caching, capped and approximate counts and cursor endpoints, gather data on who depends on counts, deprecate them with a long window and clear signals, and record the decision so future API reviews weigh long-term costs.

What to say in the interviewAbout 80 seconds aloud

Open with

The core of it is that an exact count has to visit every matching row, and in PostgreSQL there is no stored count because of MVCC, so the cost grows with the data while the contract stays the same.

Then, if they want more

Ten years ago it was a few milliseconds; now it dominates database load. The lesson I take is that public contracts are very expensive to reverse, so early on we should promise less: cursors, and totals that are optional or approximate. For now I would act in two tracks.

First, reduce the load without breaking anyone: cache counts per filter for a short time, cap the countable range with a count over a limited subquery, use planner estimates where an approximate number is acceptable, and add cursor endpoints without totals that new clients and our SDKs use. Second, plan the deprecation with data.

The server cannot see who reads the field, so I would use SDK telemetry, talk to the biggest partners, and offer an opt-out to see who does not need it. Then exact counts become opt-in, with Deprecation and Sunset headers and a long window.

Finally I would record the decision and its cost so future API reviews ask whether a promise gets more expensive as data grows.

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.
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 "Ten years ago your platform's public API exposed offset pagination with exact total...": https://scrivio-interview-library.netlify.app/spring-boot/rest-api-design/dc65513b74/