Skip to content

Latest commit

Β 

History

History
163 lines (118 loc) Β· 4.12 KB

File metadata and controls

163 lines (118 loc) Β· 4.12 KB

πŸ” CEX Backend β€” Go + Rust + PostgreSQL

A high-performance centralized crypto exchange backend built with Go (API & services), Rust (matching engine), and PostgreSQL (data storage). Designed for scalability, modularity, and performance.


πŸ—οΈ Architecture Overview

+--------------------+       +-------------------+
|     Client UI      | <-->  |   Go API Gateway  |
+--------------------+       +-------------------+
                                     |
                                     v
+--------------------+       +-------------------+
|   PostgreSQL DB    | <-->  |  Go Settlement    |
+--------------------+       +-------------------+
                                     ^
                                     |
                            +-------------------+
                            |  Rust Match Engine|
                            +-------------------+

πŸ“¦ Components

1. API Gateway (Go)

  • REST/gRPC endpoints
  • JWT auth, session handling
  • Rate limiting
  • WebSocket price/trade feed

2. Matching Engine (Rust)

  • Pure Rust high-performance engine
  • Central Limit Order Book (CLOB)
  • Fast trade matching & queuing
  • Emits TradeExecuted, OrderUpdated

3. Settlement Service (Go)

  • Balance updates, freezing
  • Fee logic, PnL, trade logging
  • Wallet management & DB updates

4. PostgreSQL Database

  • Persistent data store for:

    • Users
    • Wallets
    • Orders & trades
    • Markets & fees
    • Deposits/withdrawals

πŸ› οΈ Tech Stack

Layer Tech
Language (API) Go (1.22+)
Matching Engine Rust (2021 Edition)
Database PostgreSQL 15+
Message Queue Redis / NATS / Kafka (optional)
WebSocket gorilla/websocket
Auth JWT / OAuth2
ORM GORM / pgx (Go)

🧰 Folder Structure

exchange-backend/
β”œβ”€β”€ cmd/
β”‚   β”œβ”€β”€ api-gateway/       # Go REST + WS API
β”‚   β”œβ”€β”€ settlement/        # Go settlement logic
β”‚   └── market-engine/     # Rust binary interface
β”‚
β”œβ”€β”€ engine/                # Rust matching engine crate
β”‚   └── src/
β”‚
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ models/            # Shared Go models
β”‚   └── utils/             # Config, logger, helpers
β”‚
β”œβ”€β”€ proto/                 # gRPC/message schema (optional)
β”œβ”€β”€ config/                # .env, configs
β”œβ”€β”€ scripts/               # DB migrations, admin tasks
└── deploy/                # Docker, K8s configs

βœ… Workflow Example

Order Placement Flow:

  1. Client sends POST /order to Go API Gateway

  2. API forwards to Rust Matching Engine

  3. Engine emits events: TradeExecuted, OrderMatched

  4. Go Settlement Service listens and updates:

    • Balances
    • Orders
    • Trade history
  5. WebSocket server broadcasts updates to client


πŸ‘‹ Example PostgreSQL Tables

  • users(id, email, password_hash, ...)
  • wallets(user_id, currency, balance, locked)
  • orders(id, user_id, market, price, qty, side, status)
  • trades(id, buy_order_id, sell_order_id, price, qty)
  • markets(symbol, base, quote, min_qty, status)
  • deposits, withdrawals, sessions

πŸ“Š Performance Goals

Component Target TPS / Latency
Matching Engine > 10,000 orders/sec
Go REST API > 3,000 req/sec
PostgreSQL Tuned with indexing
WebSocket Push < 200ms latency

πŸš€ Setup

Coming soon: Docker Compose & Kubernetes manifests to spin up full local stack.


πŸ”’ Security Best Practices

  • Enforce JWT expiration + 2FA
  • Rate limit login/order APIs
  • TLS for internal services
  • Withdrawals require multi-sig/manual approval
  • Engine is stateless and sandboxed

πŸ“ License

MIT License. Build your exchange freely, safely, and securely.


🀝 Contribute

PRs and feedback welcome! Fork and star if this helps your project.