Haproxy load balancing with HTTP Headers

We're using HAProxy to load balance our websocket and comet application. There is one HAProxy and 3 applications running in the back. We use HTTP Basic Auth (and we will use other types of auth in the future, like OAuth) to identify the connected user.

We can't use regular round robin, because it is important that connections for a given user end up on the same server. It doesn't matter where, but it's important that they use the same server.

I know HAProxy as a smart cookie based approach, but unfortunately, in our case, sometimes, the same user will be connected from different machines, so the cookie would not work).

Is it possible to use a hash of the HTTP Authorization header to do the load balancing? Also, and this is more complex, is it possible to define our own hashing method?

Follow up question [after Willy's response] :

Since it's possible to do balance hdr(Authorization), would it be possible to do it only on the login field (after parsing)?

Also, what is the exact algorithm? I believe it's in the form of hash(Authorization) % number_of_backend_server, but then, what is the hash function, and more importantly, is it tweakable? (or can I add an offset).

I'm asking because this app is actually a keeping an XMPP Component connection open and that I need to load balance the XMPP components with the same algorithm. The to jid in the XMPP stanzas is determined by the HTTP Header Authorization.


Solution 1:

Yes you can very well hash any header of your choice. Just use "balance hdr(header_name)". I must say I had never thought about doing it that way. In some environments, it can indeed be quite useful. You can also hash on a URL parameter or a POST parameter if that can help.

You can't define your hashing method however. You can select if the hash is map-based or consistent but that's all.