evgenyigumnov / cblt

Safe and fast minimalistic web server, written in Rust, that serves files from a directory and proxies requests to another server.
MIT License
40 stars 7 forks source link

Load Balancer #20

Closed evgenyigumnov closed 3 days ago

evgenyigumnov commented 1 week ago

Here are some examples of load balancing configurations for Caddy:

1. Basic Load Balancing Across Multiple Backends


example.com {
    reverse_proxy /api/* backend1:8080 backend2:8080 backend3:8080
}

This configuration will load balance requests to /api/* across backend1, backend2, and backend3 on port 8080.


2. Load Balancing with Health Checks

example.com {
    reverse_proxy backend1:8080 backend2:8080 {
        health_uri /health
        health_interval 10s
        health_timeout 2s
        lb_policy round_robin
    }
}

This setup checks each backend’s health by sending requests to /health every 10 seconds. If a backend doesn't respond within 2 seconds, it's considered unhealthy. Load balancing is done using the round_robin strategy.


3. Sticky Sessions with Load Balancing

example.com {
    reverse_proxy backend1:8080 backend2:8080 backend3:8080 {
        lb_policy cookie
        lb_cookie_name my_sticky_cookie
        lb_cookie_path /
        lb_cookie_max_age 3600
    }
}

Here, sticky sessions are enabled using cookies. Clients will be consistently routed to the same backend based on my_sticky_cookie.


4. Load Balancing with Failover

example.com {
    reverse_proxy backend1:8080 backend2:8080 {
        lb_policy first
        lb_try_duration 30s
        lb_try_interval 5s
    }
}

This configuration uses a failover strategy, where Caddy tries the first backend, and if it fails, it switches to the next backend, retrying every 5 seconds for up to 30 seconds.

These configurations can be adjusted depending on the needs of your infrastructure.

evgenyigumnov commented 3 days ago

https://github.com/evgenyigumnov/cblt/commit/ed5b6ab8520f89e3ab1b19e5d27ea5831a209ea1