Scrivio Interview library
DebuggingIntermediate7 min read · updated 13 November 2026

A partner exports all of its orders through GET /orders?page=N&size=100, which is served by Elasticsearch. Around page 101 every request fails, and the logs show: Result window is too large, from + size must be less than or equal to: [10000].

Explain the limit and redesign the endpoint for deep paging and exports.

Elasticsearch refuses any request where from + size is above index.max_result_window, 10,000 by default, because every shard has to collect and sort from + size hits and the coordinating node has to merge all of them, so page 101 of 100 items costs far more than page 1. The fix is not to raise the limit but to page with search_after over a point-in-time and a unique tiebreaker sort, hidden behind an opaque cursor, and to serve full exports as an asynchronous bulk job instead of paging a search index.

Background to this question

Offset pagination with page and size parameters is the first thing most of us build, and it works well for the first screens a person looks at. It falls apart when a machine walks through every page, which is exactly what a partner export does. The error names a setting, and the tempting answer is to raise it. In this answer, we will see how a search request is executed across shards, why deep offsets are expensive, how search_after with a point-in-time works, how to expose it as a cursor in our API, and how to handle full exports properly.

1A Quick Recap of How a Search Is Executed

An Elasticsearch index is split into shards, and each shard is a small independent search engine (a Lucene index). When our service sends a search to the cluster, one node acts as the coordinator. It forwards the query to one copy of every shard, each shard finds its best matching documents, and the coordinator merges those partial results into the final page.

Now think about what from=10000 and size=100 means for that process. The coordinator cannot know in advance which shard holds documents 10,001 to 10,100 in the global order. So it asks every shard for its top 10,100 hits, sorted. With 5 shards, the coordinator receives 50,500 sorted entries, merges them, and throws away 50,400 of them to return 100. The work grows with the page number, and it is repeated on every request.

2Why the 10,000 Limit Exists and Why Raising It Is a Trap

The setting index.max_result_window, 10,000 by default, is a guard rail. It stops a single request from asking every shard to build huge priority queues in heap memory. That is the error our partner hits: page 101 with size 100 means from = 10,000, so from + size = 10,100, which is above the limit.

We could raise the setting to one million, and the export would get further. But each deep page would then make every shard sort a million hits, the coordinator would merge millions of entries, and the cost of the last page would be thousands of times the cost of the first. A single export could put pressure on heap and CPU for every other user of the cluster. There is a second problem that is easy to miss: offset paging over a changing index is not stable. If new orders are indexed while the partner pages, items shift between pages, and the export can skip or duplicate records. Raising the limit fixes neither problem; it only moves the cliff further out.

Ways to page through Elasticsearch
ApproachCost of a deep pageConsistent viewGood for
from + sizegrows with the offset, on every shardnofirst few pages of interactive search
scroll (legacy)flat, but holds a search contextyes, while it livesolder code; no longer recommended
search_after + PITflat per pageyes, while the PIT livesdeep paging and API cursors
bulk export jobone streamed passyes, from a snapshotfull exports of all records
search_after over a point-in-time keeps the cost of page 10,000 the same as page 1.

3Paging With search_after and a Point-in-Time

search_after replaces 'skip N documents' with 'continue after this document'. We sort by fields that give a total order, for example created_at and then a unique order id as a tiebreaker, and with each request we pass the sort values of the last hit we returned. Every shard can then jump straight to that position in its sorted order and return only the next 100 hits, so page 10,000 costs the same as page 1.

The tiebreaker matters. If two orders have the same created_at and we sort only by created_at, the position 'after this value' is ambiguous, and orders can be skipped or repeated at the page boundary. Adding a unique field to the sort removes that ambiguity.

The point-in-time (PIT) adds a consistent view. We open a PIT on the index with a keep_alive, for example 5 minutes, and each search uses that PIT id instead of the index name. All pages then see the index as it was when the PIT was opened, so documents indexed or updated during the export do not shift the results. The keep_alive is extended with each request, and we close the PIT at the end.

Java
POST /orders-v3/_pit?keep_alive=5m
// { "id": "46ToAwMDaWR5..." }
POST /_search
{
"size": 100,
"pit": { "id": "46ToAwMDaWR5...", "keep_alive": "5m" },
"query": { "term": { "partner_id": "p-981" } },
"sort": [ { "created_at": "asc" }, { "order_id": "asc" } ],
"search_after": [ 1730419200000, "ord-77812" ]
}
// hits[99].sort = [1730419512000, "ord-77990"]: send these as search_after next time

