Pagination for very large lists
40 min · Intermediate
Pagination means returning a long list in small pieces, called pages, instead of all at once. The two common styles are offset pagination ("skip 40, take 20") and cursor pagination ("take 20 after the last item I saw"), and at scale the difference matters a lot.
Topics in this lesson
- Why returning all rows fails
- Offset/limit pagination and deep pages
- Cursor (keyset) pagination
- Simulation: the item shifted between pages
- Response shape: items, nextCursor, hasMore
Key points
- Never return an unbounded list: every list endpoint returns a page and a way to get the next one.
- Offset pagination is simple and allows jumping to a page, but deep pages are slow and rows can repeat or be skipped when data changes.
- Cursor (keyset) pagination uses WHERE id > lastId with an index: every page is equally fast and stable under inserts.
- Return items, an opaque nextCursor and hasMore; default and cap the page size on the server.
Interview questions this lesson answers
- How would you paginate a table with a billion rows?
- What are the pros and cons of offset pagination?
- Why can items repeat when scrolling with offset pages?
- What should a cursor-paginated response contain?
- Why should the server cap the page size?
- How do you do keyset pagination when sorting by a column that is not unique, like a timestamp?
Sign in to continue this lesson
Create an account to read every lesson, practise with the exercises and keep track of your progress.