Change backend when reaching maxconn HAProxy

I have just started using HAProxy, and I am building a real-time chat application. I wonder if it is possible to send clients to another backend when the current backend reaches its maxconn (I am using "balance source"). Because sometimes, when a backend reaches its limit, all the connections to that backend are queued and the browser keeps loading while other backends are free.


I don't know, but try this in the backend configuration (adapt 10 with your maxconn) :

acl too_many be_sess_rate gt 10
use_backend b_too_many if too_many be_sess_rate

Try this, it will use another backend if current connection on frontend is greater than 20, change the number (line no 4) to suit with your needs, also I write mode tcp, you can change it to http if you need http load balance

frontend frontend_chat 127.0.0.1:8080
    mode tcp
    balance roundrobin
    acl max_conn_reached fe_conn gt 20
    default_backend be_chat1
    use_backend be_chat1_and_chat2 if max_conn_reached

backend be_chat1
    mode tcp
    balance roundrobin
    server chat1 127.0.0.1:9001 check maxconn 10

backend be_chat1_and_chat2
    mode tcp
    balance roundrobin
    server chat1 127.0.0.1:9001 check maxconn 10
    server chat2 127.0.0.1:9002 check maxconn 10