# abhinavsaurabh/low-latency-cpp-matching-engine architecture

> Architecture diagram of the GitHub repository abhinavsaurabh/low-latency-cpp-matching-engine, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abhinavsaurabh/low-latency-cpp-matching-engine
- Repository: https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine

## Overview

This is a compact, single-symbol exchange-style matching engine. An order-entry client submits an order; the engine sequences it, checks pre-trade limits, and delegates book operations. The book applies price-time priority, updates resting liquidity, and produces command events; the engine updates positions from executions and returns results. Clients can also cancel or modify orders and inspect best bid/ask snapshots. The graph preserves the documented limit/market, IOC, event, and risk capabilities. Source excerpts establish the main calls; unsampled interface declarations and demo behavior are represented only where README or indexed references support them.

Main entry points: [`src/matching_engine.cpp`](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/matching_engine.cpp) (Matching engine), [`src/order_book.cpp`](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/order_book.cpp) (Order book), [`src/risk.cpp`](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/risk.cpp) (Pre-trade risk), [`include/hft/execution.hpp`](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/include/hft/execution.hpp) (Events and snapshots)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Order entry"]
  node_engine["Matching engine"]
end

subgraph group_core["Matching core"]
  node_risk["Pre-trade risk<br/>[risk.cpp]"]
  node_book["Order book<br/>[order_book.cpp]"]
  node_matcher["Price-time matching<br/>[order_book.cpp]"]
end

subgraph group_state["Book and risk state"]
  node_bookstate[("Book state<br/>[order_book.hpp]")]
  node_positions[("Trader positions<br/>[risk.cpp]")]
end

subgraph group_model["Domain model"]
  node_orders["Order model<br/>[order.hpp]"]
  node_execution["Events and snapshots<br/>[execution.hpp]"]
  node_types["Shared trading types<br/>[types.hpp]"]
end

node_client(("Order-entry client"))

node_client -->|"submits commands"| node_engine
node_engine -->|"validates orders"| node_risk
node_engine -->|"delegates commands"| node_book
node_engine -->|"records fills"| node_risk
node_book -->|"matches flow"| node_matcher
node_matcher -->|"updates liquidity"| node_bookstate
node_book -->|"reads and maintains"| node_bookstate
node_risk -->|"reads and updates"| node_positions
node_engine -->|"checks and sequences"| node_orders
node_book -->|"adds and modifies"| node_orders
node_book -->|"creates results"| node_execution
node_engine -->|"returns command results"| node_execution
node_execution -->|"uses shared types"| node_types
node_book -->|"uses event model"| node_execution
node_risk -->|"validates order fields"| node_orders

click node_engine "https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/matching_engine.cpp"
click node_risk "https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/risk.cpp"
click node_book "https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/order_book.cpp"
click node_matcher "https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/order_book.cpp"
click node_bookstate "https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/include/hft/order_book.hpp"
click node_positions "https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/risk.cpp"
click node_orders "https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/include/hft/order.hpp"
click node_execution "https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/include/hft/execution.hpp"
click node_types "https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/include/hft/types.hpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_engine,node_client toneBlue
class node_risk,node_book,node_matcher toneAmber
class node_bookstate,node_positions toneMint
class node_orders,node_execution,node_types toneRose
```

## Components

### Order entry

- [Matching engine](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/matching_engine.cpp): `src/matching_engine.cpp`

### Matching core

- [Pre-trade risk](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/risk.cpp): `src/risk.cpp`
- [Order book](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/order_book.cpp): `src/order_book.cpp`
- [Price-time matching](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/order_book.cpp): `src/order_book.cpp`

### Book and risk state

- [Book state](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/include/hft/order_book.hpp): `include/hft/order_book.hpp`
- [Trader positions](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/src/risk.cpp): `src/risk.cpp`

### Domain model

- [Order model](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/include/hft/order.hpp): `include/hft/order.hpp`
- [Events and snapshots](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/include/hft/execution.hpp): `include/hft/execution.hpp`
- [Shared trading types](https://github.com/abhinavsaurabh/low-latency-cpp-matching-engine/blob/main/include/hft/types.hpp): `include/hft/types.hpp`

### Other components

- **Order-entry client**

## Connections

- Order-entry client → Matching engine: submits commands (evidence: `README.md`)
- Matching engine → Pre-trade risk: validates orders (evidence: `src/matching_engine.cpp`)
- Matching engine → Order book: delegates commands (evidence: `src/matching_engine.cpp`)
- Matching engine → Pre-trade risk: records fills (evidence: `src/matching_engine.cpp`)
- Order book → Price-time matching: matches flow (evidence: `src/order_book.cpp`)
- Price-time matching → Book state: updates liquidity (evidence: `src/order_book.cpp`)
- Order book → Book state: reads and maintains (evidence: `include/hft/order_book.hpp`)
- Pre-trade risk → Trader positions: reads and updates (evidence: `src/risk.cpp`)
- Matching engine → Order model: checks and sequences (evidence: `src/matching_engine.cpp`)
- Order book → Order model: adds and modifies (evidence: `src/order_book.cpp`)
- Order book → Events and snapshots: creates results (evidence: `src/order_book.cpp`)
- Matching engine → Events and snapshots: returns command results (evidence: `src/matching_engine.cpp`)
- Events and snapshots → Shared trading types: uses shared types (evidence: `include/hft/execution.hpp`)
- Order book → Events and snapshots: uses event model
- Pre-trade risk → Order model: validates order fields
