Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to load balance web requests with HAProxy. The catch is that I want particular groups of users to always go to the same server. (This is a multiplayer system, and users in the same group need to be on the same server). The users can go to any server, as long as the users with the same group id are on the same server. The server can even change (for example if a server goes down) but then all users of that group must switch to a new server. The app server will identify the user's grouping with a cookie.

As a specific example, suppose my application has a cookie GROUP_ID numbered 1..10000. There might be 3 different users with GROUP_ID=1, a different 3 users with GROUP_ID=2, etc. And let's assume I set up load balancing on servers: app1, app2, app3.

What I'd like to see is something similar to this:

  • all GROUP_ID=1 users go to app1
  • all GROUP_ID=2 users go to app2
  • all GROUP_ID=3 users go to app3
  • all GROUP_ID=4 users go to app1
  • all GROUP_ID=5 users go to app2

etc.

If server app1 becomes unresponse, then I want to see the app1 users redistributed among app2 and app3. (It'd be ok if all groups were reshuffled). All users from a particular group should still be on a common server.

Obviously the initial request to the site will not have a cookie, but once the user authenticates they will be assigned the cookie.

Is this kind of custom load balancing supported by HAProxy?

share|improve this question

1 Answer 1

up vote 1 down vote accepted
+150

1) Setup a HAPROXY frontend, with:

  • a dedicated backend for your authentication (login into the game). When you authenticate, you set a cookie called 'GROUPID'. (ie: GROUPID=GID3) for that http connection. (note: you can also pass that 'groupid' as an argument to an URL and/or set expiry time, etc..)

  • a dedicated backend for your game servers, with the following directive:

    cookie GROUPID insert indirect nocache

2) setup your servers (in the backend) as (note: check section can be removed):

server APP01 10.0.0.5:80 cookie GID3 check inter 5000 rise 2 fall 3 maxconn 2048

server APP02 10.0.0.6:80 cookie GID4 check inter 5000 rise 2 fall 3 maxconn 2048

(...)

For further reference, the documentation of HAPROXY is quite explicit (http://haproxy.1wt.eu/download/1.4/doc/configuration.txt)

share|improve this answer
    
that's terrific. thanks. –  Will Glass Mar 6 '13 at 17:48
    
so I ended up using the new "stick-table" features in 1.5 which is much more powerful. This hashes the cookie and always keeps the users with the same cookie together. You can use the "show table" command to see all the mappings. –  Will Glass Apr 1 '13 at 8:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.