0

I need to set up a HAProxy to work in a following way. Let's say I have only 10 users and 10 backend servers. My goal is to stick each user to his own server based on custom http header user_id. It means, that user's requests with header user_id=1 will be sent only to server1 (or any idle server) and no one is able to connect to this server anymore until connection is closed. I tried to test with such configuration

defaults
  mode http
  option http-server-close
  option http-buffer-request
  timeout client          25s
  timeout connect          5s
  timeout server          25s
  timeout http-keep-alive 60s
  timeout http-request    15s

frontend http-in
  bind *:5000
  default_backend servers

backend servers
  balance hdr(user_id)
  option httpchk GET /check HTTP/1.0
  server server1 localhost:5001 check
  server server2 localhost:5002 check
  server server3 localhost:5003 check

Then I use

curl -XPOST --header "user_id=1" http://127.0.0.1:5000

but this balancing works like roundrobin and requests are not being sticked to one server. The balance source is not an option in my case, because users can use the same ip address. Summarizing: my objective is to balance requests based on custom user_id header that any unique user_id will use the same own backend server each time. Can anyone help me?

jahra
  • 141
  • 5
  • I'm sorry, but it was a typo in `curl`. The legal way is to call `curl -XPOST --header "user_id: 1" http://127.0.0.1:5000`. Everything works like a charm! – jahra May 28 '18 at 13:35
  • 1
    You have not tested this fully, because it does not do everything you specified. Other users will eventually connect to the server at the same time, because the balancing algorithm is hash-based, and there is no reason to assume the result will be even distribution with such a small number of inputs. `balance hdr...` does not have a concept of "or any idle server." For any input, there is one deterministic output. Any value *y* that hashes the same as *x* will *always* go to the same server, until the number of available servers changes, which may redistribute the hashing. – Michael - sqlbot May 28 '18 at 22:33
  • @Michael-sqlbot Thank you for reply. Yes, after some testing I understood that is not fully what I need. Maybe you have ideas how can I achieve the desired behavior? – jahra May 29 '18 at 09:35
  • 1
    Depending on your motivation, just set `maxconn 1` on each server. Servers that are already handling one request won't be sent any more. – Michael - sqlbot May 29 '18 at 18:25
  • @Michael-sqlbot Will `maxconn 1` work when users come from the same ip address? – jahra May 29 '18 at 18:34
  • 1
    Setting [`maxconn`](http://cbonte.github.io/haproxy-dconv/1.6/configuration.html#5.1-maxconn) on a `server` sets the maximum number of concurrent connections for that server, regardless of source. – Michael - sqlbot May 29 '18 at 18:36
  • @Michael-sqlbot thank you for the answer. You can post it as answer, it will be helpful. – jahra May 29 '18 at 18:38

0 Answers0