Dead-simple load balancing for Cloudflare Workers
Deploy reliable load balancing in minutes. No complex configuration needed.
# 1. Clone and setup
git clone <your-repo> flowbalance
cd flowbalance
npm install
# 2. Set a default backend (optional)
# This example sets two backend servers behind your hostname
export DEFAULT_BACKENDS='{"hostname": "your-public-facing-url", "backends": ["https://server1.com", "https://server2.com"]}'
# 3. One command deploy
npm run deploy
# 4. Point a DNS A/AAAA record from `your-public-facing-url` to this cloudflare loadbalancer worker.That's it! π
Achieve industry-standard failover and fallback routing and integrate with your infrastructure seamlessly.
- Go to Google Cloud Console
- Create a new OAuth 2.0 Client ID
- Set Authorized redirect URI to:
https://your-worker.your-subdomain.workers.dev/auth/google/callback - Note the Client ID and Client Secret
Edit wrangler.toml and update the environment variables:
[vars]
# OAuth Configuration
JWT_SECRET = "your-super-secret-jwt-key-change-this-in-production"
GITHUB_CLIENT_ID = "your-github-oauth-app-client-id"
GITHUB_CLIENT_SECRET = "your-github-oauth-app-client-secret"
GOOGLE_CLIENT_ID = "your-google-oauth-client-id"
GOOGLE_CLIENT_SECRET = "your-google-oauth-client-secret"
AUTHORIZED_USERS = "your-email@example.com,another-user@example.com"
# API Configuration
API_SECRET = "your-api-secret-key"
WEB_AUTH_USERNAME = "admin"
WEB_AUTH_PASSWORD = "admin123"
# Load Balancer Configuration
# Single service format
DEFAULT_BACKENDS = "{\"hostname\":\"example.com\",\"backends\":[\"https://backend1.com\",\"https://backend2.com\",\"https://backend3.com\"]}"
# Multiple services format
DEFAULT_BACKENDS = "{\"services\":[{\"hostname\":\"api.example.com\",\"backends\":[\"https://api1.com\",\"https://api2.com\"]},{\"hostname\":\"web.example.com\",\"backends\":[\"https://web1.com\",\"https://web2.com\"]},{\"hostname\":\"admin.example.com\",\"backends\":[\"https://admin1.com\",\"https://admin2.com\"]}]}"
ENABLE_WEB_INTERFACE = "true"npm run deployNavigate to https://your-worker.your-subdomain.workers.dev and sign in with GitHub or Google!
- OAuth buttons for GitHub and Google
- Basic auth fallback
- Modern, responsive design
- Clear error messaging
- Service management interface
- Real-time health monitoring
- Add new services
- View metrics and statistics
- Global configuration overview
- API documentation
- Service Management: Add, configure, and monitor services
- Backend Control: Enable/disable individual backends
- Health Checks: Manual and automatic health verification
- Metrics: Request counts, success rates, response times
- Configuration: Session affinity, health check settings
All API calls require authentication:
# OAuth (via web interface)
Cookie: auth_token=<jwt-token>
# Basic Auth (legacy)
Authorization: Basic <base64-encoded-credentials>
# Bearer Token (API)
Authorization: Bearer <api-secret># Configure a service
POST /admin/services/{hostname}/config
Content-Type: application/json
```json
{
"vars": {
"DEFAULT_BACKENDS": "{\"hostname\": \"your-domain.com\", \"backends\": [\"https://server1.com\", \"https://server2.com\"]}"
}
}npm run deployYour load balancer is live! It automatically:
- β Health checks your backends
- β Fails over when servers go down
- β Distributes traffic evenly
- β Handles connection errors gracefully
- Smart Health Checks: Automatically finds
/health,/healthz,/statusendpoints - Zero-Downtime Failover: Instant switchover when backends fail
- Circuit Breakers: Stops sending traffic to broken servers
- Round Robin: Even traffic distribution by default
- Connection Error Handling: Retries and failover for network issues
| Variable | Description | Required |
|---|---|---|
JWT_SECRET |
Secret key for JWT signing | Yes |
GITHUB_CLIENT_ID |
GitHub OAuth app client ID | Yes |
GITHUB_CLIENT_SECRET |
GitHub OAuth app client secret | Yes |
GOOGLE_CLIENT_ID |
Google OAuth client ID | Yes |
GOOGLE_CLIENT_SECRET |
Google OAuth client secret | Yes |
AUTHORIZED_USERS |
Comma-separated list of authorized emails | Yes |
API_SECRET |
Bearer token for API access | Yes |
WEB_AUTH_USERNAME |
Basic auth username (fallback) | No |
WEB_AUTH_PASSWORD |
Basic auth password (fallback) | No |
DEFAULT_BACKENDS |
Default backend configuration (see format below) | No |
ENABLE_WEB_INTERFACE |
Enable web interface (true/false) | No |
The DEFAULT_BACKENDS environment variable uses JSON format for RFC 3986 compliance:
DEFAULT_BACKENDS = "{\"hostname\":\"example.com\",\"backends\":[\"https://backend1.com\",\"https://backend2.com\",\"https://backend3.com\"]}"Example:
DEFAULT_BACKENDS = "{\"hostname\":\"api.example.com\",\"backends\":[\"https://api-server1.com\",\"https://api-server2.com\",\"https://api-server3.com\"]}"DEFAULT_BACKENDS = "{\"services\":[{\"hostname\":\"hostname1\",\"backends\":[\"https://backend1.com\",\"https://backend2.com\"]},{\"hostname\":\"hostname2\",\"backends\":[\"https://backend3.com\",\"https://backend4.com\"]}]}"Example:
DEFAULT_BACKENDS = "{\"services\":[{\"hostname\":\"api.example.com\",\"backends\":[\"https://api1.com\",\"https://api2.com\"]},{\"hostname\":\"web.example.com\",\"backends\":[\"https://web1.com\",\"https://web2.com\"]},{\"hostname\":\"admin.example.com\",\"backends\":[\"https://admin1.com\",\"https://admin2.com\"]}]}"DEFAULT_BACKENDS = "[{\"hostname\":\"hostname1\",\"backends\":[\"https://backend1.com\",\"https://backend2.com\"]},{\"hostname\":\"hostname2\",\"backends\":[\"https://backend3.com\",\"https://backend4.com\"]}]"Format Rules:
- Use valid JSON format for all URL configurations
- Each service has a
hostnameandbackendsarray - All URLs must be properly formatted according to RFC 3986
- Each hostname gets its own independent load balancer configuration
{
"hostname": "api.myapp.com",
"backends": [
"https://api-server-1.myapp.com",
"https://api-server-2.myapp.com"
]
}{
"services": [
{
"hostname": "api.myapp.com",
"backends": ["https://api1.com", "https://api2.com"]
},
{
"hostname": "files.myapp.com",
"backends": ["https://files1.com", "https://files2.com"]
}
]
}FlowBalance works out-of-the-box with smart defaults. These settings handle 80%+ of use cases:
| Setting | Default | What it does |
|---|---|---|
| Health checks | Every 60s | Keeps bad servers out of rotation |
| Failover | Instant | Switches to healthy servers immediately |
| Retries | 1 retry | Tries failed requests once more |
| Circuit breaker | 3 failures | Stops traffic after 3 consecutive fails |
| Recovery | 60 seconds | How long to wait before trying failed servers |
Visit https://your-worker.workers.dev/__lb_admin__ to:
- π See backend health status
- βοΈ Adjust settings through the UI
- π View traffic and error metrics
- π Browse request logs
Default login: Use GitHub or Google OAuth
- Check your backend URLs are reachable
- Verify health check endpoints exist (
/health,/healthz, etc.) - Look at the admin dashboard for specific errors
- Check if backends are overloaded
- Consider adding more backend servers
- Verify network connectivity between Cloudflare and your servers
These are automatically handled! FlowBalance:
- Immediately fails over to healthy backends
- Opens circuit breakers for failing servers
- Retries with exponential backoff
π§ Custom Configuration (Click to expand)
{
"activeHealthChecks": {
"enabled": true,
"path": "/custom-health",
"interval": 30,
"timeout": 10,
"consecutive_up": 3,
"consecutive_down": 2
}
}{
"pools": [{
"id": "us-pool",
"geo_steering": {
"regions": ["US", "CA"],
"fallback_pool": "global-pool"
}
}]
}{
"load_balancer": {
"session_affinity": {
"type": "cookie",
"cookie_name": "lb_session"
}
}
}{
"backends": [
{"url": "https://big-server.com", "weight": 3},
{"url": "https://small-server.com", "weight": 1}
]
}π Performance Tuning (Click to expand)
{
"activeHealthChecks": {
"interval": 10,
"timeout": 3,
"consecutive_down": 1
}
}{
"passiveHealthChecks": {
"circuit_breaker": {
"failure_threshold": 1,
"recovery_timeout_ms": 30000,
"error_rate_threshold": 25
}
}
}{
"connection_error_handling": {
"immediate_failover": true,
"max_connection_retries": 0,
"connection_timeout_ms": 5000
}
}π’ Enterprise Features (Click to expand)
- Health scoring algorithms
- Performance analytics
- Alert integrations
- Custom middleware support
- OAuth authentication (GitHub, Google)
- API key management
- Access control lists
- Request rate limiting
- REST API for configuration
- Webhook notifications
- Metrics export
- Log streaming
- Multi-region deployments
- DNS failover support
- Edge location optimization
- Automated recovery procedures
See Advanced Configuration Guide for complete documentation.
- Fork the repo
- Create a feature branch:
git checkout -b my-feature - Run tests:
npm test - Submit a pull request
- π Documentation
- π Issues
- π¬ Discussions
MIT License - see LICENSE file.
Made with β€οΈ for developers who want load balancing, failover, and high availability that just works.