A high-performance load balancer for MongoDB Sharded Clusters and Redis Clusters built on Cloudflare's Pingora framework
A high-performance, production-ready database cluster proxy for MongoDB Sharded Clusters and Redis Clusters. Built on Cloudflare’s battle-tested Pingora framework with enterprise-grade reliability and performance.
Puerta provides intelligent database cluster proxying with advanced session management, health checking, and load balancing capabilities:
ismaster command with retry mechanismsPuerta is built on proven, high-performance foundations:
Note: This project includes local dependencies on Pingora framework components. The repository already includes the necessary Pingora framework in the examples/pingora/ directory.
git clone https://github.com/deadjoe/puerta
cd puerta
# The Pingora framework is already included in examples/pingora/
cargo build --release
Puerta uses TOML configuration files. Example configurations are provided in the config/ directory.
[server]
listen_addr = "0.0.0.0:27017"
max_connections = 10000
connection_timeout_sec = 60
worker_threads = 4
# Optional daemon mode configuration
[server.daemon]
enabled = true # Enable daemon mode
pid_file = "/var/run/puerta.pid" # PID file path
error_log = "/var/log/puerta/error.log" # Error log file
upgrade_sock = "/var/run/puerta_upgrade.sock" # Upgrade socket path
user = "puerta" # User to run as (optional)
group = "puerta" # Group to run as (optional)
[proxy]
mode = "mongodb"
mongos_endpoints = [
"mongodb1.example.com:27017",
"mongodb2.example.com:27017",
"mongodb3.example.com:27017"
]
session_affinity = true
session_timeout_sec = 1800
[health]
interval_sec = 30
[server]
listen_addr = "0.0.0.0:6379"
max_connections = 10000
connection_timeout_sec = 60
worker_threads = 4
[proxy]
mode = "redis"
cluster_nodes = [
"redis1.example.com:6379",
"redis2.example.com:6379",
"redis3.example.com:6379"
]
slot_refresh_interval_sec = 60
max_redirects = 3
connection_timeout_ms = 5000
[health]
interval_sec = 30
# Start with MongoDB configuration
./target/release/puerta run --config config/mongodb.toml
# Start with Redis configuration
./target/release/puerta run --config config/redis.toml
# Use default configuration (config/dev.toml)
./target/release/puerta run
# Enable debug logging
RUST_LOG=debug ./target/release/puerta run --config config/mongodb.toml
# Run as daemon process
./target/release/puerta run --config config/mongodb.toml --daemon
# Run as daemon with custom PID file and error log
./target/release/puerta run --config config/mongodb.toml \
--daemon \
--pid-file /var/run/puerta.pid \
--error-log /var/log/puerta/error.log
# Test configuration without starting
./target/release/puerta run --config config/mongodb.toml --test
Puerta supports seamless zero-downtime upgrades using Pingora’s graceful upgrade mechanism:
# Current setup: puerta daemon running in background
./target/release/puerta run --config config/mongodb.toml --daemon
# Upgrade process (no connection loss):
# Step 1: Start new version with --upgrade flag
./target/release/puerta_new run --config config/mongodb.toml --upgrade
# Step 2: Signal old instance to transfer listening sockets
kill -QUIT $(cat /tmp/puerta.pid)
# Advanced: Custom upgrade socket path
./target/release/puerta run --config config/mongodb.toml \
--upgrade \
--upgrade-sock /var/run/puerta_upgrade.sock
Zero-Downtime Upgrade Guarantees:
# View all available options
./target/release/puerta run --help
# Available options:
# -c, --config <CONFIG> Path to configuration file
# -d, --daemon Run as daemon process in the background
# -p, --pid-file <PID_FILE> PID file path for daemon mode
# -e, --error-log <ERROR_LOG> Error log file path for daemon mode
# -t, --test Test configuration and exit
# -u, --upgrade Enable upgrade mode for zero-downtime updates
# --upgrade-sock <UPGRADE_SOCK> Upgrade socket path for zero-downtime updates
# Run all unit tests
cargo test
# Run tests with coverage
cargo install cargo-tarpaulin
cargo tarpaulin --out Html
Comprehensive test suite for MongoDB and Redis cluster load balancing:
# Navigate to tests directory
cd tests
# MongoDB Tests
./test.sh mongo basic # Basic MongoDB functionality (~15s)
./test.sh mongo quick # Quick MongoDB verification (~30s)
./test.sh mongo full # Comprehensive MongoDB test suite (~2-3min)
# Redis Tests
./test.sh redis basic # Basic Redis functionality (~15s)
./test.sh redis quick # Quick Redis verification (~30s)
./test.sh redis full # Comprehensive Redis test suite (~2-3min)
./test.sh redis routing # Redis slot routing logic validation (~10s)
# Note: Puerta runs in single mode (MongoDB OR Redis) at any given time
# Choose tests based on currently running mode
For detailed test documentation, see tests/README.md.
# Run load balancing benchmarks
cargo bench
Puerta is designed for high-performance scenarios:
src/
├── core/ # Core connection and backend management
├── modes/ # MongoDB and Redis mode implementations
│ ├── mongodb/ # MongoDB session affinity and load balancing
│ └── redis/ # Redis cluster protocol and redirection
├── health/ # Health checking implementations
├── config/ # Configuration management
└── utils/ # Common utilities
config/ # Configuration file examples
benches/ # Performance benchmarks
The project maintains comprehensive test coverage:
Current test coverage: 116 tests passing
The project has been optimized for seamless runtime integration:
cargo testcargo clippy -- -D warningscargo build --releaseLicensed under the Apache License, Version 2.0. See LICENSE for details.