How to configure multiple origins with load balancing algorithms
When you create an application, you need to define an origin. However, more complex and robust infrastructures may need multiple origins for the same application. The Load Balancer feature allows you to use load balancing algorithms to manage high access numbers and control the behaviors of your origin.
There are separate instructions for API v3 using legacy Origin settings and API v4 using the new Connector product.
The following steps will walk you through a hypothetical scenario where the default origin should be of the Load Balancer-type with the Round Robin algorithm. Feel free to interpret and modify the step-by-step instructions according to your specific use case.
The following business rules of different origin servers must be configured with load balancing:
- The application should have three origins.
- The primary server should have a higher load capacity.
- The secondary server should have a medium load capacity, enough to handle large surges of incoming traffic.
- The backup server should have a low load capacity and only be active in special circumstances.
- Each of these servers should be hosted in a different storage provider or cloud service since server outages don’t often occur simultaneously.
- All origins must contain the same content and be set up in the exact same way for the application.
Therefore, the setup would be:
Origin | Role | Address | Load capacity | Status |
---|---|---|---|---|
1 | Primary server | example.com | High load capacity | Should always be active. |
2 | Secondary server | example.net | Medium load capacity | Should always be active. |
3 | Backup server | example.org | Low load capacity | Should only be active in case of maintenance or traffic surges. |
Now you need to configure a new Connector to link your application to your Object Storage bucket. To do so:
- Access Azion Console > Connector.
- Click the + Connector button.
- In the General section, give your connector a unique and descriptive name (for example,
Multiple Origins Connector
). - In the Connector Type section, select HTTP.
- Enable the Load Balancer switch, this enables you to add multiple addresses.
- In the Method field, select Round-Robin.
- In the first Address field, specify the primary server. For example,
example.com
. - Since this origin has a higher load capacity, in the Weight field, add a weight of
3
to this first origin. - In the Server Role field, keep Primary selected.
- Keep this address Active.
- In the second Address field, specify the secondary server. In this case,
example.net
. - Due to its lower capacity, in the Weight field, add a weight of
2
to this address. - In the Server Role field, select Primary. This address has a lower weight value, so the preferred address for connections will be the first.
- Keep this address Active.
- Add a third origin by clicking the + Add Address button.
- In the Address field, specify the backup origin. In this case,
example.org
. - Keep the Weight field blank. It’ll assume a default value of
1
. - In the Server Role field, select Backup.
- Disable this address by turning off the Active switch.
- Click the Save button.
To activate the Connector in your application:
- Access the Rules Engine tab.
- Edit the default rule or add a new request rule.
- In the Criteria section, to apply the Connector for your whole application, set the criteria to
If ${uri} starts with /
. - In the Behavior section, select the Set Connector behavior.
- Select the Connector you want to apply.
- Click the Save button.
- Wait some time for the changes to propagate to the edge. Then, access your application or use the command line to make a request and check if the changes were made.
First, to enable the Load Balancer module:
- Access Azion Console > Applications.
- Click the application for which you want to configure a new origin with load balancer.
- On the Main Settings tab, under Modules, activate Load Balancer.
- Click the Save button.
To customize the origin according to the scenario described above:
- Select the Origins tab.
- In the Origins tab, click the Default Origin from the list.
- Give your new origin a name.
- Under Type, select Load Balancer.
- In the Method field, select Round-Robin.
- In the first Address field, specify the primary server. For example, example.com`.
- Since this origin has a higher load capacity, in the Weight field, add a weight of
3
to this first origin. - In the Server Role field, keep Primary selected.
- Keep this origin Active.
- In the second Address field, specify the secondary server. In this case,
example.net
. - Due to its lower capacity, in the Weight field, add a weight of
2
to this origin. - In the Server Role field, select Primary. This origin has a lower weight value, the preferred origin for connections will be the first.
- Keep this origin Active.
- Add a third origin by clicking the + Add Origin button.
- In the Address field, specify the backup origin. In this case,
example.org
. - Keep the Weight field blank. It’ll assume a default value of
1
. - In the Server Role field, select Backup.
- Disable this origin by turning off the Active switch.
- Click the Save button.
Wait a few minutes for the changes to propagate. Then, access your application or use the command line to make a request and check if the changes were made.
- Run the following
GET
request in your terminal, replacing[TOKEN VALUE]
with your personal token and<application_id>
variable with your application ID to retrieve your default origin’s<origin_key>
, which is a UUID value:
curl --location 'https://api.azionapi.net/edge_applications/<application_id> --header 'Accept: application/json; version=3' --header 'Authorization: Token [TOKEN VALUE]'
- You’ll receive a response similar to this:
{ "results": { "origin_id": <origin_id>, "origin_key": "<origin_key>", "name": "Default Origin", "origin_type": "single_origin", ... }}
- Copy the
<origin_key>
value. - Run the following
PATCH
request to modify the default origin as follows, replacing the key and application id in the URL:
curl --location --request PATCH 'https://api.azionapi.net/edge_applications/<application_id>/origins/<origin_key>' --header 'Accept: application/json; version=3' --header 'Content-Type: application/json' --header 'Authorization: Token [TOKEN VALUE]' --data '{ "name": "Default Origin (RR3)", "origin_type": "load_balancer", "addresses": [ { "address": "example.com", "weight": 3, "server_role": "primary", "is_active": true }, { "address": "example.net", "weight": 2, "server_role": "primary", "is_active": true }, { "address": "example.org", "weight": 1, "server_role": "backup", "is_active": false } ], "host_header": "${host}", "method": "round_robin"}'
- You’ll receive a response similar to this, confirming the changes have been made:
{ "results": { "origin_id": <origin_id>, "origin_key": "<origin_key>", "name": "Default Origin (RR3)", "origin_type": "load_balancer", "addresses": [ { "address": "example.com", "weight": 3, "server_role": "primary", "is_active": true }, { "address": "example.net", "weight": 2, "server_role": "primary", "is_active": true }, { "address": "example.org", "weight": 1, "server_role": "primary", "is_active": false } ] "host_header": "${host}", "method": "round_robin" ... }}
- Wait a few minutes for the propagation to occur.
- Run the following
POST
request in your terminal, replacing[TOKEN VALUE]
with your personal token to create an Connector.
curl --request POST --url https://api.azion.com/v4/edge_connector/connectors --header 'Accept: application/json' --header 'Authorization: Token [TOKEN VALUE]' --header 'Content-Type: application/json' --data '{ "name": "Multiple Origins Connector", "active": true, "type": "http", "attributes": { "addresses": [ { "active": true, "address": "example.com", "http_port": 80, "https_port": 443, "modules": { "load_balancer": { "server_role": "primary", "weight": 3 } } }, { "active": true, "address": "example.net", "http_port": 80, "https_port": 443, "modules": { "load_balancer": { "server_role": "primary", "weight": 2 } } }, { "active": false, "address": "example.org", "http_port": 80, "https_port": 443, "modules": { "load_balancer": { "server_role": "backup" } } } ], "connection_options": { "dns_resolution": "preserve", "transport_policy": "preserve", "http_version_policy": "http1_1", "host": "${host}", "path_prefix": "", "following_redirect": false, "real_ip_header": "X-Real-IP", "real_port_header": "X-Real-PORT" }, "modules": { "load_balancer": { "enabled": true, "config": { "method": "round_robin", "max_retries": 0, "connection_timeout": 60, "read_write_timeout": 120 } }, "origin_shield": { "enabled": false, "config": null } } }}'
- You should receive a response confirming the creation of your Connector, then copy the ID field. This will be used in the following step.
- Run the following
POST
request in your terminal to create a Rules Engine rule. This will bind your Connector to your Applications, replacing the<application_id>
variable with the Applications ID and the<connector_id>
with the Connector ID you copied previously: - Run the following
POST
request in your terminal to create a Rules Engine rule. This will bind your Connector to your Applications, replacing the<application_id>
variable with the Applications ID and the<connector_id>
with the Connector ID you copied previously:
curl --request POST --url https://api.azion.com/v4/edge_application/applications/<application_id>/request_rules --header 'Accept: application/json' --header 'Authorization: Token [TOKEN VALUE]' --header 'Content-Type: application/json' --data '{"name": "string","active": true,"criteria": [ { "conditional": "if", "variable": "${uri}", "operator": "starts_with", "argument": "/" } ]],"behaviors": [ { "type": "set_edge_connector", "attributes": { "value": "<connector_id>" } }]}'