On an online auction's lot page, bid events arrive duplicated, out of order or with a sequence number missing; how should the browser client handle each case?
answer
- per-lot sequence, last applied
- at or below last: already have it
- next one: apply, then drain
- ahead: hold briefly, never paint
- gap outlives the hold: read state
basics
~20 sTrack the last applied sequence per lot: drop events at or below it, apply the next one and drain held successors, hold later ones briefly, and if the gap persists, resync from a state read.
solid answer
~40 sEvery bid event carries its lot id and a **per-lot sequence number** the server assigns, and the client keeps `lastSeq` per lot. An event with `seq <= lastSeq` is a **duplicate** (a reconnect overlap or a resend) and is dropped. `seq == lastSeq + 1` is applied, then any held successors are drained in order. An event further ahead means a **gap**: hold it in a small per-lot buffer for a second or so, never painting out of order. If the missing event arrives, drain; if not, **read the lot's current state** with its sequence number, render it, discard held events it covers and continue. My own accepted bid is matched by the sequence number in the POST response, so it never paints twice.
code
pseudocode · 26 linesonEvent(e):
s = lots[e.lotId]
if e.seq <= s.lastSeq: return // duplicate or already covered
s.held.put(e.seq, e)
drain(s)
if not s.held.isEmpty() and not s.gapTimer:
s.gapTimer = after(1500ms, () => resync(e.lotId))
if s.held.size() > 50: resync(e.lotId)
drain(s):
while s.held.has(s.lastSeq + 1):
next = s.held.take(s.lastSeq + 1)
apply(next); s.lastSeq = next.seq
if s.held.isEmpty(): cancel(s.gapTimer); s.gapTimer = null
resync(lotId):
s = lots[lotId]
if s.resyncing: return
s.resyncing = true; cancel(s.gapTimer); s.gapTimer = null
state = GET /lots/{lotId}/state // price, top bids, endTime, seq
render(lotId, state); s.lastSeq = state.seq
s.held.removeWhere(seq <= state.seq)
s.resyncing = false
drain(s)
if not s.held.isEmpty() and not s.gapTimer:
s.gapTimer = after(1500ms, () => resync(lotId))go deeper
Recall that events over a live connection can repeat, arrive late or go missing, and that a server-assigned sequence number lets the page tell which is which.
Explain the four cases against the last applied sequence number: drop, apply and drain, hold, and resync through a state read.
Bound the hold by time and size, guard against overlapping resyncs during a burst, match your own bid by its sequence number, and explain why a max-price shortcut breaks the ladder.
Decide whether the client resyncs through replay or a state read per topic, and make sequence numbers in every event and every read a contract all live features share.
## Why the client cannot just paint what arrives A live connection usually delivers a lot's bids in order, once each. "Usually" is the problem: a reconnect replays events the client already applied, a server fails over between two delivery paths and one overtakes the other, and an event lost in a hand-over leaves a hole. On an auction's **bid ladder** (the ordered list of bids with the leading one on top) each of these is visible: a duplicate shows one bid twice, a reordering shows a lower bid above a higher one, and a gap silently hides a bid that changed who is winning. The fix is a number the server assigns: a **per-lot sequence number**, increasing by one for each event on that lot. Why the server numbers events rather than trusting timestamps is a server design question; on the client the rule is simply that **the sequence number, not arrival order, decides**. ## The four cases | Event's `seq` vs `lastSeq` | Meaning | Client action | |---|---|---| | `seq <= lastSeq` | duplicate, or already covered by a state read | drop silently | | `seq == lastSeq + 1` | the next event | apply, set `lastSeq`, drain held successors | | `seq > lastSeq + 1` | a gap, or the missing event is late | hold; start a short gap timer | | gap timer fires, hole still there | the event is lost | read the lot's state and resume from it | ### Duplicates Dropping by sequence number is exact. Comparing amount and bidder is not: two real events can share them (a bid and the retraction that names it), and a resend always does. ### Out of order and gaps An event that arrives ahead of its predecessor is **held**, not painted. Most reorderings resolve quickly when the earlier event arrives, and the drain applies both in order. The hold is bounded twice: - by **time**: a second or two, short against a lot's closing window; - by **size**: if dozens of events pile up behind one hole during a closing burst, waiting longer helps nobody. When either bound trips, the client stops waiting and does a **catch-up read**: one ordinary request for the lot's current state (price, top bids, end time) and the sequence number it reflects. It renders that, sets `lastSeq` to the read's number, discards held events at or below it and drains the rest. One flag stops a burst from triggering several reads at once. ## Your own bid The bidder's own bid arrives twice: once as the HTTP response to their POST and once as an event on the lot topic. The response carries the **sequence number** the server gave the bid. The client can either apply it from the response (then the event is a duplicate by number) or simply show "your bid is being placed" until the event with that number arrives. What it must not do is paint both. ## What the bidder sees during a resync A resync should be close to invisible. The ladder keeps its last confirmed state and a small "updating" marker appears beside it; held events are **not** painted while the read is in flight, so the bidder never sees a ladder that is briefly in the wrong order. If the read takes longer than a second or two, the price gets the same "may be out of date" treatment as a reconnect, because from the bidder's seat the two situations are the same: the screen is not yet known to be true. ## What this is not - **Not "highest price wins."** It is tempting to keep the maximum price and skip sequencing, because prices mostly rise. But the ladder is a list, not a number; an auction that allows a cancelled or retracted bid can lower the price; and a closing-time extension is an event of its own that a max function never sees. - **Not a refetch on every oddity.** At 50,000 viewers a reordering is common during a burst; reading the lot every time would turn every hiccup into fifty thousand reads. ## What it costs The server must number events per lot and include the number in every state read; the client keeps `lastSeq`, a small held buffer and one gap timer per subscribed lot. That is a few lines of state for a ladder that is never visibly wrong, and a catch-up path the reconnect logic reuses unchanged.
- Why hold an early event instead of painting it and fixing the order later?Because a painted ladder is read by a person deciding whether to bid. Showing a lower bid above a higher one, or a leading bid that is about to be superseded by a missing one, can change a decision in the closing seconds. Holding for a second costs almost nothing and usually resolves the reordering.
- What if the server can replay events from a given sequence number?Then the client can ask for the missing range instead of reading the whole lot state, which is cheaper when the gap is small. The state read stays as the fallback, because replay only works while the server still holds those events, and a long outage outlives it.
- Paint events in arrival order; the next event will correct any mistake.
- Prices only rise, so keep the highest price and ignore sequence numbers.
- Sort the ladder by the time each event reached the browser.
- When a sequence number is missing, wait for it however long it takes.
- Refetch the whole lot on every out-of-order event, even during a closing burst.