Tuesday, April 16, 2024
HomeSample Page

Sample Page Title

On this article we’re going to discover a number of methods to make use of the open supply HAProxy load balancer along side SFS2X, to extend the scalability and availability of a multiplayer challenge.

We’re going to present totally different configurations for TCP and Websocket connections and several other methods to setup the system for widespread use circumstances.

To get essentially the most out of this tutorial we require a fundamental information of what a Load Balancer is and the way it works, and a few familiarity with the fundamentals of networking and the OSI Mannequin.

» Introducing HAProxy

Within the phrases of its creators, HAProxy is a free, very quick and dependable resolution providing excessive availability, load balancing, and proxying for TCP and HTTP-based purposes.

This software program is especially well-known within the Linux group and it’s a a well-liked alternative for a lot of giant site visitors web sites. It may well function each at layer 4 (TPC) and layer 7 (HTTP) of the OSI Mannequin so it’s completely appropriate to load steadiness a multiplayer service resembling SmartFoxServer 2X.

Within the subsequent sections we’re going to put in and setup HAProxy on a devoted Linux machine to behave as a Load Balancer for a number of SFS2X situations, supporting each TCP and Websocket shoppers.

» Spherical robin Load Balancing

In our first setup we purpose at operating quite a lot of SFS2X situations behind a single HAProxy and let it steadiness the incoming shopper site visitors. All customers will level to the balancer’s IP handle (or area) which it’ll deal with passing the connection to one of many SFS2X situations.

Specifically this can be performed utilizing the straightforward Spherical Robin algorithm, that distributes the connections throughout all situations evenly.

To get began we launched a brand new Ubuntu machine in AWS EC2 and put in HAProxy with this command:

sudo apt set up haproxy

After a couple of seconds the load balancer needs to be up and operating. We are able to confirm it with this:

service haproxy standing

Earlier than we dive into the HAProxy configuration we additionally must setup couple of SmartFoxServer 2X situations. We’ll skip over this part as it is best to already be accustomed to this straightforward course of and assume they’re already arrange.

For our instance we assume the 2 SFS2X servers have personal IP addresses of and

» Configuring HAProxy

We are able to now proceed by modifying the HAProxy’s configuration discovered beneath /and many others/haproxy/haproxy.cfg

For instance:

sudo nano /and many others/haproxy/haproxy.cfg

HAProxy has an unlimited variety of settings however is comparatively easy to configure and the default haproxy.cfg file is fairly minimalistic. Basically there are 4 principal sections within the configuration, known as:

  • globals: process-wide settings for efficiency and safety
  • defaults: default values that you could be or could not override within the subsequent sections
  • frontend: defines the interface with the shoppers, resembling listening ports
  • backend: describes the servers which are accessible for HAProxy to load steadiness

Whereas the globals and defaults part seem solely as soon as within the .cfg file, there could be a number of frontend and backend sections. For instance we might bind a number of ports on the frontend for various providers, resembling TCP socket and Websocket. Equally we are able to outline a number of backed sections that map the 2 frontends to their respective servers.

Let’s check out the configuration we’ve used for this setup to make clear what described up to now.


        log /dev/log    local0
        log /dev/log    local1 discover
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 degree admin expose-fd listeners
        stats timeout 30s
        consumer haproxy
        group haproxy
        maxconn 50000

        # Default SSL materials places
        ca-base /and many others/ssl/certs
        crt-base /and many others/ssl/personal

        # See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediate
        ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM->
        ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
        ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets

What you see right here is 99% the usual settings from the default config file, we simply added the maxconn parameter to permits a excessive variety of incoming connections.

For the second we are able to ignore the remainder of the settings and transfer on to the subsequent part.


        log     world
        mode    tcp
        timeout join 5000
        timeout shopper  50000
        timeout server  50000
        timeout tunnel  180s

The mode tcp command tells HAProxy that by default incoming connections needs to be handled as layer-4 TCP connections, quite than layer-7 HTTP ones. We’ll return on this matter in a second.

The varied timeout instructions management the client-to-balancer and balancer-to-server timeouts and could be modified as most well-liked. The final timeout tunnel is restricted for Websocket connections, as soon as once more regulating the size of an idle connection.

Please be aware that except in any other case specified, values are interpreted as milliseconds. In various you’ll be able to add an “s” for seconds, “m” for minutes and “h” for hours.


On this part of the .cfg file we are able to outline two totally different frontends: one for the default TCP port 9933 and one for the Websocket site visitors, on port 8080.

frontend sfs-socket
        bind *:9933
        default_backend servers-socket

frontend sfs-websocket
        mode http
        bind *:8080
        default_backend servers-websocket

The very first thing to notice is that we don’t outline a mode within the sfs-socket frontend as we’ve already declared mode tcp within the earlier default part.

Nevertheless we do declare mode http within the sfs-websocket frontend declaration to override the defaults.

Lastly the default_backend command is referring to the backend blocks that we’re about to outline.


Right here we declare the 2 referenced backed gadgets:

backend servers-socket
        steadiness roundrobin
        server sfs1
        server sfs2

