Pagination
Return a large result in smaller pages, using LIMIT/OFFSET or keyset pagination.
What is it?
An endpoint like GET /products might match thousands of rows. Sending them all at once wastes bandwidth, memory, and rendering time on data almost nobody scrolls to. Pagination returns one smaller page at a time, plus a way to fetch the next.
- LIMIT/OFFSET skips some rows and takes the next batch. It's easy to reason about ("page 3, 20 per page") but slows down the deeper you go, because the database still walks past every skipped row. - Keyset pagination ("everything after id 340") filters on a column's value instead of counting rows, so it stays fast at any depth.
Explain like I'm 10
OFFSET/LIMIT is like opening a phone book at page 1 every time someone asks for page 50: you still count past pages 1 to 49. Keyset pagination is a bookmark, so the next request opens straight to where you left off.
Examples
OFFSET/LIMIT pagination
-- Page 3, 20 rows per page
SELECT * FROM products
ORDER BY id
LIMIT 20 OFFSET 40;Skips the first 40 rows (pages 1 and 2), then returns the next 20: page 3.
Keyset pagination
SELECT * FROM products
WHERE id > 340
ORDER BY id
LIMIT 20;Instead of counting past rows with OFFSET, this jumps to rows after the last id on the previous page (340). With an index on id, the database finds that point instantly, however deep you are.
How it works
With OFFSET/LIMIT, the database still generates (or at least counts through) every row up to the offset, then throws most of them away. The deeper the page, the more wasted work.
Keyset pagination uses a WHERE condition on an indexed, ordered column (like id or created_at) to jump straight to the starting point. Page 5,000 costs about the same as page 1.
Why does it exist?
Most of a large result is never looked at. Pagination saves list endpoints and UIs from loading, sending, and rendering every matching row at once, which wastes work in the database, the network, and the browser.
When to use it
Use OFFSET/LIMIT for small to moderate results, or when users need to jump to any page number. Use keyset pagination for large, fast-growing tables, infinite-scroll feeds, or any API where deep-page speed matters.
When not to use it
If the result easily fits in one response (a settings list, a few categories), skip pagination and return everything.
Common mistakes
Paginating without ORDER BY, so rows shift between pages (appearing twice or never) when data changes between requests.
Using large OFFSETs on a growing table and expecting steady speed. OFFSET's cost grows with page depth.
Using keyset pagination on a column that isn't unique or indexed. It can be as slow as OFFSET, or skip/duplicate rows when values tie.
Practice exercises
- Easy:
Write an OFFSET/LIMIT query for page 2 of a products table, 10 rows per page.
- Medium:
Convert that query to keyset pagination, given the last product id on page 1.
- Hard:
Explain why OFFSET 100000 LIMIT 20 is slow on a large table, and why a keyset query at the same depth isn't.
Interview questions
What's the difference between OFFSET/LIMIT and keyset pagination?
OFFSET/LIMIT skips a number of rows before returning a page, so it slows down at deeper pages. Keyset pagination filters on an indexed column's value and stays fast at any depth.
Why does pagination need a stable ORDER BY?
Without it, row order isn't guaranteed between requests, so pages can show duplicate or missing rows if the data or query plan changes.
Why is deep OFFSET pagination slow on large tables?
The database still walks through (or counts) every skipped row before returning the page, even though none of them are returned.