How to Protect Exchange Software from MEV Exploits
MEV bots watch the public mempool and reorder trades to profit at your users' expense. This guide covers the exploits and the defences exchange software needs.
On this page
Short answer
MEV exploits work because pending trades are visible in the public mempool, so bots can insert their own transactions ahead of a user's trade. Exchange software stops them by hiding order flow, enforcing slippage limits and deadlines, and settling trades in ways where ordering no longer pays.
What is MEV, and why does exchange software face it?
MEV, or maximal extractable value, is the extra value that can be taken from a blockchain by choosing the order of transactions in a block. On a public chain, pending trades wait in the mempool, where anyone can read them before they are confirmed. Bots scan that queue for profitable trades and insert their own transactions ahead of, or around, a user's trade.
Exchange software faces MEV whenever it sends user trades to a public chain. A decentralised exchange routes swaps through smart contracts, so every order is visible before it settles. A centralised exchange keeps its own order book off chain, so its internal trades are not exposed the same way, but it still meets MEV when it settles on chain, lists a token, or runs an on-chain product.
The cost lands on users: worse prices, failed trades, and fees paid for transactions that never had a fair chance. For the team building the software, MEV is a design problem, not a user error. The order of transactions is part of the product, so protection has to be built into the software from the start.
Which MEV exploits should exchange software defend against?
The exploits an exchange must defend against all use the same opening: a bot sees a pending trade and acts on it before it settles. The table below lists the main patterns and where each one hurts.
| Exploit | How it works | Who pays |
|---|---|---|
| Front-running | A bot copies a trade and pays a higher fee to execute first | The trader, through a worse price |
| Sandwich attack | A bot buys before and sells after a large trade | The trader, through price movement |
| Back-running | A bot trades right after a large trade to capture the price move | Liquidity providers and later traders |
| Liquidation sniping | Bots race to liquidate a position the moment it becomes eligible | The borrower, through lost collateral |
| Just-in-time liquidity | A bot adds liquidity for one block to take the fees, then removes it | Long-term liquidity providers |
Front-running and sandwich attacks are the most common threats to ordinary swaps. A closer look at how sandwich bots work shows why large swaps attract them. Liquidation sniping matters more for lending and margin products, while just-in-time liquidity mainly affects pool-based exchanges. A mitigation plan should name which of these the product actually faces, because each one needs a different defence.
How does a public mempool expose exchange trades?
A public mempool exposes exchange trades because every pending transaction is broadcast to the whole network before it is confirmed. A bot does not need access to the exchange at all: it watches the mempool, spots a large swap or a liquidation, and submits its own transaction with a higher fee so a block builder includes it first.
The exposure is worse for large trades and for tokens with thin liquidity, because a small trade moves the price more. It is also worse at busy times, when many transactions compete for the same block and the fee market rewards whoever pays most to jump the queue.
Centralised exchanges avoid most of this inside their own matching engine, where orders never touch a mempool. The risk returns when the exchange moves funds on chain: deposits, withdrawals, listings and settlements are all visible. Decentralised exchanges face the full exposure on every swap, which is why most MEV defences were built for them first. The wider set of defences a DEX uses is covered in how a decentralised exchange prevents hacking.
How do slippage limits and deadlines protect a trade?
Slippage limits and deadlines are the simplest MEV defences, and every exchange should enforce them. A slippage limit sets the worst price a trader will accept: the swap reverts if the execution price moves beyond it. A deadline sets the latest block in which the trade may execute, so a stale transaction cannot be held and used later.
These checks do not stop a bot from trying. They limit the damage: a sandwich bot cannot push the price far past the limit, because the trade simply fails instead of settling at a bad price. The user pays a fee for the failed transaction, but keeps the rest of their funds.
Defaults matter more than the feature itself. A limit of 0.5 percent protects a liquid pair well, while a thin pair may need a wider limit to execute at all. The software should suggest a limit based on the pair's liquidity and warn clearly when a user raises it. A deadline of a few minutes is enough for most swaps; longer deadlines give bots more time to act.
How do private order flow and protected relays help?
Private order flow hides a trade from the public mempool until it is already in a block. Instead of broadcasting the transaction to the whole network, the exchange sends it through a private relay directly to block builders. Bots cannot front-run a trade they cannot see.
The best-known relays are Flashbots Protect and MEV Blocker. A wallet or exchange connects to the relay's private RPC endpoint, and transactions travel through the relay instead of the public mempool. Some relays go further and share part of the value back: MEV-Share lets searchers bid for the right to back-run a transaction, and part of that bid is returned to the user as a rebate.
Private flow is the most practical single defence for a decentralised exchange, because it needs no change to the trading model. Its limit is trust: the relay sees the transaction first, so the team must choose a relay with a clear record. For a centralised exchange, the equivalent is keeping orders inside the matching engine and publishing only the settlement, which the exchange already does by design.
How do batch auctions and intent-based trading remove the ordering advantage?
Batch auctions and intent-based trading remove the ordering advantage by changing how trades settle. In a batch auction, the exchange collects orders over a short period and settles them all at one uniform clearing price. Being first in the batch no longer helps, so front-running and sandwiching lose their payoff.
Intent-based trading works the same way from the user's side. Instead of submitting a fixed swap transaction, the user signs an intent that states the outcome they want, such as a minimum amount received. Solvers compete to fill that intent at the best price, and the user's transaction is only settled when the outcome is met. CoW Protocol is the best-known implementation of this model.
These designs protect users well, but they change the product: settlement is slower than a direct swap, and the exchange needs solvers or an auction mechanism. They suit exchanges that trade on fairness and price quality rather than raw speed.
A team can also mix models, offering instant swaps with private flow for most users and batch settlement for large orders. The choice of model belongs in the early design of how to build a DEX, before the contracts are written.
How do you build MEV protection into exchange software in 6 steps?
You build MEV protection into exchange software by layering defences at the order, routing and monitoring levels, starting with the cheapest controls and adding stronger ones where the risk justifies them.
- Enforce slippage and deadlines by default. Set a sensible slippage limit and a short deadline on every swap, and warn users who widen them.
- Route through private order flow. Connect the exchange to a private relay such as Flashbots Protect or MEV Blocker so trades skip the public mempool.
- Add commit-reveal for sensitive actions. For auctions, listings and large orders, hide the trade details in a first step and reveal them only when the order is fixed.
- Offer batch settlement for large trades. Give users the choice of a batch auction or intent-based fill when the order is large enough to attract bots.
- Protect oracles and liquidations. Use time-weighted prices for liquidations and set a minimum delay, so a single block's price move cannot trigger a cascade.
- Monitor and respond. Track failed trades, sandwich patterns and price impact per pair, and alert the team when a pair's attack rate rises.
Test each layer on a testnet with simulated attacks before launch. A defence that only works in a demo will not survive the first busy market day. An external smart contract audit should check the MEV defences as part of the review, not as an afterthought.
Example (hypothetical): a DEX launch stops a sandwich attack
Example (hypothetical): a new decentralised exchange lists a token at a quoted price of 2,000 tokens per ETH. A trader swaps 10 ETH and expects 20,000 tokens. A sandwich bot sees the trade in the public mempool, buys first, and pushes the price so the trader receives only 1,900 tokens per ETH, or 19,000 tokens. The trader loses 1,000 tokens, about 5 percent of the trade.
The bot bought 10,000 tokens for 5 ETH before the trade, then sold them after it at 1,900 tokens per ETH, receiving 10,000 divided by 1,900, which is about 5.26 ETH. Its profit is 5.26 minus 5.00, or about 0.26 ETH before fees.
The exchange then routes all swaps through a private relay. The same 10 ETH trade is hidden from the mempool, executes at the quoted price, and the trader receives the full 20,000 tokens. The bot sees nothing to attack, and the exchange's monitoring shows failed-trade reports on that pair falling to almost zero.
What are the common mistakes?
The most common MEV mistakes are relying on a single defence, setting slippage limits too wide, and treating MEV as a user problem instead of a product problem.
- One defence only. Slippage limits alone do not stop a determined bot; private flow alone does not help if the relay is down. Layer at least two defences.
- Wide slippage defaults. A default of 5 percent or more invites sandwich attacks. Set tight defaults and make users confirm any increase.
- Ignoring liquidations. A lending product that liquidates on a single price reading is an easy target. Use time-weighted prices and delays.
- No monitoring. If nobody watches failed trades and price impact per pair, an attack can run for weeks before anyone notices.
- Promising zero MEV. No design removes MEV entirely. Promise measurable protection, not a guarantee.
- Forgetting the centralised side. Internal order handling, listings and settlements need the same fairness rules as the on-chain parts.
Key takeaways
- MEV is value taken by reordering transactions, and the public mempool is what makes it possible
- Front-running and sandwich attacks hit ordinary swaps hardest; liquidations and just-in-time liquidity need their own defences
- Slippage limits and short deadlines cap the damage, but they do not stop bots from trying
- Private relays such as Flashbots Protect hide trades from the mempool until they are already in a block
- Batch auctions and intent-based fills remove the ordering advantage entirely, at the cost of slower settlement
- Layer at least two defences and monitor failed trades per pair, because no single control removes MEV
Frequently asked questions
What is MEV in simple terms?
MEV is the extra value that can be taken by choosing the order of transactions in a block. Bots watch the public mempool for profitable pending trades, then insert their own transactions before or after them to capture the price move.
Is MEV illegal?
MEV itself is not a crime in most places, because transaction ordering is part of how public blockchains work. Sandwich attacks and front-running are widely seen as unfair, and some venues ban them in their rules, but enforcement is still developing.
Does MEV affect centralised exchanges?
Less than decentralised ones, because a centralised exchange matches orders off chain where bots cannot see them. The risk returns when the exchange settles on chain, lists a token, or runs an on-chain product such as a DEX or a launchpad.
What is the cheapest MEV protection to add first?
Slippage limits and short deadlines cost almost nothing to build and cap the damage from most attacks. Routing through a private relay is the next step, because it hides trades from the mempool without changing the trading model.
What is the difference between a sandwich attack and front-running?
Front-running is a single trade placed ahead of yours to capture a price move. A sandwich attack places two trades, one before and one after yours, so the attacker profits from the price moving up and then back down around your trade.
Can MEV ever be removed completely?
No. As long as someone decides the order of transactions, there is value in that decision. Good exchange software makes extraction expensive and hard, returns some value to users, and keeps the worst attacks from working at all.