backend servers-websocket
        mode http
        steadiness roundrobin
        possibility  forwardfor
        server sfs1 test fall 3 rise 2
        server sfs2 test fall 3 rise 2

The servers-socket block defines the targets of TCP load balancing, utilizing the usual SFS2X TCP port worth (9933).

Please Notice: when defining the server endpoints we at all times use the personal IP addresses of those machines, as load balancer and relative nodes should talk in a quick, low-latency personal community.

The server-websocket block is the place we outline the Websocket endpoints, utilizing the default 8080 port.

The possibility forwardfor directive tells HAProxy to go the authentic shopper IP handle to the endpoint by including an X-Forwarded-For (XFF) entry to the HTTP header file. This in flip is supported by SmartFoxServer by enabling it within the AdminTool > Server Configurator > Net Server

Lastly the test fall 3 rise 2 parameters within the servers definition prompts the Load Balancer’s well being test system, specifying what number of checks are required to find out whether or not a server is lively or not.

  • fall 3: signifies that after 3 consecutive failed checks the server is excluded from the load balancing pool
  • rise 2: signifies that after 2 consecutive profitable checks the server is added to the load balancing pool

» Caveats

Shopper IPs in TCP mode

When operating in TCP mode there isn’t any manner for HAProxy to inform SmartFoxServer what’s the authentic IP handle of the shopper, due to this fact all customers connecting by way of TCP will seem to have the IP handle of the Load Balancer.

Whereas HAProxy is aware of the unique IP of the sender it has no clue in regards to the content material of the TCP packets it’s receiving and due to this fact there isn’t any solution to inject that data someplace within the information stream.

That is solely doable when working in HTTP mode.

Different load balancing algorithms

Spherical-robin is one among many doable load balancing techniques that can be utilized with HAProxy. Relying on the mode in use (tcp vs http) it’s doable to select from a large record of algorithms.

You may be taught extra about what is out there within the HAProxy documentation.

SSL Certificates

If it is advisable to activate HTTPS for Websocket or TCP encryption you’ll need to carry out additional configuration steps. Specifically you’ll need to deploy your SSL certificates on the Load Balancer, since it’s the entry level for all your shoppers.

It’s past the scope of this tutorial to stroll you thru the HAProxy SSL setup course of, however you’ll be able to learn all the small print within the glorious documentation supplied by on their web site.

What about UDP?

HAProxy doesn’t help UDP load balancing, so the answer introduced right here wouldn’t be work with an SFS2X challenge that requires it.

Doable alternate options might be:

  • IPVS a Linux-only, kernel-based load balancing software
  • Nginx one other well-liked proxy/load balancer/net server

Single level of failure

As you will have most likely realized if all of the shopper site visitors goes by way of one Load Balancer this may develop into a single level of failure. How can we stop a service blackout if the HAProxy turns into unavailable?

Usually we would wish to run an lively HAProxy and a number of passive replicas (i.e. with precisely the identical configuration) that may take over at any time when it’s essential.

Additionally these servers would must be behind a Digital IP handle which might be pointed to a special machine when essential, making the transition from a failed Load Balancer to an lively one absolutely clear.

Should you’re fascinated by studying extra about this system you’ll find the small print on this article by Oracle.

» Backup servers

An attention-grabbing function in HAProxy is that of backup servers, that are situations outlined within the backend part of the config that aren’t added to the load balancing pool till it turns into empty.

Let’s return to our websocket setup as an example:

backend servers-websocket
        mode http
        steadiness roundrobin
        possibility  forwardfor
        server sfs1 test fall 3 rise 2
        server sfs2 backup test fall 3 rise 2

Right here we’ve barely modified the configuration by including a backup key phrase to the sfs2 occasion (final line). What does it do?

By marking this server as backup the Load Balancer will now not distribute shoppers among the many two situations however quite preserve sending customers to sfs1 till it turns into unavailable.

When this occurs the sfs2 occasion turns into lively and stays like that till one ore extra non-backup servers are added to the pool. For instance when sfs1 is restarted and turns into useful once more.

This straightforward possibility could be helpful for various eventualities, resembling operating a giant Foyer server performing because the entry level for customers, who can then be despatched to different servers to play video games. The one Foyer server might be run behind an HAProxy and with a backup Foyer able to take over when essential.

Right here’s an hypothetical structure for a cluster that mixes what we simply described with the earlier setup:

We now have a Foyer Balancer as the principle entry level for our utility: right here customers will login, handle their profile, seek for different customers, create and handle their Buddy Lists and chat with them.

The Sport Balancer as a substitute will direct gamers to totally different recreation servers the place they are often matched with different customers and play.

Lastly we’ve a principal database, shared amongst all servers, that may be consumer to trace lively customers, entry their state, recreation statistics, leaderboards, buddy lists and extra.

» Wrapping up

The subject of excessive availability and scalability in clusters is a big topic and we’ve barely scratched the floor. We could also be returning on this matter sooner or later with extra articles however, for now, we hope to have supplied sufficient ideas to get you began experimenting.

As ordinary, in case you have any feedback, questions or doubts tell us by way of our SmartFoxServer help discussion board.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments