Why Paginate
Returning a huge list in one response is slow and fragile. Pagination slices results into pages so clients fetch them in chunks. The three common styles differ in performance and correctness under change.
Offset Pagination
The client asks for a limit and an offset, such as skip 40 take 20. It is easy but slow deep in the list because the database must count past every skipped row, and inserts can shift items between pages.
Cursor and Keyset
Cursor pagination returns an opaque token pointing to the next page. Keyset pagination filters by the last seen sort value, such as id greater than the last id. Both stay fast at any depth and stay stable when rows are inserted, but they cannot jump to an arbitrary page number.
Choosing
- Use offset for small lists or admin tables that need page jumps.
- Use keyset or cursor for large or fast changing feeds.
Key idea
Offset paging is simple but slow and unstable deep in a list, while keyset and cursor paging stay fast and consistent by anchoring on the last seen row.