Bring a local order book back in line with the venue: start from a checkpoint, apply diffs after its block in block order, and re-anchor at a checkpoint when the book stops matching. A Hyperliquid L4 or full-depth L2 book is a baseline plus ordered changes, so one skipped, repeated, or reordered diff corrupts every later state.
Symptoms
- The best bid is at or above the best ask in your local book.
- Order or level counts drift away from the server’s snapshot for the same time.
- An
update or remove diff names an order your book does not hold.
- The book goes wrong after a reconnect, a lag notice, or a
gap_detected event.
Match the symptom to the cause
How the records are ordered
- L4 snapshots (
/l4, /l4/history) carry last_block_number, the chain block the book is current to, plus checkpoint_timestamp and diffs_applied. diffs_applied: 0 is a clean checkpoint; a higher number means the server rolled the last checkpoint forward to your time.
- L4 diffs (
/l4/diffs) carry block_number, seq, oid, side, price, diff_type and new_size. diff_type is new, update or remove, and new_size is null on a remove. The REST diffs come back in (block_number, seq) order.
- L4 WebSocket batches (
l4_batch) carry the same events with short names: bn for the block, dt for the diff type, px and sz. Items are ordered by block number, then by their order within the block. A new order can carry insert_before: insert it ahead of that order ID within its price level, or append it at the tail when the key is absent.
- Full-depth L2 diffs (
/l2/diffs) are price-level changes derived from L4: block_number, side, price, and the level’s size and count after the diff. Set the level to those values.
Never sort book events by timestamp alone. Many events share one block time.
Rebuild and verify a book over REST
This script anchors a Hyperliquid SOL book at 23:00 UTC yesterday, applies one minute of L4 diffs after the anchor block, and compares the result with the server’s book at the end of that minute:
A correct rebuild prints the same block on both sides and the same bid and ask counts, for example local block 1232392518 server block 1232392518 and bids 4809 4809 asks 6323 6323. The diff window overlaps the anchor on purpose; the block filter drops what the anchor already holds. Apply insert_before as well when queue position matters.
Re-anchor after a mismatch
A mismatch means the local book is no longer a valid state. Do not patch it level by level.
- Mark the book unsafe and stop publishing from it.
- Fetch a checkpoint at or before the last time you trust:
/l4 with timestamp, or the nearest entry from /l4/history for a window. Over the WebSocket, unsubscribe and subscribe again: a new L4 subscription opens with a fresh l4_snapshot. For history, start a new replay, which anchors at the latest checkpoint at or before start.
- Apply diffs whose block number is greater than the new
last_block_number, in order.
- Record the mismatch, the checkpoint you re-anchored at, and the gap in the run manifest.
Lighter books
Lighter books do not need diff sequencing on the live stream. Every lighter_orderbook message is a full book of up to 20 levels per side, not a diff, and a newer book is never replaced with an older one. Replace your local book with each message. For Lighter tick history on /v1/lighter/orderbook/{symbol}/history with granularity=tick, each item holds a checkpoint plus its ordered deltas. Keep the checkpoint, follow the returned cursor without resetting it, and order deltas by sequence. Sequence values order deltas but are not a consecutive counter within one market, so a jump in sequence is not a gap by itself.
Verify the fix
- Your book’s block and order counts match a server snapshot at the same block.
- No diff with a block number at or below the anchor’s
last_block_number was applied.
- Every reconnect, lag notice and
gap_detected event led to a re-anchor, recorded with the output.
Next step
Last modified on December 1, 2026