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.
- β 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)
- β 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)
- β 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)
- β 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
See PERFORMANCE_BENCHMARK_REPORT.md for detailed performance comparison.
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
# 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- 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
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
βββββββββββββββββββ
β API Gateway β
ββββββββββ¬βββββββββ
β
ββββββ΄βββββ
β β
βββββΌββββ ββββΌβββββββ
βTradingβ βMatching β
βServiceβ β Service β
βββββ¬ββββ βββββ¬ββββββ
β β
ββββββ¬βββββ
β
ββββββΌββββββββββββββββ
β Production β
β Components β
β - Auth β
β - Liquidation β
β - Funding Rate β
β - Market Data β
β - Notification β
β - Database β
β - Monitoring β
ββββββββββββββββββββββ
- C++17 compiler (GCC 7+, Clang 5+, MSVC 2017+)
- CMake 3.10+
- (Optional) Docker for x86_64 SIMD testing
# 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# Prepare configuration
cp config.ini.example config.ini
# Edit config.ini as needed
# Run
cd build
./production_server ../config.ini# Build production image
make docker-build
# Run with Docker Compose
docker-compose -f docker-compose.production.yml up -d- Architecture Guide - Detailed architecture design
- Deployment Guide - Production deployment instructions
- Performance Comparison - Performance benchmarks
- Persistence Optimization - Persistence module optimization
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# 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 performanceSee 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
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
[Add your license here]
- Inspired by industry-leading nanosecond-latency matching engines
- Built with modern C++17 and best practices