HAProxy and “sharding”

haproxyscaling

I am cross posting this from stack overflow since it was suggested I might get a better answer here …

I was wondering if anyone has used HAProxy for sharding. Specifically, I would like to be able use a cookie I define w/a identifier and have any client that presents that cookie/identifier combination be directed to the same server. It seems possible and my preliminary tests seem to work but I am not a sure.

My listen configuration looks like:

listen  appli1-rewrite 0.0.0.0:80
cookie  mysession= prefix
balance roundrobin
server  app1_1 192.168.0.1:80 cookie server01
server  app1_2 192.168.0.2:80 cookie server02
option  httpclose

However, when I look at the request headers using Live Http Headers I don't see the mysession cookie indicating which server it will be going to, which is what I expected based on the prefix operation. I am quite new to HAProxy so any help/guidance would be great!

Best Answer

first, remove the equal sign after your cookie name, otherwise it will never match. Second, please ensure that your server correctly sets the "mysession" cookie, as the cookie prefix mode relies on the server to set the cookie. If it does not, use the insert mode instead. Third, please add "option httpclose" in your config, because if there is any sort of keepalive, haproxy won't see the cookie (or will not be able to mangle it).

Hoping this helps,
Willy