Skip to content

Latest commit

Β 

History

49 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

High-Performance Matching Engine - High-Performance Matching Engine

A production-ready perpetual futures exchange matching engine with nanosecond-level latency, featuring advanced optimizations including memory pooling, lock-free data structures, SIMD acceleration, and optimized persistence.

πŸš€ Features

Core Trading Features

  • βœ… Order book management (Red-Black Tree, ART, O(log n))
  • βœ… Price-time priority matching engine (nanosecond latency)
  • βœ… Position management (bidirectional positions)
  • βœ… Account management (margin, P&L)
  • βœ… Funding rate calculation
  • βœ… Event Sourcing & Deterministic Calculation
  • βœ… Microservices Architecture (Matching Service + Trading Service)

Production Features

  • βœ… User authentication & authorization (JWT, API keys)
  • βœ… Liquidation engine (risk calculation, forced liquidation)
  • βœ… Funding rate management (auto settlement)
  • βœ… Market data service (K-line, depth, 24h statistics)
  • βœ… API Gateway (routing, authentication, rate limiting)
  • βœ… Monitoring system (Prometheus metrics, alerts)
  • βœ… Notification service (email, SMS, push)
  • βœ… Database manager (multi-database support)
  • βœ… RESTful API server (HTTP/1.1, JSON)

Performance Optimizations

  • βœ… Memory pool optimization (+5-10% performance)
  • βœ… Lock-free data structures (+10-20% concurrency)
  • βœ… SIMD optimization (2-4x batch computation on x86_64)
  • βœ… NUMA-aware optimization (multi-core)
  • βœ… FPGA acceleration framework (reserved)

Infrastructure Features

  • βœ… Logging system (5-level, file output)
  • βœ… Configuration management (INI + environment variables)
  • βœ… Metrics collection (Prometheus format)
  • βœ… Error handling (custom exception system)
  • βœ… Rate limiting (Token bucket algorithm)
  • βœ… Health checking (system health monitoring)
  • βœ… Optimized persistence (async writing, 3.6x throughput)
  • βœ… Graceful shutdown (signal handling)
  • βœ… Docker support (multi-stage builds)
  • βœ… Kubernetes ready

πŸ“Š Performance

See PERFORMANCE_BENCHMARK_REPORT.md for detailed performance comparison.

Performance Benchmarks

Key Optimizations:

  • Memory pooling for efficient allocation
  • Lock-free data structures
  • SIMD optimizations (AVX2) - 2-4x acceleration
  • ART (Adaptive Radix Tree) - better cache locality
  • NUMA awareness
  • Hot path optimizations

Performance Results (vs Original):

  • ART+SIMD: +25-45% throughput, -35-55% latency ⭐
  • Optimized V2: +20-30% throughput, -20-30% latency
  • ART: +10-20% throughput, -15-25% latency
  • Optimized: +15-25% throughput, -10-20% latency

Running Benchmarks

# Quick test (10K orders)
./run_benchmark.sh 10000

# Full test (50K orders)
./run_benchmark.sh 50000

# Or run directly
cd build && ./comprehensive_performance_comparison 10000

Persistence Performance

  • Trade Logging: 368K trades/sec, 2.71 ΞΌs latency
  • Order Logging: 358K orders/sec, 2.79 ΞΌs latency
  • Throughput Improvement: 3.6-3.7x over original

πŸ—οΈ Architecture

Monolithic Architecture

perpetual_exchange/
β”œβ”€β”€ include/core/          # Core headers
β”‚   β”œβ”€β”€ order.h            # Order structure
β”‚   β”œβ”€β”€ orderbook.h        # Order book (Red-Black Tree, ART)
β”‚   β”œβ”€β”€ matching_engine.h  # Matching engine
β”‚   β”œβ”€β”€ auth_manager.h     # Authentication & authorization
β”‚   β”œβ”€β”€ liquidation_engine.h  # Liquidation system
β”‚   β”œβ”€β”€ funding_rate_manager.h  # Funding rate management
β”‚   β”œβ”€β”€ market_data_service.h  # Market data service
β”‚   β”œβ”€β”€ api_gateway.h      # API gateway
β”‚   β”œβ”€β”€ monitoring_system.h  # Monitoring system
β”‚   β”œβ”€β”€ notification_service.h  # Notification service
β”‚   β”œβ”€β”€ database_manager.h  # Database manager
β”‚   └── rest_api_server.h  # REST API server
β”œβ”€β”€ src/core/              # Core implementations
β”œβ”€β”€ src/                   # Applications and benchmarks
β”œβ”€β”€ services/              # Microservices
β”‚   β”œβ”€β”€ matching_service/  # Matching Service (gRPC)
β”‚   └── trading_service/   # Trading Service (gRPC)
└── docs/                  # Documentation

