> For the complete documentation index, see [llms.txt](https://docs.evoq.finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.evoq.finance/advanced-mechanism/matching-engine/priority-queue-matching.md).

# Priority Queue Matching

While a queueing model could be used, it could be exploited by numerous accounts supplying tiny amounts, causing high gas costs for borrowers. Instead, we use a <mark style="color:blue;">**priority queue matching**</mark> system that sorts users by descending volume, ensuring that higher volumes are matched first. For each new request, the protocol arranges the top 8 opposite requests by volume to respond. Below is an example.

<figure><img src="/files/idXVbcsciWDT85PSpbTk" alt=""><figcaption><p>Example: Priority queue matching</p></figcaption></figure>

1. The initial state: Supply 1 is 800 USDC, supply 2 is 1000 USDC, and supply 3 is 500 USDC.
2. When borrow 4 is requested, the priority goes to supply 2.
3. After matching order 1, supply 1 remains at 800 USDC, supply 2 at 600 USDC, and supply 3 at 500 USDC.&#x20;
4. Thus, for borrow 5, the next priority is supply 1.
5. After matching order 2, supply 1 is 300 USDC, supply 2 is 600 USDC, and supply 3 is 500 USDC.
6. Hence, for borrow 6, the next priority is supply 2.
7. Then, matching order 3 is processed.
8. Unmatched amounts of supply 1 (300 USDC) and supply 3 (500 USDC) fall back to the underlying protocol.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.evoq.finance/advanced-mechanism/matching-engine/priority-queue-matching.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
