Load Balancer
Introduction
The Load Balancer is an element of the K2 architecture that can be used to distribute the load among multiple application servers. K2 API can thus connect to multiple application servers at the same time. When a request is sent, the least loaded server is determined first, and that server receives the request for processing.

Picture: Load Balancer and K2 API
Example of using the Load Balancer and K2 API:
K2 API receives a request for processing. For the fastest processing of the request, the Load Balancer is contacted. The Load Balancer sends a request to all AS and determines their current load. It evaluates the least loaded server and passes this information to K2 API. K2 API then sends the request directly via the application server selected by the Load Balancer.
The LoadBalancer can also be used for Web K2, which finds the most suitable application server when a user logs in and connects the user to it.

Picture: Load Balancer and Web K2
Example of using the Load Balancer and Web K2:
Web K2 receives a user login request. To process the request as quickly as possible, Web K2 contacts the Load Balancer. The Load Balancer sends a request to all AS and determines their current load. It passes the information about the least loaded server to Web K2. Web K2 uses the best-evaluated server, so the user login request is processed as quickly as possible. Every further request of the given user is then processed by the specific selected AS.
Each installation of K2 API and Web K2 has its own Load Balancer. In the installation, it is thus possible to set a specific list of application servers as well as the Load Balancer configuration, with which we can influence the algorithm for selecting a suitable application server.
The Load Balancer evaluates the most suitable application server for processing a request in two modes:
- Loading and evaluating the best AS before each connection request (preLoadingEnabled=false)
- Before each request to create a connection, the application servers are queried for their statistics, which are then processed and evaluated. The most suitable AS is then used for the given request.
- Loading and evaluating the best AS continuously (preLoadingEnabled=true)
- Statistics are loaded and the best AS is evaluated at a specified time interval. Each connection request is evaluated based on the precalculated result of the statistics.
Load Balancer parameters
- requestTimeout:(default 1s) - the time within which the AS must send its statistics. If a connection with the AS cannot be established within this time, or the AS does not respond within this time, the AS is left out of this particular evaluation. On the next request, it is included in the list of possible application servers again.
- validTimeout: (default 0 for preLoadingEnabled=false, 1000 for preLoadingEnabled=true) - the time for which the obtained statistics, and thus the best AS, are valid.
- maxHistoryCount:(default 0) - the number of records in the history. If the number is 0, the history is not recorded. It is used only for tuning the optimal parameter settings, when we can look into the history of request distribution and adjust the parameters accordingly.
- preLoadingEnabled:(default false) - switches the mode with continuous loading of statistics on/off. If it is set to true, validTimeout means the time interval in which the statistics are updated.
- the appServers section defines the application servers that will be used by the Load Balancer. The default is the one used in the K2 API/Web K2 configuration
- E.g. for K2 API (Web K2 is similar, only the configuration is done in JSON) "<add server="." pipe="ScriptService"/>"
- server: IP or PC name
- pipe: AS pipe name
- the multiplier section is used to set the weight of individual statistics and thereby influence the calculation algorithm
- E.g. for K2 API (Web K2 is similar, only the configuration is done in JSON) "<add uid="bscSystemCPUAvg" value="1"/>"
- uid: statistic identifier
bscSystemCPUAvg - average CPU usage over the monitored time
bscSystemFreeMemory - current free system memory (this is the only counter for which a higher value is better, i.e. <add uid="bscSystemFreeMemory" value="-1"/>).
bscAppPoolSpandAvg - average time a request waits on the K2 AS before it is served
bscAppNCLSessions - number of connected Web K2 users
bscAppAnonymousSessions - number of anonymous threads for K2 API
bscAppSchedulerSessions - number of running scheduled tasks
- value: the value by which the statistic value received from the AS will be multiplied. This makes it possible to influence the weight of the given parameter.
The value 0 switches off the evaluation of this parameter in the calculation.
If, in either mode, the Load Balancer does not connect to the AS within the required time interval, or the AS does not deliver statistics within the specified time, the AS is excluded from the evaluation for the given pass. In the next pass, the Load Balancer will try to connect to the given AS again.