Architecture
Exchange is built with a microservices architecture where specialized components handle different aspects of the trading system. This design ensures high performance, fault tolerance, and scalability.System overview
The exchange consists of four main components that communicate via Redis pub/sub:Core components
Router
REST & WebSocket API layerHandles all incoming HTTP requests and routes them to the appropriate service via Redis queues.
Engine
Order matching engineMaintains in-memory order books and executes trades using price-time priority matching.
WS Stream
Real-time data streamingManages WebSocket connections and pushes live market data updates to subscribed clients.
DB Processor
Asynchronous persistenceProcesses database writes asynchronously to avoid blocking the critical trading path.
Component details
Router
The router is built with Actix Web and serves as the main entry point for all client requests. Location:crates/router/src/main.rs
Key responsibilities:
- Accept HTTP requests on
/api/v1/*endpoints - Validate and parse request payloads
- Push messages to Redis queues for processing
- Return responses from Redis pub/sub
- Serve market data directly from Postgres (for historical data)
Engine
The matching engine is the heart of the exchange, responsible for maintaining order books and executing trades. Location:crates/engine/src/main.rs
Key responsibilities:
- Maintain in-memory order books for all trading pairs
- Track user balances in-memory
- Execute orders using price-time priority
- Validate user balances before order placement
- Generate fill notifications
- Publish trade executions to Redis
Attempt to match
Search the opposite side of the order book for matching orders:
- For buy orders: check asks (sell orders)
- For sell orders: check bids (buy orders)
Execute trades
If matches are found:
- Update filled quantities
- Update user balances
- Generate fill events
- Remove fully filled orders
WS Stream
The WebSocket server manages real-time connections and pushes market data updates to subscribed clients. Location:crates/ws-stream/src/main.rs
Key responsibilities:
- Accept WebSocket connections from clients
- Handle subscribe/unsubscribe requests
- Listen to Redis pub/sub channels
- Push updates to subscribed clients
- Manage connection lifecycle
depth@{SYMBOL}- Order book depth updatestrades@{SYMBOL}- Real-time trade executionsticker@{SYMBOL}- Ticker data updates
DB Processor
The database processor handles all Postgres writes asynchronously to keep the critical path fast. Location:crates/db-processor/src/main.rs
Key responsibilities:
- Pop database update messages from Redis
- Write trade executions to Postgres
- Update user balances in persistent storage
- Store order book snapshots
- Handle kline (candlestick) updates
Data flow
Here’s how data flows through the system for a typical order placement:Engine processes order
Engine pops the order from Redis, validates balances, and attempts to match it.
Trade execution
If matched, the engine:
- Updates in-memory balances
- Publishes trade events to Redis
- Sends order book updates to Redis
WebSocket broadcast
WS Stream receives trade events from Redis and broadcasts to subscribed clients.
Database persistence
DB Processor receives trade data from Redis and writes to Postgres asynchronously.
Redis queues and channels
Redis serves as the communication backbone: Queues (LPUSH/RPOP):ORDERS- Order creation, cancellation requestsUSERS- User creation, balance updatesDATABASE- Asynchronous database write requests
depth:{SYMBOL}- Order book updatestrades:{SYMBOL}- Trade executionsticker:{SYMBOL}- Ticker updates
Performance characteristics
The in-memory architecture enables sub-10ms order execution, but requires careful memory management for production deployments.
- Order validation: < 1ms
- Matching engine: < 5ms
- Balance updates: < 1ms
- Redis pub/sub: < 2ms
- WebSocket push: < 1ms
Scalability considerations
For production deployments:- Horizontal scaling: Run multiple router instances behind a load balancer
- Engine sharding: Partition order books by trading pair across multiple engine instances
- Redis clustering: Use Redis Cluster for high availability
- Database replication: Set up Postgres read replicas for market data queries
- WebSocket scaling: Use Redis pub/sub to coordinate updates across multiple WS servers
Next steps
API reference
Explore all available endpoints
Deployment
Learn how to deploy to production