Monisha Chiber
Monisha Chiber

Reputation: 71

Is it possible in haproxy to have sticky sessions based on cookie and still load balance?

So if this is the backend config:

backend main
    mode http
    balance leastconn
    cookie serverid insert indirect nocache
    stick-table type string len 36 size 1m expire 8h
    stick on cookie(JSESSIONID)       
    option httpchk HEAD /web1 HTTP/1.0
    http-check expect ! rstatus ^5
    server monintdevweb 10.333.33.33:443 check cookie check ssl verify none #web1 
    server monintdevweb2 10.222.22.122:443 check cookie check ssl verify none #web2      
    server localmaint 10.100.00.105:9042 backup #maint 
    option log-health-checks    
    option redispatch
    timeout connect 1s
    timeout queue 5s 
    timeout server 3600s

It seems it always sends all users to web1, i.e. it's not evenly load balancing using leastconn algorithm specified. I tried with stick table using src IP and that does what I want - i.e. persist sessions - but each new session should get balanced between servers. Is it not possible to have that using cookies?

Another problem with cookies I noticed was that if I were to bring down services on web1, all users get redirected to web2, but then redirected back to web1 when web1 is restored!! What real life scenario would find this behavior useful?

Upvotes: 2

Views: 9296

Answers (1)

Monisha Chiber
Monisha Chiber

Reputation: 71

I need to use the "application ID" which will help the load balancer differentiate between each user session, so it can continue to load balance requests. I am not using IIS/asp.net so that's why it didn't hit me earlier. But here is the config that works:

Change these lines:

cookie serverid insert indirect nocache
stick-table type string len 36 size 1m expire 8h
stick on cookie(JSESSIONID)

To:

stick-table type string len 36 size 1m expire 8h
stick on cookie(DWRSESSION)

Where DWRSESSION is the session ID my application uses.

Upvotes: 4

Related Questions