Skip to content

UdayPandey01/hft-exhange-simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fulcrum Exchange: High-Frequency Trading (HFT) Simulator

Language Language Platform Messaging Database

Fulcrum is a high-performance, event-driven financial exchange simulator engineered for microsecond-level latency and high-throughput order processing. The project explores the core challenges of financial technology, leveraging a distributed microservices architecture with Rust for the matching engine and Go for concurrent network services.


Architectural Overview 🏗️

The system comprises decoupled, specialized microservices communicating asynchronously via a central message bus (Kafka). This architecture ensures resilience, scalability, and clear separation of concerns.

graph TD
    subgraph Client
        A[gRPC Client]
    end

    subgraph Backend Services
        B(Order Gateway - Go) -- gRPC --> A
        C(Matching Engine - Rust)
        D(Market Data Distributor - Go)
        E(Clearing & Settlement - Go)
    end

    subgraph Message Bus
        K[Apache Kafka]
    end

    subgraph Database
        P[PostgreSQL]
    end

    subgraph Frontend
        W[WebSocket Client]
    end

    A -->|"1. SubmitOrder Request"| B
    B -->|"2. Publishes Order"| K(orders topic)
    K(orders topic) -->|"3. Consumes Order"| C
    C -->|"4. Publishes Trade"| K2(trades topic)
    K2 -->|"5. Consumes Trade"| D
    K2 -->|"6. Consumes Trade"| E
    D -->|"7. Broadcasts Trade"| W
    E -->|"8. Persists Trade"| P
Loading

Core Features ✨

  • Low-Latency Matching Engine
    Developed in Rust for predictable, microsecond-level performance using a price-time priority algorithm.

  • Event-Driven Architecture
    Microservices are decoupled and communicate asynchronously via Kafka, ensuring resilience and scalability.

  • High-Performance API
    The Order Gateway exposes a gRPC API for efficient, low-latency order submission.

  • Real-Time Market Data
    The Market Data Distributor broadcasts trade events instantly to clients using WebSockets.

  • Durable Settlement
    The Clearing & Settlement service persists all executed trades in PostgreSQL for auditing and compliance.


Tech Stack 🛠️

Component Technology Rationale
Matching Engine Rust (Tokio) Zero-cost abstractions, no garbage collector, predictable low-latency execution
Network Services Go Excellent concurrency model for high-throughput I/O-bound tasks
API & Communication gRPC, Protobuf High-performance, language-agnostic RPC framework
Messaging / Event Bus Apache Kafka Durable, scalable, high-throughput message queue for service decoupling
Database PostgreSQL ACID-compliant relational DB for reliable trade persistence
Real-Time Frontend WebSockets Bi-directional protocol for instant market data broadcasting
Containerization Docker Portable, reproducible environments for every service
Orchestration Kubernetes (Planned) Automated deployment, scaling, and management of containers

Getting Started 🚀

Follow these steps to set up and run the system locally.


Prerequisites ✅

Ensure the following tools are installed:

  • Rust & Cargo – for the Matching Engine
  • Go – for Order Gateway, Market Data, and Settlement services
  • Docker & Docker Compose – to run Kafka and PostgreSQL
  • Protobuf Compiler (protoc) – to compile gRPC service definitions
  • grpcurl – for testing the gRPC API
  • wscat – for testing WebSocket connections

Running the System ⚙️

Start each service from the project’s root directory in separate terminal windows.

Terminal 1: Start Infrastructure

docker compose up

Terminal 2: Start the Matching Engine (Rust)

cargo run --bin matching-engine

Terminal 3: Start the Order Gateway (Go)

go run ./services/order-gateway-go/cmd/main.go

Terminal 4: Start the Market Data Distributor (Go)

go run ./services/market-data-go/cmd/main.go

Terminal 5: Start the Clearing & Settlement Service (Go)

go run ./services/clearing-settlement-go/cmd/main.go

Testing the End-to-End Flow

Terminal 6: Connect a WebSocket Client

Connect to the Market Data Distributor to view real-time trades:

wscat -c ws://localhost:8080/ws

Terminal 7: Execute a Trade

Send a SELL order to rest on the book:

grpcurl -plaintext -proto ./proto/v1/order.proto -d '{"user_id": "seller-789", "symbol": "AAPL", "quantity": 10.0, "price": 150.00, "side": "SELL"}' localhost:50051 v1.OrderService/SubmitOrder

Send a matching BUY order to create a trade:

grpcurl -plaintext -proto ./proto/v1/order.proto -d '{"user_id": "buyer-123", "symbol": "AAPL", "quantity": 5.0, "price": 150.00, "side": "BUY"}' localhost:50051 v1.OrderService/SubmitOrder

Observe the Result

Trade data will appear instantly in your WebSocket client (Terminal 6). You can also verify trade records in the PostgreSQL database, persisted by the settlement service.

About

A high-performance HFT exchange simulator featuring a low-latency Rust matching engine and concurrent Go microservices communicating via Kafka.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors