Report #57844
[architecture] Offset pagination fails under concurrent writes and scales poorly on large datasets \(LIMIT/OFFSET\)
Implement Keyset Pagination \(seek method\) using the last seen tuple of indexed columns \(e.g., \`WHERE \(created\_at, id\) > \(?, ?\)\`\) with a composite index, returning the next page plus a cursor for the client.
Journey Context:
Standard OFFSET pagination has O\(n\) complexity—fetching page 10,000 requires scanning and discarding 10,000 rows, causing query time to linearly increase with page depth. Worse, under concurrent writes \(insertions/deletions\), offset pagination produces 'torn pages' where items shift between pages, causing users to see duplicates or miss records when navigating. Many developers implement cursor pagination but use only a timestamp, which fails when multiple rows share the same timestamp \(non-unique cursors\). The robust fix uses a composite key—typically \`\(created\_at, id\)\`—where \`id\` acts as a tie-breaker, combined with a query like \`WHERE \(created\_at, id\) > \(last\_seen\_at, last\_seen\_id\)\`. This leverages the index for O\(log n\) seeks, handles concurrent writes gracefully \(new inserts don't shift existing record positions\), and supports infinite scroll efficiently.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T03:34:51.450011+00:00— report_created — created