By Editorial Team, reviewed by Editorial Team (approved by site owner) · Published · 2 min read
The matching engine is the heart of an exchange. It is conceptually simple and unforgiving in practice.
Back and lay
A back bet says "I think this outcome will happen" at a price the customer wants. A lay bet says "I will take the other side" at a price. A match happens when a back offer and a lay offer agree on the price.
The order book
For each market outcome, the engine keeps two sorted lists: back offers (best price first) and lay offers. The customer screen shows the best few prices on each side and the amounts available.
Price-time priority
A common rule is price first, then time: better prices match first, and among equal prices the earlier offer matches first. Whatever rule you choose, apply it consistently and document it, because customers will compare outcomes.
Partial matches
An order is often only partly filled. The unmatched part either waits in the book or is cancelled, depending on the order type. The engine must track matched and unmatched amounts exactly and never lose a fraction.
Liability
A lay bet risks more than the stake: the layer's liability depends on the price. The wallet must reserve that liability when the offer is placed and release or adjust it as matching, cancellation and settlement happen. Mistakes here show up directly as negative balances.
Correctness before speed
- Process orders for one market in a single ordered stream so results are deterministic.
- Make every operation idempotent, so a retry cannot double-match.
- Keep an append-only log of orders and matches so you can replay and audit any market.
- Handle market suspension (for example at the start of an event) in the same ordered stream, not as a side channel.
Test like it matters
Replay recorded order flows, check that the book never crosses, and compare wallet totals before and after. Exchanges tend to fail in the edge cases: simultaneous cancels and matches, rounding, and suspension timing.
This is a pattern overview, not a complete design. Your market types and volumes will shape the details.