Notice that the request carries no index name; the PIT already pins the index and its state. Also notice that the response gives us the sort values of the last hit, which are all we need to fetch the next page.

4Hiding the Mechanism Behind an Opaque Cursor

Our partners should not have to know about Elasticsearch, PITs or sort values. So the API returns a cursor: an opaque string that encodes the PIT id, the last sort values and the query parameters, base64-encoded and signed with an HMAC so clients cannot tamper with it. The client calls GET /orders?limit=100, receives the items and a next_cursor, and calls GET /orders?cursor=... until next_cursor is absent.

Page numbers do not disappear entirely. For interactive search, people rarely look past page 5, so we can keep 'page 1, 2, 3' for the first few pages and cap it well below the limit, for example at 50 pages, while machine clients use cursors. The API documentation should say clearly that cursors expire (when the PIT expires) and that an expired cursor returns an error asking the client to start again.

Step throughA partner pages with a cursor
PartnerOrders APIElasticsearch
GET /orders?limit=100
open PIT, keep_alive=5m
search pit + sort, size 100
100 hits, last sort values
items + next_cursor (signed)
GET /orders?cursor=...
search pit + search_after
next 100 hits
items + next_cursor

9 messages in order. Play them, or step through them one at a time.

The cursor carries the PIT id and the last sort values, so each page costs the same.

5Serving Full Exports the Right Way

Even with search_after, an export of millions of orders through 100-item pages means tens of thousands of HTTP round trips, and a search index is not the source of truth; it can lag behind the database by seconds. When the real need is 'give me everything', a better contract is a bulk export: POST /exports returns 202 Accepted with a job id, a background worker reads the data (from a database replica, or from Elasticsearch with search_after if that is the only store) and writes compressed files to object storage such as S3, and the client downloads them from presigned URLs when the job finishes.

This takes the load off the request path, makes the export resumable, and gives the partner one consistent snapshot. Interactive search keeps its page numbers, cursor paging serves incremental API access, and exports become a separate product with their own quotas.

6Follow-up questions

Why is the scroll API no longer the recommended choice for deep pagination?

Scroll was built for this job: it keeps a search context open on every shard and returns batches one after another. It still works, but Elastic now recommends search_after with a point-in-time for deep paging. A scroll context is tied to a single sequence of requests, so it cannot be shared or used to go backwards, and each open scroll holds resources on every shard until it expires, which becomes expensive when thousands of partners hold them. A PIT is lighter, can be used by several searches, and combined with search_after gives the same consistent, flat-cost paging with a cleaner model.

What happens if the PIT expires in the middle of an export?

The next request with that PIT id fails. The API should translate this into a clear error, for example 410 Gone or 400 with a code like cursor_expired, and the client starts again from the beginning or from a time-based filter. Choosing a keep_alive that covers the time between two page requests, not the whole export, avoids most expiries, because each request extends it.

7Conclusion

In this answer, we saw that the 10,000 limit protects the cluster from deep offset queries that make every shard sort from + size hits, and that raising it only moves the problem. We replaced offset paging with search_after over a point-in-time and a unique tiebreaker sort, exposed it as a signed opaque cursor, kept page numbers only for the first pages of interactive search, and moved full exports to an asynchronous job that writes files to object storage.

What to say in the interviewAbout 90 seconds aloud

Open with

The error comes from index.max_result_window, which is 10,000 by default. With from and size paging, every shard has to collect and sort from plus size hits, and the coordinating node merges all of them, so page 101 makes each shard sort over ten thousand documents just to return a hundred.

Then, if they want more

Raising the setting would let the export go further, but deep pages would get slower and slower and could hurt the whole cluster, and offset paging over a changing index also skips or repeats documents. So I would redesign the endpoint.

For deep paging I would use search_after with a point-in-time: I open a PIT, sort by created_at plus a unique order id as a tiebreaker, and each request passes the sort values of the last hit. Every page then costs the same, and the PIT gives a consistent view while it lives.

In the API, I would hide all of that behind an opaque, signed cursor, return next_cursor in each response, and keep page numbers only for the first few pages of interactive search. And for a partner who wants all their orders, I would not page a search index at all.

I would offer an asynchronous export: POST creates a job, a worker reads from the database or a replica and writes compressed files to S3, and the partner downloads them through presigned URLs. That is cheaper for us and more reliable for them.

About this answer

Last updated
13 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 official documentation for the version you use. 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 "Explain the limit and redesign the endpoint for deep paging and exports.": https://scrivio-interview-library.netlify.app/architecture/api-design-at-scale/42dd620bcd/