Skip to content

Pagination

Introduction

Do not return all the data at once; return one page at a time and tell the caller how to fetch the next page.

Why use it

  • Protect the database and the network: fetch only a few dozen rows each time.
  • Faster response: users see the first page without waiting for everything.
  • Abuse prevention: cap page size, or a caller can still send pageSize=1000000 and take you down.

Two approaches compared

Offset pagination Cursor pagination
Request ?page=3&pageSize=20 ?after=<marker of last item on previous page>&limit=20
Jump to a page? Yes, jump straight to page 50 No, only move forward
Deep pages Slower and slower (DB must count past all prior rows) Stays fast (index seek)
Inserts while paging May duplicate or skip a row Stable, no duplicates or gaps
Best for Admin tables with modest data Feeds, timelines, large datasets, public APIs

How to do it

  1. Use offset for admin UIs (simple and intuitive); use cursors for feeds and large tables.
  2. Always use a stable sort, e.g. ORDER BY created_at, id. Unstable ordering makes pages jump around.
  3. A cursor is usually “the sort-key values of the last item on the previous page”, often encoded as an opaque string so you can change the implementation later.
  4. Set a default page size and a max page size (e.g. default 20, max 100).
  5. Tell the caller whether there is a next page, and provide the next cursor.
  6. Cursor pagination needs an index on the sort columns; see Indexes.

Component comparison

Offset pagination (page / size)

Built into the frameworkOpsLow

Request which page and how many items per page; the database skips the preceding rows and returns one page. The most intuitive way to paginate.

Best forAdmin tables and lists with modest data volume.

Avoid whenTables with millions of rows, feeds, and timelines that keep receiving new data.

Pros

Can jump straight to page N, which suits tables with page numbers

Easy to implement with EF Core Skip/Take or SQL OFFSET/LIMIT

Easy for both frontend and backend to understand

Cons

Slows down on deep pages because the database still has to count past all preceding rows

If data is added or removed between page turns, an item may repeat or be skipped

Requires a stable sort order, otherwise page results get shuffled

Cursor / keyset pagination

Hand-rolledOpen sourceOpsLow

No page numbers; instead remember the sort value of the last item on the previous page and continue from just after it, like a bookmark.

Best forFeeds, timelines, large tables, and public list endpoints.

Avoid whenThe product requires "jump to page N" or page number navigation.

Pros

Deep pages stay just as fast, because the index jumps straight to the row

Rows inserted while paging do not cause duplicates or gaps

A good fit for a feed that keeps scrolling downward

Cons

You cannot jump to a chosen page, only to the previous or the next

The sort column needs an index, plus a unique column as a tiebreaker

Except for DRF you write it yourself, and cursors with several sort columns are easy to get wrong

Page numbers + total count

Built into the frameworkOpsLow

On top of offset pagination, also return "how many items and pages in total", so the UI can show "1234 in total" and a page bar.

Best forAdmin lists with small to medium data volume that need to show the total and page numbers.

Avoid whenLarge tables or feeds, where users do not care about the total.

Pros

Complete user experience, with the total and the last page visible

Frontend pagination components are usually designed around this kind of data

Costs only one extra COUNT query over offset pagination

Cons

COUNT(*) is slow on large tables, and running it on every page turn wastes resources

The total and the list come from two queries, so data changing in between makes them disagree

On large tables the total often needs caching or estimation, adding complexity

Checklist

  • Every list endpoint is paginated; none “return everything”
  • Default and maximum page sizes are set
  • Ordering is stable (unique column as a tie-breaker)
  • Large tables and feeds use cursor pagination
  • Sort / cursor columns are indexed
  • Responses tell the caller whether a next page exists