HAProxy community

Custom load balancing



I am using HAProxy for load balancing my HTTP requests. I would like to know if there is any way to customize the selection of backend server based on the responses returned by each server. I have a servlet which can return the responses (number of clients connected to it). I would like to use this information and route the request to the backend server which has the lowest number.

My HAProxy configuration looks like:

listen http_front xx.xx.xx.xx:8080
  mode http
  option httpchk GET /servlet/IHealthCheckServlet
  server app1 xx.xx.xx.xx:8080 check port 8080
  server app2 xx.xx.xx.xx:8080 check port 8080
  server app3 xx.xx.xx.xx:8080 check port 8080

Can you help me in this regard.


Well you can just use the leastconn algorithm to do this, its just a single option:

I suggest you work with existing load balancing algorithms haproxy provides. You have a lot of options and should be able to find what you require easily (as I said leastconn probably already satisfies your requirement).

Custom application controlled load balancing can be complicated and cumbersome - a delayed reaction to high load may actually have the opposite effect and self-DDoS your backend servers.

However, yes, with the appropriate load-balancing algorithm you can shift the load by adjusting server weights. You can do this via the admin socket, or better yet with auxiliary agent checks:


You may even be able to intercept HTTP responses with LUA and set appropriated server weights.