Rebuild an order book from a snapshot and updates
An L2 snapshot gives quantities aggregated by price. Incremental messages update later levels. The meaning of a quantity, update order and resynchronization procedure come from the venue protocol. In Kraken WebSocket v2, a quantity of zero removes a level. The subscriber trims each side to the requested depth and can verify the checksum over the top ten levels. Kraken book checksum guide
Reproduce a small update by hand
Start with bids 100 × 3 and 99 × 2 and asks 101 × 1 and 102 × 4. Apply bid 100 × 0 and ask 101 × 2 with absolute quantities. The best bid becomes 99 with size 2, and the best ask remains 101 with size 2. Adding the new quantity to the old would give an incorrect ask size of 3.
Keep bids sorted descending and asks ascending. Process all levels in one message before publishing an updated book. Preserve decimal precision for prices and quantities, especially if the checksum depends on their textual digits.
Reject an unsynchronized book
Use the specified sequence or checksum to detect a missed update. Mark the book unavailable after a gap and obtain a new snapshot using the venue’s synchronization procedure. In a recorded test, drop one frame and verify that no stale intermediate book is emitted after integrity fails. Binance local book procedure · Reconnect and resynchronize a feed.