Load Balancing with HAProxy

HAProxy is free software that functions as a load balancer and reverse-proxy server for TCP and HTTP-based applications. HAProxy distributes connection requests across multiple server nodes. With very little resource usage, it enables you to handle huge volumes of HTTP and HTTPS traffic. Moreover, HAProxy performs server health checks and forwards users to functioning node in case one of the servers is down, which makes it a helpful failover tool.

Assuming forementioned benefits, we configured HAProxy to distribute connections for MySQL RDBMS and our user interface.

In this article, we will consider steps to perform load balancing for three DataSunrise proxies in High Availability mode at the following addresses:,,


There are several steps to perform before starting to work with HAProxy.

Before configuring load balancing, you need to install DataSunrise on high availability mode on these three servers. To implement High Availability mode on DataSunrise, perform the following:

  1. DataSunrise installation wizard allows you to select the local or remote location for a configuration tab. Select Remote.
  2. At the DataSunrise Server details tab, specify the details of the current DataSunrise instance: server name, hostname where DataSunrise instance is installed, port number of the web UI of the instance (11000, by default).
  3. At the Dictionary location tab, specify the database to store DataSunrise configuration (Dictionary). All servers configured to use this database will share a common configuration (including common credentials to access web UIs).

Refer to DataSunrise administrator user guide for more detailed instructions on high availability mode of DataSunrise. Also ensure the same version of product is installed on all servers.

Next we need to create a DataSunrise instance on the virtual host where MySQL is installed and open proxies on machines that will connect to the MySQL database.

To create an instance, perform the following on the master machine:

  1. Enter DataSunrise GUI and go to Databases subsection of the Configurations section.
  2. Click the Database + button.
  3. Enter the hostname, port number and other required information.
  4. Once the instance is created, select newly created instance from the list and click Edit.
  5. A window with instance configurations will open, click the Proxy + button.
  6. Specify the server, hostname and port number (mysql0,, 3306 correspondingly) and click Save.
  7. Open proxies on slave machines at the same instance specifying corresponding server name and hostname for each proxy.

At the end, the instance configuration will look like this:


Configuring HAProxy for MySQL

Configuring HAProxy for database management systems is pretty simple. Below we will configure HAProxy to function as a connection distributor for DataSunrise proxies (,,

After installing HAProxy open the mentioned above /etc/haproxy/haproxy.cfg file and edit the ‘listen’ configuration block as follows:

listen af_mysql_balancer
        mode tcp
        bind *:3306
        balance leastconn
        server mysql0 check
        server mysql1 check
        server mysql2 check

HAProxy will perform health checks and distribute connections according to the number of connections on the server node, forwarding the user to the server with the least number of connections.

balance <algorithm> is for selecting balancing algorithms.
roundrobinRound Robin is the default algorithm, it selects servers in turns.
leastconnWith this algorithm, HAProxy directs each new user to the server with the least number of connections.
sourceThe algorithm to direct users to servers based on a hash of the user’s IP address.
For the complete list of balancing algorithms refer to HAProxy Configuration Manual.

In case the server currently used by a user is down, the connection will be closed. The user will have to reconnect and HAProxy will open a new database connection to the server that is up and has the least amount of connections.


All the other database management systems can be configured the same way, only ports need to be changed.

Configuring HAProxy as a load balancer for DataSunrise backend service (GUI)

In this section, we will consider steps to perform load balancing for three DataSunrise proxies in High Availability mode at the following addresses:,,

Install HAProxy on a separate server and open /etc/haproxy/haproxy.cfg with any text editor. Specify the listener configuration in the following way:

listen af_gui_balancer
        timeout client 50000
        timeout server 50000
        mode http
        bind *:11000 ssl crt /home/anon/proxy.pem
        redirect scheme https if !{ ssl_fc }
        cookie HA_BACKEND_ID insert
        balance leastconn
        server node0 ssl verify none check cookie 0
        server node1 ssl verify none check cookie 1
        server node2 ssl verify none check cookie 2
  1. timeout client <timeout> – set the maximum user inactivity time period after which the connection will be closed (milliseconds).
    timeout server <timeout> – set the maximum server inactivity time period after which the connection will be closed (milliseconds).
  2. mode { tcp|http } – defines the protocol of the instance. In our case, the balancing will be performed at the HTTP protocol level. The client request will be analyzed in depth before connecting to any server. Any request which is not RFC-compliant will be rejected. Layer 7 filtering, processing and switching will be possible.
    HAProxy can balance at TCP level as well, but specifics of web applications having a user session condition makes it hard to perform proper load balancing with no opportunity to analyze or modify certain parts of HTTP queries.  We use http mode because we need HAProxy to assign and read persistence cookie that will enable HAProxy to determine the server which the user must be directed to. Thus, the web-server will correctly operate with the user session.
  3. bind [<address>]:<port_range> – bind option defines one or several listening addresses and ports in a frontend. <address> can be a host name, IPv4 address, IPv6 address or ‘*’. ‘*’ means that the port will be open in all available IP-addresses.
    ssl crt /home/anon/proxy.pem – The ssl option enables SSL deciphering which requires certificates. Certificates and keys will be taken from the /home/anon/proxy.pem file. System users must have read-access to the file. The pem file content should be in the following format:
    -----END CERTIFICATE-----
    -----END RSA PRIVATE KEY-----
  4. redirect scheme https if !{ ssl_fc } the following configuration forwards all plain text HTTP connections on port 11000 to HTTPS scheme. Thus, users will not be able to use the administration service without encryption.
  5. cookie HA_BACKEND_ID insert. The given option defines the persistent cookie. After the first request, HAProxy sends cookie to the user. According to the cookie value HAProxy will determine which server node must be used for subsequent queries of the user.
    HA_BACKEND_ID is the cookie name.
    The insert option defines that HAProxy will assign those cookie values after the first query. The persistent cookie value is used until the user clears browser cookies or ends the session.
  6. The last three lines define instances.
    node0 is an arbitrary name of the instance used only for identification in terms of HAProxy. – the address and port of the instance.
    ssl – defines that the instance accepts https connections.
    verify [none | optional | required]. By default, the setting is set to none, which means that client certificate will not be requested.
    The check option enables the health checking mechanism. Before forwarding the client query to the node, HAProxy checks whether the node is available.
    cookie <value> – the parameter sets the cookie value assigned to the server.

Save the configuration changes and relaunch HAProxy. As a result, we will have the configuration enabling HAProxy to function as follows:

  • A user connects to HAProxy.
  • HAProxy performs health check of the servers.
  • HAProxy forwards the user to the server node with the least load, which actually balances the load.

Multiple instances can be added if required. HAProxy operates on Linux and Solaris. For Windows operating system try more advanced Nginx solution.