Enabling Load Balancing on your server - marcos8154/SocketAppServer GitHub Wiki

Starting with version 1.4.8 of the framework, it is possible to enable load balancing, raising a front-server to listen to all clients, and adding sub-servers below so that requests are distributed.

IMPORTANT: it is necessary that both the front-server and the sub-servers are running version 1.4.8 or higher

First step: create the front-server

Create a project for the front-server and in the Main() method add the following code:

    Server server = new Server();
    server.Port = 4000;
    server.BufferSize = 400000;
    server.ServerEncoding = Encoding.UTF8;

    server.EnableLoadBalanceServer(2)
        .EnableDynamicInstanceAllocationManagement(new SubServerArmer(), 1)
        .AddSubServer("localhost", 4040, Encoding.UTF8, 4096, 3, 2);

    server.Start();

The EnableLoadBalanceServer() method

This method is responsible for making the Server object a load balancer. See your signature below:

public LoadBalanceConfigurator EnableLoadBalanceServer(int maxAttemptsToGetAvailableSubServer = 3,
            bool cacheResultsForUnreachableServers = false)
  • maxAttemptsToGetAvailableSubServer: Determines the number of attempts to recover an available server in the pool
  • cacheResultsForUnreachableServers: Enables the request result cache for the Action x Controller x Parameters set if none of the servers are available

The EnableDynamicInstanceAllocationManagement() method (optional)

This method enables a mechanism that will notify you when it is necessary to start more instances of sub-servers, and also when to stop them (when they are no longer needed)

The method requests an instance of the implementation of "INotifiableSubServerRequirement", along with a parameter to determine the lifetime of each dynamically allocated sub-server

See your signature below:

public LoadBalanceConfigurator EnableDynamicInstanceAllocationManagement(INotifiableSubServerRequirement notifiable,
            int serversLifetimeInMinutes = 10)
  • notifiable: An instance of a class that implements "INotifiableSubServerRequirement", legalized in the namespace "MobileAppServer.LoadBalancingServices"
  • serversLifetimeInMinutes: Determines the lifetime (in minutes) of each dynamically allocated sub-server

The AddSubServer() method (required)

In this method you must inform the basic connection parameters with each of the sub-server instances, according to your signature below:

 public LoadBalanceConfigurator AddSubServer(string address, int port, Encoding encoding,
            int bufferSize, int maxConnectionAttempts, int acceptableProcesses)

IMPORTANT: each sub-server added using the AddSubServer () method is FIXED in the pool and has no time to expire

Several parameters above do not need a description, after all their names are already expressed on their own. But there is one in particular that deserves our explanation:

  • acceptableProcesses: This parameter is the most important, as it determines the acceptable process limit for the sub-server in question to accept new requests.

A little more about INotifiableSubServerRequirement

To allow the dynamic allocation of new sub-servers, as well as their interruption, you must create a class in your project that implements the "INotifiableSubServerRequirement" interface. It contains 2 methods that are described below:

public class SubServerArmer : INotifiableSubServerRequirement
{
    public SubServer StartNewInstance()
    {
        /*
            Define here your logic responsible for physically 
            starting a new instance of a sub-server, 
            either on the same host or on a remote host
        */

        //Returns data for connection to the newly started sub-server
        return new SubServer("Win7-PC", 4040, Encoding.UTF8, 4096, 3, 2);
    }

    public void StopInstance(SubServer server)
    {
        /*
            Define your logic here to stop the sub-server instance, 
            either on the local host or on a remote host
        */
    }
}

The StartNewInstance() method will be requested whenever there are no available sub-servers in the pool, while the StopInstance() method will be invoked automatically as soon as the lifetime of the dynamically allocated sub-server expires.

Concluding

After making these settings, you are ready to use your load-balancing front-server. Now just point your clients to this server, and it in turn will distribute requests among the sub-servers. And if there are no more servers in the pool to meet the volume of requests, you will be asked to create new instances (if enabled)