Search code examples
haproxyblue-green-deployment

In HAProxy, is it possible to stop routing traffic to a specific server with active sessions by disabling the server?


I am trying implement the following setup

HA -|
    |- Redis1
    |- Redis2

At any time only one of the redis instances should serve the incoming requests.

Going by the documentation, it seems that you can disable a server dynamically and HA would stop directing the traffic to the disabled server. While this worked for new client connections, existing client connections are still served content from the disabled server. But if I kill the redis instance, even the existing client connections are redirected to the other instance.

Is it possible to achieve this behavior without killing the instance?

Heres my HA config:

global
    stats socket /opt/haproxy/admin.sock mode 660 level admin
    stats socket ipv4@*:19999 level admin

defaults
    log global
    mode tcp

listen myproxy
    mode tcp
    bind *:4444
    balance roundrobin
    server redis1 127.0.0.1:6379 check
    server redis2 127.0.0.1:7379 check

Solution

  • Found the answer. Need to add the following directives:

    on-marked-down shutdown-sessions
    

    This closes any existing sessions. Eg:

    server redis1 127.0.0.1:6379 check on-marked-down shutdown-sessions
    server redis2 127.0.0.1:7379 check on-marked-down shutdown-sessions