
CEX
A centralised exchange, matched in Rust.
CEX is a spot trading exchange built end to end: a Rust matching engine with price-time priority, an Axum REST and WebSocket API, Postgres persistence with JWT authentication, and a browser trading terminal with a live order book, tape and candlestick chart. Orders live in memory, fills are emitted as events, and the whole path is benchmarked.
- Role
- Matching engine, API and terminal
- Stack
- Rust · Tokio · Axum · SQLx
- Data
- Postgres · WebSocket event stream
- Year
- 2026 · source available
- Rust
- Tokio
- Axum
- SQLx
- Postgres
- WebSockets
- JWT
- Argon2
- Criterion
- Docker
- Chart.js
- Vanilla JS
The matching engine
The engine is the centre of the system. Orders are stored in two sorted sides of the book, and incoming orders match by price first, then time. Every fill produces an execution report, updates both orders and removes filled levels. Five order types — market, limit, IOC, FOK and post-only — share the same matcher, so the API layer never has to reason about book state directly.
- Price-time priority. Better price wins; at the same price the older order is filled first.
- Five order types. Market and limit orders walk the book; IOC and FOK decide what happens to the remainder, and post-only orders are rejected if they would cross.
- Evented results. Trades, fills and book changes are emitted as events for the API and the WebSocket stream.
- Measured, not guessed. The matcher has a Criterion benchmark so regressions show up as numbers.
The API and persistence
Axum exposes the exchange over REST and WebSockets. Registration and login use Argon2 password hashes and JWT sessions; authenticated handlers place and cancel orders, while public handlers serve markets, tickers, the book and recent trades. SQLx talks to Postgres with compile-time-checked queries and migrations, and the service ships with a Dockerfile and environment-based config so the same binary runs locally and in a container.
- REST. Auth, orders, balances, markets and trade history split across focused handler modules.
- WebSocket. A single connection streams book deltas, trades and tickers to the terminal.
- Storage. Postgres through SQLx with migrations, typed DTOs and an explicit error layer.
- Operations. .env.example, Docker image and a built-in simulator that quotes and cancels orders so the book is never empty.
The terminal
An exchange is only as good as the screen traders actually use. The front end is deliberately dependency-light: plain ES modules, a shared API/WebSocket client, and Chart.js for candles. It keeps the order book, tape and ticker in sync from one socket, lets you switch symbols and timeframes without a reload, and puts the order ticket next to the depth book so price, size and spread are always in view.

The terminal. Symbol, depth, tape, candles and the order ticket on one screen.

Live depth. Bids and asks update from the WebSocket stream, with spread and last price in the header.

Sessions. Argon2-hashed passwords, JWT sessions and a terminal that refuses to render without a valid token.
What I focused on
- Correctness before features. One book per symbol, explicit order status transitions, and a small deterministic matcher that is easy to reason about.
- One source of truth. The engine owns order state; the API and WebSocket layers only translate events.
- Fast feedback. Benchmarks and a simulation mode make it possible to iterate on the book without a real market.
- A terminal that stays honest. Every number on screen comes from the same socket that drives the book.