Microservices Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   API Gateway   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
    β”‚         β”‚
β”Œβ”€β”€β”€β–Όβ”€β”€β”€β” β”Œβ”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
β”‚Tradingβ”‚ β”‚Matching β”‚
β”‚Serviceβ”‚ β”‚ Service β”‚
β””β”€β”€β”€β”¬β”€β”€β”€β”˜ β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
    β”‚         β”‚
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ Production         β”‚
    β”‚ Components         β”‚
    β”‚ - Auth             β”‚
    β”‚ - Liquidation      β”‚
    β”‚ - Funding Rate     β”‚
    β”‚ - Market Data      β”‚
    β”‚ - Notification     β”‚
    β”‚ - Database         β”‚
    β”‚ - Monitoring       β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • C++17 compiler (GCC 7+, Clang 5+, MSVC 2017+)
  • CMake 3.10+
  • (Optional) Docker for x86_64 SIMD testing

Build

# Clone repository
git clone https://github.com/lanpishu6300/matching-engine.git
cd matching-engine

# Build
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . -j$(nproc)

# Or use Makefile
make build

Run Production Server

# Prepare configuration
cp config.ini.example config.ini
# Edit config.ini as needed

# Run
cd build
./production_server ../config.ini

Docker Deployment

# Build production image
make docker-build

# Run with Docker Compose
docker-compose -f docker-compose.production.yml up -d

πŸ“– Documentation

πŸ”§ Configuration

See config.ini.example for all configuration options:

# Logging
log.level=INFO
log.file=logs/exchange.log

# Rate Limiting
rate_limit.global_orders_per_second=10000.0
rate_limit.per_user_orders_per_second=1000.0

# Persistence
persistence.enabled=true
persistence.db_path=./data
persistence.buffer_size=10000
persistence.flush_interval_ms=100

πŸ“Š Benchmarks

Run Benchmarks

# Use the benchmark script
./run_benchmark.sh 10000

# Or run directly
cd build
./comprehensive_performance_comparison 10000  # All versions comparison
./quick_benchmark      # Quick test (10K orders)
./full_benchmark       # Full benchmark
./persistence_benchmark  # Persistence performance

Performance Results

See PERFORMANCE_BENCHMARK_REPORT.md for detailed results.

Summary:

  • ART+SIMD: 625-1160K orders/sec, 0.5-1.0 ΞΌs latency ⭐
  • Optimized V2: 600-1040K orders/sec, 0.8-1.4 ΞΌs latency
  • Original: 500-800K orders/sec, 1.2-2.0 ΞΌs latency
  • SIMD Acceleration: 2-4x on x86_64
  • Persistence Throughput: 360K+ records/sec

🎯 Production Ready

This project includes all production-grade features:

  • βœ… Comprehensive logging
  • βœ… Configuration management
  • βœ… Metrics and monitoring
  • βœ… Error handling
  • βœ… Rate limiting
  • βœ… Health checks
  • βœ… Optimized persistence
  • βœ… Graceful shutdown
  • βœ… Docker support

πŸ“ License

[Add your license here]

πŸ‘€ Author

lanpishu6300@gmail.com

πŸ™ Acknowledgments

  • Inspired by industry-leading nanosecond-latency matching engines
  • Built with modern C++17 and best practices

About

High-Performance Perpetual Futures Exchange Matching Engine

Topics

Resources

Stars

390 stars

Watchers

8 watching

Forks

Releases

Packages

Contributors

Languages