The reusable issue is an order-of-operations invariant. A backend fetches page size plus one rows in descending sequence order to detect older history, while clients expect each response page in ascending chronological order. The analysis should determine whether trimming happens before or after reversal, identify the cursor boundary that prevents gaps and duplicates, and illustrate the rule on a small consecutive sequence. No runtime evidence is available; this is a reasoning exercise.
Correct overflow trimming for backward cursor pagination with chronological pages
Reasoned result: treat the descending database order as the canonical order for overflow detection and trimming. If page size is three and the raw rows are eight, seven, six, five, the fourth row is only evidence of older history and should be removed from the tail before reversing. The response becomes six, seven, eight. The next exclusive cursor is built from the oldest returned row, six, so the next query selects sequence values below six. Repeating yields three, four, five with cursor three, then one, two with no cursor. Reversing before trimming is safe only if the extra row is then removed from the chronological head; removing the chronological tail would discard the newest valid row and retain the overflow.
The pagination design must treat a client cursor as an untrusted capability constrained by the current request. Use an opaque authenticated token, preferably authenticated encryption because ordinary signed base64 payloads reveal their contents. Bind the token to a domain separated purpose, version, authenticated principal or tenant, a keyed digest of the thread, a keyed digest of canonicalized effective filters, backward direction, an immutable snapshot marker, and the exclusive ordering position. On every paginated request, derive scope from the authenticated route and normalized request filters rather than from cursor claims, authenticate the token before using its contents, compare every binding in constant time, and reject any mismatch with one generic invalid cursor response. Keep the initial snapshot unchanged while advancing only the oldest returned position. A high water mark excludes later messages but does not by itself freeze deletions, edits, or backdated inserts; strict repeatability needs database snapshot or versioned history semantics.