System Architecture
UltraBalancer is built with a modular, high-performance architecture leveraging Rust’s async capabilities.Core Modules
Backend Module
Manages backend servers and their state.Server
Individual backend representation with health tracking
Server Pool
Collection of servers with concurrent-safe operations
Health Tracker
Monitors backend health and updates status
Circuit Breaker
Prevents cascading failures
- Lock-free status updates using atomics
- Arc-based shared state for zero-copy semantics
- DashMap for concurrent server access
- Per-server connection counting
Balancer Module
Implements load balancing algorithms. Architecture:- Algorithm enum: Defines available algorithms
- Selector: Unified interface for backend selection
- Algorithm implementations: Individual algorithm logic
- Each algorithm is self-contained
- Stateless where possible
- Thread-safe state management
- Zero-allocation in hot paths
Proxy Module
Handles HTTP proxying and request routing. Components:- ProxyServer: Manages TCP listener and connection dispatch
- RequestHandler: Processes individual HTTP requests
- Middleware: (Future) Request/response transformation
- Accept TCP connection
- Parse HTTP request
- Extract client metadata
- Select backend via algorithm
- Proxy request to backend
- Stream response to client
- Record metrics
Metrics Module
Collects and exports performance metrics. Collectors:- MetricsCollector: Thread-safe metric storage
- MetricsExporter: Formats metrics for export
- Request counters (total, success, failed)
- Response time statistics (avg, min, max, percentiles)
- Uptime and throughput
- Per-backend connection counts
Configuration Module
Handles configuration parsing and validation. File Structure:- YAML (primary)
- TOML (supported)
- CLI arguments (override)
Error Module
Centralized error handling with thiserror.Utils Module
Shared utilities for advanced features.- ConnectionPool: Per-backend connection limits
- RateLimiter: Token bucket rate limiting
Concurrency Model
Tokio Async Runtime
UltraBalancer uses Tokio for async I/O: Key Features:- Work-stealing scheduler
- Automatic load balancing
- Configurable thread pool size
- Async/await throughout
Shared State Management
Lock-Free Operations:- Atomic counters for metrics
- Arc + RwLock for read-heavy data
- DashMap for concurrent HashMap
- Arc wrapping for shared ownership
- Clone = reference count increment
- No data duplication
Performance Optimizations
Memory Management
Memory Management
- Arc-based sharing: No unnecessary copies
- Pre-allocated buffers: Reduced allocations
- Connection pooling: Reuse TCP connections
- Lock-free structures: Minimize contention
CPU Efficiency
CPU Efficiency
- Async I/O: No thread per connection
- Work stealing: Optimal CPU utilization
- Zero-copy proxying: Minimal data movement
- Efficient algorithms: O(1) for most operations
Network Optimization
Network Optimization
- Keep-alive connections: Reduce handshakes
- Connection pooling: Reuse backend connections
- Streaming responses: Low memory footprint
- Efficient parsing: Minimal overhead
Build Configuration
Release Profile
Optimized for maximum performance:Cargo.toml
Dependencies
Core:tokio: Async runtimehyper: HTTP implementationtower: Service abstractions
parking_lot: Fast synchronization primitivesdashmap: Concurrent HashMaparc-swap: Lock-free Arc swapping
tracing: Structured loggingserde: Serializationanyhow/thiserror: Error handling