Quick revision
The short answer
An id is assigned when the event is created, but its transaction may commit later, so a reader can see id 1006 committed, move its cursor past it, and never see id 1005 when that commits a moment later; clock differences between workers widen the window because ids are only roughly time-ordered. The simple fix is to serve only events older than a safety lag, such as ids whose timestamp is more than 5 seconds old, longer than any write transaction. The stronger fix assigns a commit-ordered sequence after commit, from the CDC log position or a single writer numbering events from an outbox, and pages by that, with reconciliation per time bucket to detect any remaining gaps.
What to remember
In this answer, we saw that keyset paging by creation id skips events because ids are assigned before commit, so a later id can become visible first and move the cursor past an earlier one, with clock differences between workers widening the window. A safety lag that serves only events older than any transaction is the simple fix; a commit-ordered sequence from the CDC log or a single numbering writer over an outbox is the strong fix; and reconciliation per time bucket with a re-read API detects and repairs anything that remains.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
An id is assigned when the event is created, but its transaction may commit later, so a reader can see id 1006 committed, move its cursor past it, and never see id 1005 when that commits a moment later; clock differences between workers widen the window because ids are only roughly time-ordered. The simple fix is to serve only events older than a safety lag, such as ids whose timestamp is more than 5 seconds old, longer than any write transaction. The stronger fix assigns a commit-ordered sequence after commit, from the CDC log position or a single writer numbering events from an outbox, and pages by that, with reconciliation per time bucket to detect any remaining gaps.
Why this question is asked
Keyset pagination with WHERE id > :lastSeenId is the textbook way to page through a growing table, and it works perfectly for a table that nobody is writing to. For a live feed, it has a subtle hole: the order in which ids are assigned is not the order in which rows become visible. Interviewers ask this because it tests whether we think about concurrency at the level of transactions. In this answer, we will recap keyset paging, reproduce the skip on a timeline, see how Snowflake ids widen it, apply the safety-lag fix, design a commit-ordered sequence, and add gap detection.
1A Quick Recap of Keyset Paging
With keyset pagination, also called cursor pagination, the client remembers the last id it received and asks for the next page with WHERE id > :lastSeenId ORDER BY id LIMIT 500. It is fast at any depth because the database jumps straight to the right place in the index. For a partner syncing our feed, it looks ideal: they poll every few seconds and each time ask for everything after their cursor.
The hidden assumption is that once the partner has seen id 1006, every event with a smaller id is already visible. If any event with a smaller id can appear later, the partner's cursor has already passed it and it will never be served.
2How a Committed Event Gets Skipped
Ids are assigned when the event is created, at the start of the work, but the row only becomes visible to readers when its transaction commits. Two writers working at the same time can therefore commit in the opposite order of their ids.
7 messages in order. Play them, or step through them one at a time.
Notice that every step is individually correct. The reader saw only committed data, and event 1005 is in the database. The problem is that the reader's cursor encodes the belief that nothing below 1006 will ever appear, and transactions broke that belief. With thousands of events per second, a small fraction always falls in such windows, which is how 0.05 percent goes missing.
3Why Snowflake Ids Widen the Window
With Snowflake-style ids the problem gets bigger, because ids from different workers are only roughly ordered by time. If worker 3's clock runs 30 ms behind worker 7's, events created on worker 3 receive ids that are smaller than events created at the same moment on worker 7. So even with instant commits, a partner can receive an event from worker 7 and move its cursor past ids that worker 3 is still about to generate. Commit delays and clock differences add up, and the window in which events can be skipped is their sum.
4The Simple Fix: Serve Only Events Older Than a Safety Lag
If we never serve events newer than a certain age, every event in the served range has had time to commit. Snowflake ids carry their own timestamp, so we can turn 'five seconds ago' into an id bound.
The lag must be longer than any write transaction plus the largest clock difference between workers. That is only safe if it is enforced, so we cap transactions with statement_timeout and idle_in_transaction_session_timeout in PostgreSQL, for example at 3 seconds, and alert on clock offsets above a few milliseconds. The cost is freshness: partners see events 5 seconds late, which is almost always acceptable for a sync feed. The weakness is that the guarantee depends on timeouts and clocks behaving, so it reduces the risk rather than eliminating it.
5The Stronger Fix: Number Events in Commit Order
The root problem is that the id is assigned before commit. So we add a second number, assigned after commit, in commit order, and page by that instead. Two common ways exist. With CDC, the database log already records changes in commit order: a consumer reads committed events from the log and assigns each a feed sequence, or uses the log position itself. With an outbox, writers insert events into an outbox table in their transaction, and a single numbering process reads committed outbox rows and assigns consecutive feed sequence numbers.
- Writer commits event and outbox row togetherevent_id assigned at creation
- Numbering process reads committed rowsfrom the CDC log or the outbox, in commit order
- Assign feed_seq = previous + 1a single writer, so no reordering
- Partners page by feed_seqnothing can appear behind the cursor
4 steps in order. Play them, or step through them one at a time.
Because only one process assigns feed_seq, and it only sees committed events, a smaller feed_seq can never appear after a larger one has been served. The cursor is now safe without any lag. The cost is a component to run, a single numbering writer or a CDC consumer, and a delay of however long that component takes, typically well under a second.
6Detecting Gaps With Reconciliation
Whatever we build, we want evidence that partners have everything. A reconciliation endpoint returns counts per time bucket, for example per minute of event creation, and partners or our own checker compare those counts with what they received. When a bucket differs, the API offers a way to re-read a time range, so the partner can repair its copy without a full resync. This turns a silent loss into a detected and repairable one, and it would have revealed the 0.05 percent long before partners noticed.
7Follow-up questions
Why does the same problem appear with database sequences and auto-increment keys?
Because they have the same shape: a value is taken from the sequence when the row is inserted, but the row becomes visible only at commit, and sequences are deliberately not transactional, so other transactions can take later values and commit first. A transaction holding id 1005 can commit after one holding 1006, exactly as with Snowflake ids. MySQL auto-increment behaves the same way under concurrent inserts. Only a number assigned after commit, in commit order, avoids it.
Could we page by a commit timestamp instead?
PostgreSQL can track commit timestamps, but timestamps tie when several transactions commit in the same instant, and a page boundary in the middle of a tie can skip or repeat events. We would need a tie-breaker and still care about clock behaviour, so a sequence assigned after commit is simpler.
How long should the safety lag be?
Longer than the longest allowed write transaction plus the largest expected clock offset between workers, with margin. If transactions are capped at 3 seconds and clocks are kept within 50 ms, 5 seconds is reasonable, provided the caps are enforced and monitored.
8Conclusion
In this answer, we saw that keyset paging by creation id skips events because ids are assigned before commit, so a later id can become visible first and move the cursor past an earlier one, with clock differences between workers widening the window. A safety lag that serves only events older than any transaction is the simple fix; a commit-ordered sequence from the CDC log or a single numbering writer over an outbox is the strong fix; and reconciliation per time bucket with a re-read API detects and repairs anything that remains.
Open with
The id is assigned when the event is created, but the row is only visible when its transaction commits. So writer A can take id 1005 and still be in its transaction while writer B takes 1006 and commits.
Then, if they want more
A partner polling at that moment gets 1006 and moves its cursor to 1006. When 1005 commits a moment later, the next query asks for ids above 1006, so 1005 is never served, even though it is in the database.
With Snowflake-style ids it is worse, because ids from different workers are only roughly time-ordered, and clock differences add to the window. The quick fix is a safety lag: only serve events whose id timestamp is more than, say, 5 seconds old, longer than any write transaction, and enforce that with transaction timeouts.
Partners get slightly less fresh data, but nothing is skipped. The proper fix is to number events after commit, in commit order, for example from the CDC log position or a single writer that numbers committed outbox rows, and page by that feed sequence instead of the creation id. Then nothing can ever appear behind the cursor.
I would also add reconciliation: counts per time bucket that partners can compare, and an API to re-read a time range.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how events can be skipped, and redesign the feed so a cursor never misses a...": https://scrivio-interview-library.netlify.app/architecture/storage-and-pipelines/695a17e7bb/