Keyset pagination
Definition
Keyset pagination fetches the next page with a condition on the sort key of the last row returned (WHERE id > :last ORDER BY id LIMIT n) instead of OFFSET.
In practice
OFFSET 100000 makes the database read and discard 100,000 rows before returning the page, so deep pages get slower and heavier. With an index on the sort key, keyset pagination seeks straight to the starting point. Results are also stable: rows inserted while a user pages do not shift items between pages.
The trade-off is that users cannot jump to page 500 directly, only to the next or previous page. The sort key must be unique, so add a tie-breaker such as (created_at, id). The cursor is usually encoded and returned to clients as an opaque token so the API can change it later.
Example
Page 5,000 of a feed with OFFSET 100000 LIMIT 20 takes 180 ms; WHERE (created_at, id) < (:ts, :id) ORDER BY created_at DESC, id DESC LIMIT 20 takes 1 ms on every page.