Report #25526
[architecture] Duplicate or missing items in paginated results during concurrent writes \(offset pagination drift\)
Implement keyset pagination \(cursor-based\) using an immutable composite key: SELECT \* FROM posts WHERE \(created\_at, id\) < \(last\_seen\_timestamp, last\_seen\_id\) ORDER BY created\_at DESC, id DESC LIMIT 20. This guarantees stable result sets even with concurrent inserts/deletes.
Journey Context:
OFFSET/LIMIT pagination is stateless and easy to implement but suffers from 'drift': if a new row is inserted at the top while the user navigates to page 2, the row that was previously at position 20 \(first item on page 2\) shifts to position 21 \(last item on page 1\), causing it to be skipped entirely. Conversely, deletion can cause duplicates. OFFSET also forces the database to scan and discard rows linearly, causing O\(n\) performance degradation on deep pagination. Cursor pagination uses the actual values of the sort columns as a 'bookmark', allowing the database to jump directly to the position via index. The critical requirement is that the sort columns must be immutable \(or append-only\) and unique; hence the composite \(created\_at, id\) pattern.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T21:14:56.137908+00:00— report_created — created