Pattern: Feeds and cursor pagination
Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.
For agents
- Offer as a suggestion. Example values must be tuned.
- Core ideas: the cursor says “continue after this item” instead of “skip N rows”, one item’s event never rebuilds the list, and counts and lists pass through the same filter.
- Related: photo and video ratios, rate limits.
1. Problem
Feeds that use OFFSET get slower with every page, repeat or skip items when new content arrives, and rebuild the whole list when one item changes. Many simultaneous players or timers drain phones.
2. Options
| Method | Upside | Downside |
|---|---|---|
Page number plus OFFSET |
Simple; jump to page N | PostgreSQL still computes the skipped rows, so large offsets are inefficient (its manual says so); items shift when data changes |
| Cursor (keyset) pagination (recommended) | Constant cost per page; stable under inserts | No jump to page N; needs a stable sort key |
| Opaque page token | The server can change the cursor format freely; Google’s API guidelines describe page tokens | Slightly more code |
3. Recommended design
- Cursor, not
OFFSET. Sort by a stable, unique key (for example time plus id) and ask for “items after this cursor”. Return anexttoken that the client treats as opaque, as in AIP-158 (Google API Improvement Proposals on pagination). Reason: the cost per page stays flat and new items do not shift pages. - Page size limit: the server enforces a maximum (example: 50) regardless of what the client asks. OWASP API Security lists unrestricted resource consumption as a risk.
- Load ahead: start the next request when the user is a few items from the end (example: 5 items), not at the end.
- One item’s event never rebuilds the list: update that item in place (like, comment count, deletion) and leave the others untouched.
- Bounded resources: limit the number of mounted rows, active players and timers (example: only the visible item plays; at most one video at a time; recycle off-screen views).
- Same filter for numbers and lists: the count shown (for example “12 comments”) and the list use the same filter (blocked users, hidden items), otherwise the numbers lie.
- Stable layout: reserve each card’s height before content loads (see media ratios and web.dev on layout shift).
4. Scenarios and edge cases
- New item arrives while scrolling: the cursor continues after the last seen item; the new item appears on the next refresh at the top.
- Item deleted between pages: the cursor is a position by key, so nothing is skipped.
- Same timestamp for many items: the unique id in the sort key breaks the tie.
- Client requests a huge page size: the server caps it silently.
- Offline return: keep the cursor with the cached list so the user continues from the same place.
5. Common mistakes
OFFSETon large tables.- Sorting by a non-unique column only.
- Rebuilding the whole list on a single like.
- Many autoplaying videos.
- Count and list built from different filters.
- No server-side maximum page size.
6. How to audit
- What: pagination uses a cursor.
- How to check: read the endpoint and query plan.
- Evidence: the query without
OFFSET; plan output.
- What: page cost stays flat.
- How to check: fetch page 1 and page 500 with a large test dataset; time both.
- Evidence: both timings.
- What: an item event updates only that item.
- How to check: like an item and watch rendering.
- Evidence: profiler or log showing one row updated.
- What: page size is capped.
- How to check: request 10,000 items.
- Evidence: response size equals the cap.
Sources
- PostgreSQL: LIMIT and OFFSET — PostgreSQL Global Development Group
- AIP-158: Pagination — Google API Improvement Proposals
- OWASP API Security Top 10 – 2023 — OWASP
- Cumulative Layout Shift (CLS) — web.dev (Google)