Quick revision
The short answer
A global ordered page needs scatter-gather: the coordinator asks each of the 16 shards for its top rows by the sort key and merges them with a k-way merge. With OFFSET, page p needs (p + 1) x 50 rows from every shard, because any shard may hold all the next rows, so cost grows with depth times shard count. With a keyset cursor, each shard runs WHERE (created_at, id) < (:t, :id) ORDER BY created_at DESC, id DESC LIMIT 50, the coordinator returns the top 50 of the merge, and the cursor holds only the last returned sort values; parallel queries help latency, but the slowest shard sets the page time and an unavailable shard needs a defined contract, and heavy use justifies a global time-ordered read model instead.
What to remember
In this answer, we saw that a globally ordered page over 16 shards needs scatter-gather with a k-way merge, that OFFSET makes each shard return (p + 1) x 50 rows so cost grows with depth times shard count, and that a keyset cursor holding only the last returned (created_at, id) keeps every page at 50 rows per shard. Parallel queries help latency but the slowest shard sets the page time, a missing shard needs an explicit contract so the cursor never skips its rows, and heavy use justifies a global read model fed by CDC.
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.
A global ordered page needs scatter-gather: the coordinator asks each of the 16 shards for its top rows by the sort key and merges them with a k-way merge. With OFFSET, page p needs (p + 1) x 50 rows from every shard, because any shard may hold all the next rows, so cost grows with depth times shard count. With a keyset cursor, each shard runs WHERE (created_at, id) < (:t, :id) ORDER BY created_at DESC, id DESC LIMIT 50, the coordinator returns the top 50 of the merge, and the cursor holds only the last returned sort values; parallel queries help latency, but the slowest shard sets the page time and an unavailable shard needs a defined contract, and heavy use justifies a global time-ordered read model instead.
Why this question is asked
Sharding by customer id makes the main workload, one customer's orders, fast and local to one shard. The admin view asks the opposite question: all orders, newest first, regardless of customer. Every page now needs data from every shard, and the way we paginate decides whether that stays cheap or explodes with depth. Interviewers ask this to see whether we can reason about distributed queries quantitatively. In this answer, we will recap the setup, build scatter-gather with a k-way merge, see why OFFSET gets expensive, design the keyset cursor, deal with slow and missing shards, and consider a read model.
1A Quick Recap of the Setup
Orders are sharded by customer id across 16 databases, so the orders of any one customer live on one shard, while orders created at the same moment are spread across all 16. Each shard has an index on (created_at, id), so each shard can return its own newest orders quickly. The admin page wants the newest 50 orders globally, then the next 50, and so on.
No single shard knows the global order. The newest 50 orders might be spread evenly, or all 50 might be on shard 9 because one big customer placed a burst of orders. So every page must consult every shard.
2Scatter-Gather With a K-Way Merge
Scatter-gather means the coordinator, the service behind the admin API, sends the same query to every shard (scatter) and combines the answers (gather). Each shard returns its 50 newest rows in order. Since the global top 50 can include at most 50 rows from any one shard, those 16 lists of 50 are guaranteed to contain the answer.
This is a k-way merge of 16 sorted lists; with only 800 rows, a priority queue or even a plain sort is fast. The 750 rows not returned are discarded, and this waste is the price of not knowing where the next rows live.
3Why OFFSET Gets Expensive
With offset pagination, page p (counting from 0) is OFFSET p x 50 LIMIT 50 in the global order. The coordinator cannot push the offset down to a shard, because it does not know how many of the skipped rows came from each shard. Any shard might hold all of the first p x 50 + 50 rows, so each shard must return its top (p + 1) x 50 rows, and the coordinator merges them all and throws away all but 50.
| Page (from 0) | Rows per shard | Rows read in total | Rows returned |
|---|---|---|---|
| 0 | 50 | 800 | 50 |
| 10 | 550 | 8,800 | 50 |
| 100 | 5,050 | 80,800 | 50 |
| Keyset, any depth | 50 | 800 | 50 |
The cost grows with depth times shard count. Page 100 reads 5,050 rows from each of 16 shards, 80,800 rows, to return 50, and each shard also does the work of skipping through its index. Deep pages get slower linearly, and a few people paging deep can load every shard at once.
4Designing the Keyset Cursor
With a keyset cursor, the position is the sort values of the last row returned, here (created_at, id) of the 50th order. The next page asks every shard for rows strictly older than that position. Each shard can again return at most 50 relevant rows, so every page costs the same as the first.
Notice that the cursor holds only the last returned sort values, not per-shard positions, because the global position is enough to compute every shard's next rows. The id must be globally unique across shards, for example a Snowflake-style id, so the tie-breaker is well defined when two orders share a timestamp. And we sign the cursor so clients cannot edit it.
5Slow Shards and Missing Shards
Querying shards in parallel makes page latency roughly the latency of the slowest shard, not the sum, but that is still a weakness: one shard in the middle of a backup or a vacuum slows every page. So each shard call gets a timeout, and we monitor per-shard latency for the admin queries.
An unavailable shard is a harder question, and the API contract must answer it. Returning a page without that shard's rows is dangerous with a keyset cursor: the cursor would move past the missing shard's rows, and they would never appear on later pages. So either the request fails with a clear retryable error, which suits an admin tool, or the response is marked partial and carries no advancing cursor, or the cursor records per-shard positions so the missing shard can be resumed later. Whichever we choose, we write it down.
6When to Build a Global Read Model Instead
If this view is used heavily, or needs filters and sorts beyond creation time, fanning out to 16 shards on every request stops making sense. We maintain a global, time-ordered index instead: CDC from all shards feeds a search engine such as OpenSearch, or an analytical store, holding all orders sorted and filterable. The admin API queries one system, at the cost of slight staleness, typically seconds, and of running the pipeline. For an internal admin view, that trade is usually a good one.
7Follow-up questions
Why can't the coordinator ask each shard for only 50 / 16, about 4 rows?
Because the newest 50 orders are not necessarily spread evenly. A single large customer can place all of the last 50 orders, and they would all be on one shard. Asking each shard for 4 rows would then return the wrong page. Each shard must return up to the full page size, unless we keep statistics that prove a bound, which is rarely worth it.
How do we support jumping to page 500 directly?
We generally do not, because random access to deep pages is exactly what makes offsets expensive. Instead, the admin view offers filters, such as a date range or a customer, which become efficient keyset starting points. Jumping to 'orders from 3 March' is a cursor starting at that timestamp.
Could each shard keep its own cursor position to save work?
Per-shard positions in the cursor do not reduce the 50 rows each shard returns, but they help with partial results, because a missing shard can be resumed from exactly where it was. The cost is a larger cursor that grows with the number of shards.
8Conclusion
In this answer, we saw that a globally ordered page over 16 shards needs scatter-gather with a k-way merge, that OFFSET makes each shard return (p + 1) x 50 rows so cost grows with depth times shard count, and that a keyset cursor holding only the last returned (created_at, id) keeps every page at 50 rows per shard. Parallel queries help latency but the slowest shard sets the page time, a missing shard needs an explicit contract so the cursor never skips its rows, and heavy use justifies a global read model fed by CDC.
Open with
Since orders are sharded by customer, no shard knows the global order, so every page is a scatter-gather. The coordinator sends the same query to all 16 shards, each returns its newest rows by created_at and id, and the coordinator does a k-way merge, for example with a priority queue, and returns the top 50.
Then, if they want more
Each shard must return up to 50 rows, because all of the next 50 could be on one shard.
With OFFSET it gets expensive fast: page p needs the top (p + 1) times 50 rows from every shard, since the offset cannot be pushed down, so page 100 reads about 80,000 rows to return 50, and cost grows with depth times shard count.
With a keyset cursor, each shard runs WHERE (created_at, id) < (:t, :id) ORDER BY created_at DESC, id DESC LIMIT 50, the coordinator merges and returns the top 50, and the cursor holds only the last returned sort values, signed and opaque, with a globally unique id as the tie-breaker.
I query shards in parallel with timeouts, but the slowest shard still sets the page time. If a shard is down, I would rather fail the page with a retryable error, because a partial page would move the cursor past that shard's rows forever.
And if the view is used heavily, I would build a global time-ordered read model, for example in OpenSearch fed by CDC, instead of fanning out on every request.
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 "How does pagination work across shards, why do offsets get expensive, and how would you...": https://scrivio-interview-library.netlify.app/architecture/api-design-at-scale/3d784cfc16/