Handle Duplicate Market Observations After a Reconnect
A reconnect is where clean market-data code goes to die. You drop the socket, resubscribe, and the server replays a snapshot you already have. Apply it twice and your local book is wrong. Skip the resync and your book silently drifts. This post covers how Kalshi's orderbook feed actually frames snapshots, deltas and sequence numbers, where reconnect boundaries fall, and what to persist so a bad book is detectable instead of invisible. It is about market observation, not order placement.
The protocol: snapshot, then relative deltas
On Kalshi's v2 WebSocket, the order book arrives on one channel. When you subscribe to orderbook_delta, the server sends an initial full state, then incremental changes. The docs state it plainly: the channel "Sends orderbook_snapshot first, then incremental orderbook_delta updates."
The two message types do different jobs, and conflating them is the most common bug.
- orderbook_snapshot replaces a market's book wholesale. Treat it as absolute state.
- orderbook_delta is a relative quantity change at one price level, not a new absolute value. A collector that stores price to quantity rebuilds from the snapshot and then mutates per delta, removing levels that reach zero or below.
- Kalshi books are bids-only on both sides by design. A YES bid at price p is the same resting interest as a NO ask at 1 minus p, so there is no separate ask array to reconcile.
One more protocol detail that bites people: prices can arrive as strings and the server does not guarantee identical text for the same price across a snapshot and a later delta. Compare prices as decimals, not as raw strings, or you will create phantom levels. If you are building a strategy on Banger, note the SDK already hands you tick prices as Decimal, which sidesteps that particular trap.
seq is per subscription, not per market
This is the single fact that decides how you handle a reconnect, and it is easy to get wrong. Every orderbook_delta carries a seq, described in Kalshi's reference as a sequence number for consistency verification. The number is monotonic per subscription (the sid), not per market.
A production write-up measured this directly: subscribing to 30 tickers at once produced 30 snapshot messages numbered seq 1 to 30, and every delta afterward, across all tickers, continued one shared sequence (31, 32, 33 and so on). The practical consequence is blunt. A gap in seq means the whole subscription is suspect, so you rebuild the subscription, not one market.
So the dedup and gap logic is: track the last seq you accepted. If the next delta is not last_seq plus one, you have a gap. Stop trusting every book on that sid, not just the one that looked odd.
Where reconnect boundaries create duplicates
After any disconnect, a fresh subscribe gives you a fresh snapshot with seq restarting. Duplicates come from three places: the replayed snapshot after reconnect, deltas that were in flight when the socket dropped, and re-delivery of a snapshot the server emits on its own. That last one is real. On a determined (closed) market, Kalshi production has been observed sending an orderbook_snapshot with an empty market_id and no price levels. A client that rejected the empty ID and closed the socket looped on reconnect, one reconnect roughly every 11 seconds for over half an hour, taking down every other market's book on that connection. The lesson: your reconnect handler is itself a source of duplicate and malformed input, so it must be idempotent.
The clean rule for dedup is to make snapshots authoritative and deltas idempotent against seq. A snapshot resets state unconditionally. A delta is applied only if its seq is exactly the next one expected. Anything with a seq at or below your last accepted value is a duplicate and gets dropped. You do not need, and will not get, exactly-once delivery. You get an ordered sequence you can verify, which is enough to detect loss and dedup replays.
Resync without cycling the whole connection
Historically the only recovery was to reconnect and wait for a new snapshot. Per the Kalshi changelog, a get_snapshot action was added to update_subscription on orderbook_delta. It sends an orderbook_snapshot for the requested markets without adding them to the subscription or affecting the existing delta stream, and only market_tickers is accepted (not market_ticker, market_id, or market_ids). Re-verify this against the live docs before you depend on it, since the channel surface changes.
That gives you two valid recovery paths after a gap, and both end in a fresh snapshot replacing the uncertain book before you act on it:
- Full reconnect: drop the socket, re-authenticate, resubscribe, rebuild from the new snapshot. Simple, heavier, resets seq for the whole sid.
- In-place resync: call update_subscription with get_snapshot for the affected tickers, replace those books, and keep the existing delta stream running.
Whichever you pick, do not apply deltas to a market until its snapshot has arrived. A widely cited failure mode is a client that keeps receiving deltas for a ticker it never snapshotted, swallows them, and leaves the local book permanently empty with only a debug log to show for it.
What evidence to save
The order book exists only if you record it. A minute later, that state is gone from every source, so your capture is the only ground truth you will have. Save enough to prove whether a book was valid at any instant:
- Raw snapshot and delta messages with their seq and timestamp, per sid, exactly as received. Kalshi added optional millisecond timestamps (ts_ms) on orderbook_delta; prefer those where present.
- Local control events in the same stream: connect, disconnect, subscribed, gap, resync. One open-source collector tags these as local events so a replay knows exactly when a book is invalid. Without them you cannot tell a real market move from a reconnect artifact.
- A note of whether each book was snapshot-ready when you read it, so downstream analysis can discard reads taken before resync completed.
When a state must be rebuilt is now simple to state. Rebuild on any seq gap, on any reconnect, and on any delta for a market without a current snapshot. In every case, replace the book from a fresh snapshot rather than guessing what changed.
Where this fits a paper workflow
If you are prototyping a strategy rather than running your own collector, Banger is a paper-research beta for exactly this stage. Hosted historical backtesting currently supports Kalshi binary markets, and the SDK exposes a per-tick callback (on_market_tick) with Decimal prices and a possibly-absent bid or ask. Public live deployment is blocked, so this is for building and inspecting evidence, not venue execution. Treat a paper result as evidence under a specific data, fill and fee model, not proof of real fills. The reconnect discipline above is what keeps the input to that evidence honest: a backtest fed from a silently drifted book is measuring your bug, not the market. Install with pip install bangertrades and verify the current API surface against the package and CLI docs before wiring anything up.
None of this buys exactly-once delivery, and no schema here is invented: snapshot replaces, delta mutates, seq verifies order per subscription, and a gap means rebuild. Get those four right and duplicates after a reconnect become a non-event.
Sources
- Orderbook Updates - Kalshi API Documentation
- Kalshi WebSocket API Reference (orderbook_delta fields, seq)
- Streaming Polymarket and Kalshi Order Books over WebSockets: 7 Lessons from Production
- Kalshi API Changelog (get_snapshot action, ts_ms)
- Kalshi API: The Complete Developer Guide (2026) - Parlay
- Keep WebSocket open on closed-market orderbook snapshots (PR #4, fastkalshi)
- WS: orderbook permanently desyncs after a sequence gap (Issue #189)
- chess-bets: order-book logger, Kalshi seq gaps and recording
- apma4903: Kalshi order-book collector semantics and local events
- Kalshi API Guide: Market Data, Auth & Rate Limits
- bangertrades on PyPI
- Banger CLI documentation