Why pgBalancer
AI Load Balancing Engine
Machine learning algorithm with adaptive learning rate (10%), exploration vs exploitation (20%), and weighted random selection. Learns from query execution patterns and automatically optimizes routing decisions.
Predictive Query Routing
AI analyzes query complexity (0-100 scale), estimates rows, detects read/write operations, and predicts execution time. Routes queries to optimal backends based on health scores and current load (0.0-1.0 scale).
Health Scoring & Metrics
Each backend node tracked with avg_response_time, current_load, total_queries, success/failed queries, error_rate, predicted_load, and health_score. Metrics decay over time for freshness.
Adaptive Learning System
AI updates node metrics after each query execution. Learns from feedback (response time, success/failure) and adjusts health scores. Continuous model improvement with success rate tracking.
REST API Management
Production HTTP/JSON API with 17 endpoints: /api/v1/status (server stats), /api/v1/nodes (backend management), /api/v1/health/stats, /api/v1/control/reload, /api/v1/watchdog/info, and AI statistics.
MQTT Event Publishing
Distributed coordination via MQTT protocol. Publishes node_status changes, failover_events, and health_check results to topics: pgbalancer/cluster/health, pgbalancer/cluster/failover, pgbalancer/cluster/config.
bctl CLI Tool
Command-line client with MQTT integration. Commands: bctl nodes, bctl status, bctl health, bctl watchdog-status. Supports --table (box-drawing), --json, and --verbose output formats.
JWT Authentication
Optional HMAC-SHA256 JWT tokens. Login endpoint: POST /api/v1/auth/login. Bearer token format with 1-hour expiry (3600s). Backwards compatible with password authentication.
Connection Pooling
Efficient connection reuse with configurable parameters: num_init_children (32), max_pool (4 per child), child_life_time (300s), child_max_connections (0=unlimited). Automatic cleanup and session management.
Watchdog Clustering
Multi-node watchdog coordination with leader election, heartbeat monitoring, automatic failover coordination, and recovery management. Supports VIP (Virtual IP) management and distributed consensus.
Query Cache
Intelligent query result caching with cache invalidation, memory management, and TTL support. AI-driven cache warming and prefetching based on query patterns.
3-Node PostgreSQL Cluster Architecture
pgbalancer (Port 5432)
AI-Powered Connection Pooler & Load Balancer
• Exploration: 20%
• Health Scoring
• Query Analysis
• 17 Endpoints
• JWT Auth
• AI Statistics
• Node Status
• Failover Events
• Health Checks
PostgreSQL Primary
PostgreSQL Standby 1
PostgreSQL Standby 2
AI Routing Logic
(Node 1)
(All 3 Nodes)
(AI Predicted)
| Feature | Description | Benefit | Performance | Scalability |
|---|---|---|---|---|
| AI Intelligence Engine | Machine learning algorithms analyze patterns and optimize automatically | 30% performance improvement, self-tuning capabilities | Real-time ML optimization | Adaptive resource allocation |
| Intelligent Connection Pooling | AI-driven connection reuse with predictive scaling capabilities | 15x connection efficiency, reduced latency | <0.5ms overhead per query | 10,000s of concurrent clients |
| Smart Load Balancing | ML-powered query distribution with adaptive workload optimization | Intelligent scaling, optimized resource usage | AI algorithm selection | 1000+ backend nodes |
| Predictive Scaling | AI forecasts traffic patterns and pre-scales resources automatically | Zero-downtime scaling, traffic prediction | ML-based forecasting | Dynamic auto-scaling |
| Health Monitoring | AI-powered continuous backend health prediction and monitoring | Predictive failover, 99.99% availability | Intelligent health checks | Multi-backend AI monitoring |
| Intelligent Query Cache | AI-driven caching with machine learning pattern recognition | 90% cache hit rate, intelligent prefetching | ML-backed, nanosecond retrieval | Adaptive cache sizing |
| REST API | AI-enhanced HTTP API with intelligent management and monitoring | Smart integration, AI insights | Async, ML-optimized | AI-native, cloud-ready |
| Adaptive Query Routing | AI analyzes patterns and routes queries to optimal backends | Intelligent replica usage, ML-based routing | AI query parsing & optimization | Smart read replica distribution |
| MQTT Clustering | Distributed cluster coordination via MQTT messaging | Multi-node coordination, automatic discovery | Event-driven, real-time updates | Horizontal scaling, fault tolerance |
| bctl Management | Command-line utility for cluster administration | Easy configuration, monitoring integration | Fast CLI operations, real-time monitoring | DevOps-friendly, automation ready |
| Real-Time Metrics | Comprehensive monitoring with Prometheus integration | AI insights, performance analytics | Low-latency metrics, ML-enhanced | Cloud-native monitoring, scalable |
| Feature | pgbalancer | pgpool-II | PgBouncer | Pgcat |
|---|---|---|---|---|
| AI Intelligence Engine | ✓ Machine Learning | ✗ | ✗ | ✗ |
| Predictive Scaling | ✓ AI-Powered | ✗ | ✗ | ✗ |
| Intelligent Connection Pooling | ✓ AI-Enhanced | ✓ Advanced | ✓ Basic | ✓ Advanced |
| Smart Load Balancing | ✓ ML-Optimized | ✓ Multi-algo | ✗ | ✓ Round-robin |
| AI-Enhanced REST API | ✓ AI Insights | ~ PCP protocol | ✗ | ~ HTTP stats |
| Intelligent Query Cache | ✓ ML-Driven | ✓ Built-in | ✗ | ✗ |
| Predictive Health Checks | ✓ AI-Predicted | ✓ Advanced | ~ Basic | ✓ Advanced |
| Intelligent Failover | ✓ AI-Enhanced | ✓ Automatic | ✗ | ✓ Automatic |
| Adaptive Query Routing | ✓ AI-Optimized | ✓ Intelligent | ✗ | ~ Basic |
| Performance | Ultra High (AI+C) | High (C) | Very High (C) | High (Rust) |
| MQTT Clustering | ✓ Distributed | ✗ | ✗ | ✗ |
| Management CLI | ✓ bctl Tool | ~ pgpool commands | ✗ | ~ Basic CLI |
| Real-Time Metrics | ✓ Prometheus + AI | ~ Basic stats | ~ Simple metrics | ~ Basic monitoring |
Key Features
AI Load Balancing
Machine learning algorithm with adaptive learning, response time prediction, and health scoring. Learns from query patterns.
Predictive Analytics
AI forecasts query execution times, analyzes complexity, and predicts backend performance based on historical data.
Adaptive Routing
Intelligent query distribution using exploration vs exploitation strategy, weighted selection, and health scoring.
REST API
Production-ready HTTP/JSON API with 17 endpoints including /api/v1/nodes, /api/v1/status, and AI statistics.
MQTT Clustering
Distributed coordination via MQTT with event publishing for node status, failover events, and health checks.
bctl CLI Tool
Command-line client with box-drawing tables, JSON output, and MQTT integration for cluster management.
JWT Authentication
Optional HMAC-SHA256 JWT tokens with Bearer format, 1-hour expiry, and backwards-compatible password auth.
Connection Pooling
Efficient connection reuse with configurable pool sizes (num_init_children, max_pool), connection timeouts, and cleanup.
Health Monitoring
Continuous backend health checks with configurable intervals, timeout detection, and automatic node recovery.
Watchdog Support
Multi-node coordination with leader election, automatic failover, and coordinated recovery across instances.
Query Analysis
Smart query parsing with read/write detection, complexity estimation (0-100 scale), and optimal backend selection.
High Performance
Ultra-fast C implementation with <10ms REST API response time and <0.5ms query routing overhead.
Deploy pgbalancer for Production
Install pgbalancer and start scaling your PostgreSQL connections with AI-powered load balancing, automatic failover, and comprehensive monitoring.