A real-time cryptocurrency trading platform modeled on Binance, built as a set of decoupled microservices communicating over Redis. It features a matching engine with live order books, real-time depth and trade streaming over WebSockets, candlestick (k-line) charts, and persistent trade history.
Tech stack: Next.js, React, TypeScript, Node.js, Express, WebSockets, Redis, TimescaleDB (PostgreSQL), Docker.
Matching-engine order book throughput on a mixed workload: 60% new limit orders, 30% orders that trade against the book and 10% cancels, each followed by a depth snapshot. Throughput stays roughly flat as the book grows from 2,000 to 100,000 resting orders.
| Resting orders | Orders/sec | p50 latency (µs) | p99 latency (µs) |
|---|---|---|---|
| 2,000 | 53,814 | 17.30 | 33.38 |
| 20,000 | 43,838 | 21.55 | 46.30 |
| 100,000 | 46,764 | 19.46 | 27.30 |
These are single-threaded, in-process benchmarks of the engine's order book, not end-to-end API throughput. See Performance for details.
The system is split into independent services that talk to each other through Redis queues and pub/sub channels. The matching engine is a single-threaded process that handles one message at a time; the API, WebSocket server and database processor run as separate processes.
┌──────────┐
│ frontend │ Next.js UI (charts, order book, trades)
└────┬─────┘
REST │ WebSocket
┌────────────┴────────────┐
▼ ▼
┌─────────┐ ┌─────────┐
│ api │ │ ws │ streams depth/trade updates
└────┬────┘ └────▲────┘
│ push orders │ pub/sub
▼ │
┌──────────────── Redis ───────┴────────────┐
│ queues: "messages", "db_processor" │
└────┬───────────────────────────┬──────────┘
▼ ▼
┌─────────┐ ┌─────────┐
│ engine │ matching │ db │ persists trades
└─────────┘ engine └────┬────┘
▲ ▼
┌─────────┐ ┌─────────────┐
│ mm │ market maker │ TimescaleDB │
└─────────┘ └─────────────┘
| Service | Description | Port |
|---|---|---|
api |
Express REST API. Accepts orders and serves order book depth, recent trades, k-lines, and tickers. Pushes order requests onto the Redis messages queue and awaits the engine's response. |
3000 |
engine |
The core matching engine. Consumes orders from the Redis messages queue, maintains the in-memory order book, matches trades, and publishes results. Supports state snapshots (WITH_SNAPSHOT=true) for recovery on restart. |
— |
ws |
WebSocket server. Lets clients subscribe to channels (e.g. depth, trades) and pushes real-time updates via Redis pub/sub. | 3001 |
db |
Database processor. Consumes trade events from the Redis db_processor queue and writes them to TimescaleDB. Also refreshes materialized views used for k-line data. |
— |
mm |
A market maker bot that places orders against the API to provide liquidity for testing/demo. | — |
frontend |
Next.js trading UI: candlestick charts (lightweight-charts), live order book depth, trade feed, markets page, and a swap/order form. |
3000 (Next dev) |
- Node.js (18+)
- Docker & Docker Compose
Spin up Redis and TimescaleDB:
cd docker
docker-compose up -dThis starts:
- Redis on
localhost:6379 - TimescaleDB on
localhost:5432(db:my_database, user:your_user, password:your_password)
Each service is a standalone Node package. In separate terminals:
# Matching engine
cd engine && npm install && npm run dev
# REST API
cd api && npm install && npm run dev
# WebSocket server
cd ws && npm install && npm run dev
# Database processor
cd db && npm install && npm run seed:db # seed schema / tables
cd db && npm run dev
# Market maker (optional, provides liquidity)
cd mm && npm install && npm run devcd frontend
npm install
npm run devOpen http://localhost:3000.
Base URL: http://localhost:3000/api/v1
| Method | Endpoint | Description |
|---|---|---|
POST |
/order |
Place a new order |
GET |
/depth |
Current order book depth |
GET |
/trades |
Recent trades |
GET |
/klines |
Candlestick (k-line) data |
GET |
/tickers |
Ticker information |
The order book (engine/src/trade/Orderbook.ts) keeps each side as price levels sorted by price, with a FIFO queue of orders at each level, so orders match best price first and then in arrival order. An order-id index makes cancels O(1), and each level keeps a running total of its remaining quantity, so a depth snapshot reads one value per price level instead of scanning every order.
Hardware: Apple M5 (10 cores, 24 GB), Darwin 25.6.0 (darwin arm64), Node v24.16.0. Seed 42; 5,000 measured ops (+500 warm-up) per run; median of 3 runs, each in a fresh process. Commit 48468d1.
| Resting orders | Scenario | Orders/sec | p50 latency (µs) | p99 latency (µs) |
|---|---|---|---|---|
| 2,000 | mixed (60% insert / 30% cross / 10% cancel, + depth) | 53,814 | 17.30 | 33.38 |
| 2,000 | insert + depth | 51,276 | 17.25 | 25.25 |
| 2,000 | cross, partly filled | 354,518 | 2.55 | 6.00 |
| 2,000 | cancel | 2,801,964 | 0.25 | 1.21 |
| 20,000 | mixed (60% insert / 30% cross / 10% cancel, + depth) | 43,838 | 21.55 | 46.30 |
| 20,000 | insert + depth | 49,997 | 19.05 | 26.34 |
| 20,000 | cross, partly filled | 404,218 | 2.34 | 5.17 |
| 20,000 | cancel | 2,059,536 | 0.34 | 1.21 |
| 100,000 | mixed (60% insert / 30% cross / 10% cancel, + depth) | 46,764 | 19.46 | 27.30 |
| 100,000 | insert + depth | 44,147 | 20.67 | 30.75 |
| 100,000 | cross, partly filled | 419,110 | 2.30 | 5.25 |
| 100,000 | cancel | 1,672,604 | 0.55 | 1.34 |
- mixed is closest to how the engine runs, because
Engine.tspublishes depth after every message. - cross, partly filled: an incoming order sweeps the best price level and the unfilled rest is placed on the book.
- The book is held at the stated size throughout each run.
- These benchmarks don't include Redis, the API, the database or the network.
Run them yourself:
cd engine
npm ci
npm run bench -- --label after # writes bench/results/after.json
npm run bench:report # regenerates the tables in this READMEMethod, scenario definitions, raw results and a comparison with the previous implementation are in engine/bench/README.md.
Each service is TypeScript with its own tsconfig.json. Common scripts:
npm run build— compile TypeScript todist/npm run dev— build and runnpm test— run tests (the engine uses Vitest)
ISC