Haproxy as Load Balancer – some details as we have explored

We had written about load balancing with nginx in our previous article.

Now in this article we will try to discuss some of the load balancing schemes in haproxy and their configuration in web applications.

Haproxy server can be used to distribute the load balancing of web request to different web/ application servers.

The main 2 sections in haproxy are –

  • frontend – here Haproxy listens to connections
  • backend – HAPoxy sends incoming connections

The Load Balancing Schemes –

Round Robin – Nginx send the web request to different servers in order they are defined in the nginx configuration file.

Example Configuration  –

backend servers
mode http
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1\r\nHost:localhost
server server1 127.0.0.1:3000 check
server server2 127.0.0.1:3001 check

In above only the DNS entries are to be inserted in backend section with a name mentioned, in our case it 
is servers. We attached the servers here -

    server server1 127.0.0.1:3000 check
    server server2 127.0.0.1:3001 check

We can use weight for any server, so that, if we put weight 2 for server1, then for every 3 requests 2 
requests will go to server1 and rest to server2.

When haproxy is used with default session handling (i.e. to sending all requests from a client to be sent to same server) , it is needed to add

cookie COOKIE-NAME prefix directive into the backend. The cookie directive is to be added for all servers where sessions are to be used. Here haproxy will add unique identifier for all subsequent requests from same client. It will look like –

    cookie SRV_ID prefix
    server server1 127.0.0.1:3000 cookie check

leastconn -

Here server with lowest no of connections will receive the connection.

Example Configuration  –

backend servers
mode http
balance leastconn
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1\r\nHost:localhost
server server1 127.0.0.1:3000 check
server server2 127.0.0.1:3001 check

Haproxy is having a web interface which will run in 1936 port for default configuration, where we can view 

status of web/app servers which are under load balancing mode. For our case the web console is like -

Screenshot from 2015-01-07 09:45:40

Where in frontendservers section, the haproxy information is showing and in servers section server1 and 
server2 are running. We have just started to explore haproxy for load balancer, we will update more here, 
when we have more details.  

For more details about those load balancing settings, we should refer to haproxy documentation.

Reference : Load Balancing with Haproxy

If you find this article helpful, you can connect us in Google+ and Twitter for other updates.

Leave a Reply

Your email address will not be published. Required fields are marked *