Balance traffic across multiple origins
Spread the requests of an application across several origin addresses with a Load Balancer connector and a Rules Engine rule that sets it.
You can spread the requests of an application across several origin servers by enabling Load Balancer on a connector. A Rules Engine rule then sends the requests to that connector, and Load Balancer distributes them among its addresses with a balancing algorithm. To connect an application to a single origin, refer to Connect an application to an origin.
An account that has not migrated to API v4 configures load balancing in the legacy Origins of the application. For more information, refer to Origins.
Prerequisites
Console
- An application served by a workload. To create both, refer to Applications quickstart.
- Two or more origin servers that hold the same content and are set up the same way for the application.
- Access to Azion Console. To sign in, refer to Access Azion Console.
Plan the addresses
The example on this page balances three origin servers. Each one is hosted with a different storage provider or cloud service, because server outages rarely occur at the same time. All three hold the same content and are set up the same way for the application. Two of them share the traffic, and the third stands by for maintenance and traffic surges.
| Address | Server | Load capacity | Weight | Server role | Active |
|---|---|---|---|---|---|
example.com | Primary | High | 3 | primary | Always |
example.net | Secondary | Medium, enough for large surges of traffic | 2 | primary | Always |
example.org | Backup | Low | 1, the default when the weight is left blank | backup | Only during maintenance or a traffic surge |
The weight follows the load capacity of each server. Both example.com and example.net are primary, and the higher weight makes example.com the preferred address for connections. example.org stays inactive until maintenance or a surge needs it. For how the weight, the server role, and the active state distribute requests, refer to Load Balancer.
Create a connector with Load Balancer
Load Balancer belongs to the connector, under attributes.modules in the API. A connector created without Load Balancer keeps it disabled until you enable Load Balancer on the connector. This is the attributes.modules object of an HTTP connector the API created with no load balancing:
To balance the three addresses of the example with the Round-Robin algorithm, the connector carries these values. Each key is under attributes:
| Key | Value in the example |
|---|---|
type | http |
modules.load_balancer.enabled | true |
modules.load_balancer.config.method | round_robin |
modules.load_balancer.config.max_retries | 0 |
modules.load_balancer.config.connection_timeout | 60 |
modules.load_balancer.config.read_write_timeout | 120 |
addresses[].address | example.com, example.net, and example.org |
addresses[].modules.load_balancer.weight | 3, 2, and 1 |
addresses[].modules.load_balancer.server_role | primary, primary, and backup |
addresses[].active | true, true, and false |
connection_options.transport_policy | preserve |
connection_options.host | ${host}, which forwards the Host header of the user’s request |
In Azion Console, you set up connectors in the Connectors menu, not in a tab of the application. For each field, refer to Load Balancer and Connector settings.
Console
To create the connector in Azion Console:
Access Azion Console > Connectors.
In the General section, enter balanced-origins in Name.
In the Connector Type section, select the HTTP type.
In the Modules section, turn on Load Balancer. The connector then accepts more than one address.
In the Load Balancer Configuration section, select Round Robin in Method.
Enter 0 in Max Retries, the value for no retries.
Enter 60 in Connection Timeout and 120 in Read/Write Timeout.
In the Address Management section, enter example.com in Address and 3 in Weight. Select Primary in Server Role, and keep Active turned on.
Enter example.net in Address and 2 in Weight. Select Primary in Server Role, and keep Active turned on.
Enter example.org in Address and 1 in Weight. Select Backup in Server Role, and turn off Active.
In Transport Protocol Policy, select the option that preserves the protocol of the user’s request.
Enter ${host} as the host the connector sends in the Host header to the origin.
Azion Console shows Connector successfully created.
Send requests to the connector
A connector receives no request until a rule sets it. The rule in this section runs in the Request Phase of the application. Its criterion matches every path, with the ${uri} variable, the starts_with operator, and / as the argument, so the connector serves the whole application. Its behavior sets the connector.
The ${uri} variable works on every application. A criterion on ${request_uri} needs Application Accelerator on the application. For every variable and operator, refer to Rules Engine for Applications.
Console
To create the rule in Azion Console:
Access Azion Console > Applications, then select your application.
Select + Rule.
In the General section, enter a Name, such as balance-origins.
In the Phase section, select Request Phase. A rule keeps the phase it is created in.
In the Criteria section, set the variable to ${uri}, the operator to starts_with, and the argument to /.
In the Behaviors section, select Set Connector, then select the connector with Load Balancer in Connector.
Select Save.
The rule appears in the Rules Engine tab of the application, under Request.
For the behavior and its attributes, refer to Set Connector.
Confirm that the application answers
The rule takes a few minutes to propagate. Until then, the application answers as it did before the rule existed.
To confirm the route, send a request to the domain of your workload, with that domain in place of <your-workload-domain>:
The response comes from one of the active addresses of the connector. A workload’s domain ends in .map.azionedge.net, and the API returns it in workload_domain when it creates the workload. If the response does not come from your origin servers yet, send the request again until it does. If it never does, refer to Troubleshoot Applications.
Balance the addresses on a v3 application
An account that runs on API v3 has no connectors: load balancing belongs to an origin of the application, of the Load Balancer type. To find out which model your account runs on, refer to Verify your account migration.
Load Balancer is a setting on the application, and the addresses sit on one origin. To set both in Azion Console:
Access Azion Console > Applications > your application.
In the Main Settings tab, under Modules, turn on Load Balancer.
In the Origins tab, select Default Origin to balance the addresses the application already uses, or + Origin to add a separate one. Editing the default origin replaces the address the application was created with.
Enter a name and set Type to Load Balancer.
In Method, select the method you want. For example: Round-Robin.
In the first Address field, enter the primary server. In Weight, enter its share of the requests, such as 3 for a server with more capacity. In Server Role, keep Primary, and keep the address Active.
Select + Add Origin for each further address. A Weight left blank takes 1. An address with Server Role set to Backup receives requests only when the primary addresses fail, and an address with Active off receives none.
Select Save.
The rule that sends requests to the origin is the Set Origin behavior of the application’s Rules Engine, in place of Set Connector. For every field of the origin, refer to Origins.