1. What is an order book?
The order book consists of two queues sorted by price:
- 1.Bid:Sorted by price from highest to lowest—the highest bidder is awarded first
- 2.Ask (Ask):Sorted by price from low to high—the lowest asking price is sold first
The difference between the highest bid and the lowest ask is called the "spread."

2. Basic rules of matchmaking
When new orders come in, the matching engine does one thing: see if they can be closed immediately.
- 1.Bid price ≥ Lowest ask price → Immediate transaction, lowest selling price
- 2.Ask price ≤ Highest bid price → Immediate Execution, based on the highest bid price
- 3.None of these works, → Put it in the order book and wait for the counterpart's orders
Key rule: New orders are always executed at the "counterparty's price," not at your own listed price. This ensures price continuity.

3. Price - Time Priority
When multiple orders are priced the same, who closes first? The rules are:
- 1.Price priority:Better price prioritized (higher buy orders, lower sell orders)
- 2.Time priority:When prices are the same, first come, first served
That's why high-frequency traders place servers next to exchange data centers—a one-millisecond delay could get them back.

4. Different order types
There are two basic types:
- 1.Market Order:No price was specified; the deal was immediately executed at the best price offered by the counterparty
- 2.Limit Orders:Set a price: if it can be closed, it's done; if not, place an order, etc
Derived options include IOC (Immediate Execution with Uncompleted Partial Cancellation), FOK (Full Execution or Full Withdrawal), Post-Only (Orders Placed Without Taking Orders), etc.

5. The real bottleneck in matching performance
The matchmaking itself is not very computational, but the bottleneck lies in:
- 1.Order book data structure:High-frequency adding, deletion, check, and correction, red-black trees and table skipping are mainstream
- 2.Persistence:Every order must be placed to recover from a crash
- 3.Downstream Notice:Transaction notifications should be broadcast to downstream systems such as market data, clearing, and risk control
- 4.Risk control pre-installed:All orders must undergo risk control inspections before being matched
What truly determines system performance is not the "speed" of the algorithm, but the engineering capability throughout the entire order lifecycle.

