June 12, 2026 · 6 min read
The ack that wasn't durable: a WAL bug in my matching engine
How Nerve's Go matching engine told clients an order was placed before it was safe on disk, and the two fixes that closed the gap.
Nerve's matching engine runs one goroutine per symbol. That goroutine is an actor: it owns the order book, reads commands from an inbox, and is the only thing that ever touches the book. No locks, no contention, and matching stays fast and predictable.
Every command is written to a write-ahead log before the engine replies. If the process dies, it replays the WAL on start and rebuilds the book exactly. That was the plan, anyway.
The bug
The actor called wal.WriteEntry(data) and then sent the response on the reply channel. WriteEntry writes into a bufio.Writer. The bytes sit in memory until the next periodic sync flushes them.
// before
wal.WriteEntry(data) // buffered, not on disk yet
m.replay <- response // client hears "placed"
// ...crash here and the order is goneSo there's a window where the client has been told its order is on the book, and the order exists only in RAM. Crash in that window and the order disappears, even though the client has a success response for it. For an exchange that's the worst kind of bug: silent, rare, and about money.
Fix one: sync before you speak
The rule is simple: never acknowledge what isn't durable. The engine now calls wal.Sync() after the write and before the reply, in all three branches (place, cancel, modify), and the symbol config turns fsync on.
wal.WriteEntry(data)
wal.Sync() // flushed and fsynced
m.replay <- responsefsync per command costs latency. The honest trade is to batch: collect the commands that arrive in one tick, write them, sync once, then reply to all of them. Durability per batch, latency amortised.
Fix two: retries that know what happened
The second problem sat one hop up. The order service talks to the engine over gRPC. On a timeout it can't tell whether the engine never saw the order or processed it and the reply was lost. Retrying with the same clientOrderId got rejected as a duplicate, so the client saw an error for an order that might be live.
The fix is a GetOrderStatus(clientOrderId) RPC that goes through the actor's inbox like everything else. On a timeout the order service asks first and only retries if the engine has never heard of the order.
What I took from it
- An acknowledgement is a promise. Make sure the promise is on disk first.
- Idempotency keys aren't enough on their own; the caller also needs a way to ask what happened.
- Single-writer actors make these bugs easy to reason about, because there's exactly one place where the order of events is decided.