Skip to content

API & data design · 2 of 4

Lists that never end

Step 1 · The pattern

GET /orders returns every order

Customers open their order history and the API returns all their orders in one response. Most have a few dozen, so each request takes about 150 ms. Easy to write, easy to use. (All numbers are illustrative.)

  • a list request
  • served

In short

Lists that never end: how it works and how it breaks

GET /orders returning every order is the easiest list endpoint to write, and it works while lists are short. Lists grow, unevenly: most customers have a few dozen rows, one has hundreds of thousands, and their request holds a server, its memory and a database connection for seconds. A few of those at once fill the fleet, and everyone's requests time out. Design every list as paged from day one, since adding paging later breaks clients: the server enforces a maximum page size whatever the client asks for. Prefer a cursor (an opaque token meaning 'after this item') to offsets (?page=400): an offset makes the database read and discard every earlier row, so deep pages get slower, and rows added or removed between requests make items repeat or vanish. A cursor built on an indexed, unique sort key costs the same for page 1 and page 4,000.

What this simulation simplifies

  • The big list costs a fixed three seconds; in practice it grows with the list and can exhaust memory first.
  • Database cost is folded into the API's time.

How this comes up in interviews

"Design the 'list my orders' endpoint for customers with anywhere from 1 to 1 million orders."

Then they ask

  1. Offset or cursor?
  2. What does the cursor contain?
  3. What if a customer needs everything?

A strong answer covers

  • Always paginated, with a server-enforced maximum page size.
  • Cursor (keyset) on an indexed, unique sort key such as (created_at, id).
  • Opaque cursor tokens; no total count by default.
  • Offsets slow down with depth and skip or repeat rows on inserts.
  • Bulk needs go to an async export job.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last reviewed: Official documentation

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.