🚀 A high-performance centralized cryptocurrency exchange built in Rust.
Introduction ·
Architecture ·
User Interface ·
Features ·
Tech Stack ·
API Endpoints ·
Order Matching & Execution ·
Contributing ·
Local Development ·
License
Exchange is a low-latency centralized cryptocurrency exchange designed for high-frequency trading (HFT). Built in Rust, it features an in-memory order book, real-time WebSocket updates, and an optimized backend powered by Redis and Postgres for maximum efficiency.
- Github Repository - Exchange-Web
- ⚡ Ultra-fast Order Matching – Sub-10ms order execution.
- 🏦 In-memory Order Book & Balances – Ensures low latency and high throughput.
- 🔥 Real-time WebSockets – Live updates for market depth, trades, and tickers.
- 📈 Advanced Matching Engine – Optimized price-time priority order matching.
- 🔄 Fault-tolerant Architecture – Redis-backed pub/sub and persistent Postgres storage.
- 🔌 Efficient API Layer – REST & WebSocket APIs for seamless integration.
- Rust – Core language for speed and memory safety.
- Redis – High-speed pub/sub and caching layer.
- Postgres – Persistent storage for trade history and user balances.
- Tokio – Asynchronous runtime for high-performance networking.
- Actix – Web framework for API handling.
- Tokio-Tungstenite - WebSockets – Real-time data streaming.
- Serde – Serialization/deserialization of structured data.
- SQLx – Database interaction with async support.
- Fred (Pub/Sub) – Efficient Redis client for messaging.
-
Primary Rust Backend
- Handles REST API and WebSocket requests.
- Routes requests to Order Management, User Balances, and Market Data services.
- Publishes responses via Redis.
-
Redis
- Acts as a message queue for real-time order processing.
- Temporarily stores market data (active orders, tickers).
- Manages pub/sub for market data updates.
-
Postgres
- Persistent storage for trade history, balances, and order data.
- Updated asynchronously to avoid performance bottlenecks.
-
WebSocket Layer
- Streams market depth, ticker data, and trade executions.
- Pushes updates in real time to connected clients.
-
Matching Engine
- Maintains in-memory order books.
- Executes trades using price-time priority.
- Manages the order lifecycle (creation, execution, cancellation).
POST /api/v1/order→ Create/Execute a new orderGET /api/v1/order→ Get details of an orderDELETE /api/v1/order→ Cancel an active orderGET /api/v1/orders→ Get open orders for a userDELETE /api/v1/orders→ Cancel all orders for a user
GET /api/v1/klines→ Get kline (candlestick) dataGET /api/v1/depth→ Get order book depthGET /api/v1/trades→ Get recent tradesGET /api/v1/tickers→ Get market tickers
POST /api/v1/users→ Create a new userPOST /api/v1/user/deposit→ Deposit funds (pending)POST /api/v1/user/withdraw→ Withdraw funds (pending)
struct Engine {
orderbooks: Vec<Orderbook>,
balances: BTreeMap<String, UserBalances>,
}
struct Orderbook {
bids: BTreeMap<Decimal, Vec<Order>>,
asks: BTreeMap<Decimal, Vec<Order>>,
asset_pair: AssetPair,
last_update_id: i64,
}
struct Order {
order_id: String,
user_id: String,
symbol: String, // e.g., SOL_USDC
side: String, // Buy/Sell
order_type: String, // Limit/Market
order_status: String,
quantity: Decimal,
filled_quantity: Decimal,
price: Decimal,
timestamp: i64,
}
struct Amount {
available: Decimal,
locked: Decimal,
}
struct UserBalances {
user_id: String,
balance: HashMap<Asset, Amount>,
}- Orders are processed asynchronously using Rust’s
tokio. - The engine pops new orders from Redis and checks the order book.
- If a match is found, trade execution happens.
- Trades and balances are published via Redis for WebSocket updates.
- Final orderbook state is stored in Postgres.
We love our contributors! Here's how you can contribute:
- Open an issue if you believe you've encountered a bug.
- Follow the local development setup guide below to get your local dev environment set up.
- Make a pull request to add new features/make quality-of-life improvements/fix bugs.
git clone https://github.com/jogeshwar01/exchange.git
cd rust-exchange
cargo build
cp .env.example .env // Configure Postgres and Redis credentials in .env.
docker-compose up -d
cargo run- Production deployment is handled by GitHub Actions.
- Add the following secrets to your GitHub repository:
DOCKER_USERNAMEDOCKER_PASSWORDPAT(Personal Access Token withreposcope)
The exchange is open-source under the MIT License. You can find it here.