A Rust-based centralized exchange (CEX) simulator built to explore systems programming concepts such as asynchronous actor-based architecture, order matching, balance management, transactional persistence, and recovery.
The project focuses on the core backend components of a spot exchange.
latency benchmarks measured over 100 iterations.
- Place Order : ~ 2.419 ms
- Match Order : ~ 2.713 ms
- Cancel Order: ~ 1.400 ms
crates
├── api (REST API and HTTP routes)
│
├── balance (Balance manager and reservation logic)
│
├── benchmark (End-to-end benchmark runner)
│
├── domain (Shared domain models)
│
├── engine (Matching engine and order book)
│
├── exchange (Actors and exchange orchestration)
│
└── storage (PostgreSQL repositories)
- Price-Time Priority (FIFO) matching engine
- Limit and Market (IOC) orders
- Partial and complete order fills
- Balance reservation and release
- Atomic settlement
- PostgreSQL persistence
- Recovery on restart
- REST API
- Order cancellation
- End-to-end latency benchmarking
- Rust
- Tokio
- Axum
- SQLx
- PostgreSQL
- Serde
Uses Price-Time Priority (FIFO) matching with a BTreeMap for price levels and a VecDeque for FIFO execution within each price level.
Supported Orders
- Limit
- Market (IOC)
Supported Operations
- Full fills
- Partial fills
- Multiple maker matches
- Order cancellation
Exchange state is persisted in PostgreSQL. Settlement is executed atomically using a PostgreSQL transaction to ensure consistency across balances, orders, and trades.
POST /depositPOST /ordersPOST /orders/{order_id}/cancelGET /balances/{user_id}GET /book/{market}git clone https://github.com/<your-username>/CEX-rust.git
cd CEX-rustUpdate your .env file with the database connection.
DATABASE_URL=postgres://postgres:password@localhost/cexrustRun migrations:
cargo sqlx migrate runcargo run -p apicargo run -p benchmark -- place
cargo run -p benchmark -- match
cargo run -p benchmark -- cancelMIT



