Education

What is a matching engine?

By LayerZeroNov 2, 20264 min read

A matching engine is the part of an exchange that receives orders, keeps them in an order book, and pairs buyers with sellers. When a new order arrives, the engine checks whether it can trade against orders already resting in the book. If the prices cross, it creates a trade. If they do not, a limit order waits in the book until it is filled, canceled, or expires. The engine's rules decide who trades first and at what price.

Last updated: November 2, 2026.

How does a matching engine handle a limit order?

A limit order sets the worst price a trader will accept. A buy limit order can trade against any sell order at that price or lower, and a sell limit order against any buy order at that price or higher. The engine compares the incoming order with the best orders on the other side of the book. If they cross, it fills as much as it can at the prices of the resting orders.

Any part that does not fill is added to the book at the limit price, unless the order's instructions say otherwise. A market order has no price limit. It trades against the best available orders until it is filled, that side of the book runs out, or a venue protection limit stops it.

What decides which order is filled first?

The matching algorithm. Every common algorithm gives priority to the best price first. They differ in how they share fills between orders at the same price, or in when matching happens.

AlgorithmHow fills are decided after priceWhere it is used
Price-time priorityThe earliest order at that price fills firstMany stock and crypto order books
Pro-rataFills are shared in proportion to the size of each orderSome futures markets
Call or batch auctionOrders are collected for a period, then cleared at one priceOpening and closing auctions, and venues that limit speed races

Price-time priority is also called first in, first out.

What order types does a matching engine support?

Beyond limit and market orders, most engines accept instructions that control how an order behaves:

  • Post-only: the order may only rest in the book. If it would trade immediately, it is rejected or repriced.
  • Immediate-or-cancel: whatever can fill now fills, and the rest is canceled.
  • Fill-or-kill: the whole order fills at once, or nothing does.
  • Stop: the order waits until the market reaches a trigger price, then enters as a market or limit order.

Where does the matching engine run?

A centralized exchange runs its engine on its own servers, and users see only the results. On an onchain venue, matching runs in smart contracts or in the chain itself, so matched trades are recorded publicly and anyone can verify that the rules were followed. A hybrid venue matches orders offchain for speed and settles balances onchain.

The matching engine is one component of a wider system. The guide to crypto exchange backends shows how it fits with risk, ledgers, and settlement.

FAQ

Is a matching engine the same as an order book?

No. The order book is the list of resting buy and sell orders. The matching engine is the logic that applies the rules to that list and creates trades.


Do AMMs use a matching engine?

No. A basic automated market maker prices each trade against a pool of assets using a formula, so no orders are paired. The comparison of order books and AMMs covers both models.


What is a partial fill?

A partial fill happens when only part of an order finds a match. The rest of a limit order stays in the book, unless its instructions cancel it.

Build trading across chains

For tools to build applications that work across chains, see LayerZero developer resources.

Connect to our team

Start building