I have a requirement to route using consistent hash algorithm based upon fields within the request, i may need to customize the HAProxy routing to accomplish this. Can route using consistent hash algo and fields within the request.
Maybe balance hash <expr>
is what you’re looking for?
https://docs.haproxy.org/dev/configuration.html#4.2-balance
You may use intermediate variables to process your own id for each request, and then tell haproxy to use it for routing decision using the hash
balance algo:
balance hash var(txn.my_var)
hash-type consistent # automatically hashes input with default sdbm func
http-request set-var-fmt(txn.my_var) "%[req.hdr(test)] %[req.hdr(test2)]"
You may also manually hash the id using the hashing function of your choice if needed (since 2.9):
balance hash var(txn.my_var),xxh32
hash-type consistent none # don't hash input, user already hashed it
http-request set-var-fmt(txn.my_var) "%[req.hdr(test)] %[req.hdr(test2)]"
https://docs.haproxy.org/dev/configuration.html#4.2-hash-type
Can HAProxy be used to route specifically based upon incoming requests?
balance hash var(txn.my_var) hash-type consistent # automatically hashes input with default sdbm func http-request set-var-fmt(txn.my_var) “%[req] %[req]”
you need to be more specific about what you’re interested in the request: headers? uri? client ip? body? There are plenty of fetches that may help you to build your custom ID for each request.
List of http request-oriented fetches may be found here: https://docs.haproxy.org/dev/configuration.html#7.3.6
(search for req.* fetches)
However please note that not all fetches will work for balance “hash” (during early request handling), you should avoid relying the http payload.
I want to use consistent balance hashing with the request body. Is this possible?
It will have performance implications and limitations, but I think this could be achieved with the help of option http-buffer-request:
option http-buffer-request
hash-type consistent
balance hash req.body