# Load Balancer quickstart

This guide instructs you through spreading the requests of your first connector across two origin servers with [Load Balancer](/en/documentation/platform/connectors/#load-balancer).

- Enable Load Balancer on the connector, and add a second address with a weight for each address.
- Send repeated requests through your workload and see answers from both servers.
- Take one address out of rotation, and see every request reach the other one.

Four parts make the chain, listed in the order this guide uses them:

1. The **connector** from the [Connectors quickstart](/en/documentation/platform/connectors/quickstart/) gets Load Balancer and the *Round Robin* method.
2. The **addresses** of the connector become two: `httpbin.org`, with a weight of `2`, and `httpbingo.org`, with a weight of `1`.
3. The **rule** that sends every request under `/anything` to the connector stays as it is.
4. The **workload** that serves your application receives the requests. Its deployment needs no change.

Both servers are public services that answer a request for `/anything/get` with a JSON copy of that request. Each one identifies itself in the `server` response header, so the response shows which address answered. To use your own servers, replace both hostnames with servers that hold the same content.

---

The prerequisites and the three stages show the steps of one interface at a time. Select yours:

## Prerequisites

- The result of the [Connectors quickstart](/en/documentation/platform/connectors/quickstart/): the connector `my-connector`, and a rule that sends every request under `/anything` to it.
- The workload domain of the [workload](/en/documentation/platform/workloads/) that serves the application, of the form `<id>.map.azionedge.net`. This guide writes it as `<workload-domain>`.
- `curl`, to send the requests to the workload domain.

**Console**

- Access to [Azion Console](https://console.azion.com/).

**CLI**

- The [Azion CLI](/en/documentation/devtools/cli/) on your machine, authorized with a personal token.
- The ID of the connector.

**API**

- A personal token for the `Authorization` header.
- The ID of the connector.

---

## Enable Load Balancer and add an address

A connector holds one address until Load Balancer is on. With Load Balancer on, it holds up to 15 addresses, and a balancing method chooses one for each request. *Round Robin* takes the addresses in turn. For the bounds, refer to [Connectors limits](/en/documentation/platform/connectors/limits/#load-balancer).

Each address carries a weight from 1 to 100, and a higher weight allocates more traffic to that address. The weight shapes the share of requests, not an exact split. Each address also carries a server role. Both addresses in this guide are *Primary*. A *Backup* address receives requests only when every primary address fails, and the *IP Hash* method refuses it. For more information, refer to [Server role](/en/documentation/platform/connectors/load-balancer/balancing-methods/#server-role).

Every address of a connector receives the same `Host` header and the same transport protocol. Two changes to the connection options make one setting work for both servers:

- The `Host` header becomes `${host}`, the host the client requested, instead of `httpbin.org`. A fixed `httpbin.org` would send `httpbingo.org` a `Host` header that names a different server.
- The connector reaches both servers over HTTP. Over HTTPS, `httpbingo.org` accepts only connections for its own name.

**Console**

To enable Load Balancer in Azion Console:

1. **Open the Connectors page**

   Access [Azion Console](https://console.azion.com/) > **Connectors**.

2. **Open your connector**

   Select `my-connector`, the connector from the Connectors quickstart.

3. **Send the requested host in the Host header**

   In **Host**, enter `${host}`.

4. **Connect to the servers over HTTP**

   In **Transport Protocol Policy**, select *Force HTTP*.

5. **Turn on Load Balancer**

   In **Modules**, turn on **Load Balancer**.

6. **Select the balancing method**

   In **Load Balancer Configuration**, set **Method** to *Round Robin*.

7. **Set the first address**

   Under **Address Management**, on `httpbin.org`, set **Server Role** to *Primary* and enter `2` in **Weight**.

8. **Select Add Address**

9. **Enter the second address**

   In the new **Address**, enter `httpbingo.org`, without a protocol or a port.

10. **Set the second address**

    Set **Server Role** to *Primary* and enter `1` in **Weight**.

11. **Select Save**

The Console shows `Connector has been updated`. The connector has Load Balancer on and two active addresses.

**CLI**

To enable Load Balancer with the Azion CLI, put the whole connector in a JSON file. The update command needs the full body, with every field the connector keeps. Save this body as `connector.json`:

```json
{
  "name": "my-connector",
  "active": true,
  "type": "http",
  "attributes": {
    "addresses": [
      {
        "address": "httpbin.org",
        "modules": { "load_balancer": { "server_role": "primary", "weight": 2 } }
      },
      {
        "address": "httpbingo.org",
        "modules": { "load_balancer": { "server_role": "primary", "weight": 1 } }
      }
    ],
    "connection_options": {
      "transport_policy": "force_http",
      "host": "${host}"
    },
    "modules": {
      "load_balancer": { "enabled": true, "config": { "method": "round_robin" } }
    }
  }
}
```

`modules.load_balancer` on each address holds its role and weight. `modules.load_balancer` under `attributes` turns Load Balancer on, and `config` must carry at least one key.

Update the connector from the file. Replace `<connector-id>` with the ID of your connector. The command needs `--type` even though the file names the type:

```bash
azion update connector --connector-id <connector-id> --type http --file connector.json
```

The output confirms the update:

```text
Updated Connector with ID <connector-id>
```

The connector has Load Balancer on and two active addresses. The settings that `config` leaves out take the API defaults: `max_retries` `0`, `connection_timeout` `60`, and `read_write_timeout` `120`.

**API**

To enable Load Balancer with the API, send a `PATCH` request to the connector. Replace `<connector-id>` with the ID of your connector, and `[TOKEN VALUE]` with your personal token:

```bash
curl --request PATCH \
  --url https://api.azion.com/v4/workspace/connectors/<connector-id> \
  --header 'Accept: application/json' \
  --header 'Authorization: Token [TOKEN VALUE]' \
  --header 'Content-Type: application/json' \
  --data '{
  "attributes": {
    "addresses": [
      {
        "address": "httpbin.org",
        "modules": { "load_balancer": { "server_role": "primary", "weight": 2 } }
      },
      {
        "address": "httpbingo.org",
        "modules": { "load_balancer": { "server_role": "primary", "weight": 1 } }
      }
    ],
    "connection_options": {
      "transport_policy": "force_http",
      "host": "${host}"
    },
    "modules": {
      "load_balancer": { "enabled": true, "config": { "method": "round_robin" } }
    }
  }
}'
```

`addresses` replaces the list of addresses, and `modules.load_balancer` on each one holds its role and weight. `modules.load_balancer` under `attributes` turns Load Balancer on, and `config` must carry at least one key. A `PATCH` keeps the connection options the body leaves out.

The API answers with `202` and `"state": "pending"`. The connector has Load Balancer on and two active addresses. The settings that `config` leaves out take the API defaults: `max_retries` `0`, `connection_timeout` `60`, and `read_write_timeout` `120`.

---

## Confirm both addresses answer

The check is the same whichever interface changed the connector. In the commands, replace `<workload-domain>` with the workload domain of your workload.

A connector change takes several minutes to reach Azion's distributed infrastructure, and data centers apply it at different times. Until then, some requests still reach `httpbin.org` alone, and some can return a `502` error page. Repeat the requests until both servers answer. For more information, refer to [Propagation](/en/documentation/platform/connectors/how-it-works/#propagation).

Send a request for a path under `/anything`, and print the response headers with the body:

```bash
curl -s -D - https://<workload-domain>/anything/get
```

Send the same request several times. An answer from `httpbin.org` carries this `server` header:

```text
HTTP/2 200
…
server: gunicorn/19.9.0
…
```

An answer from `httpbingo.org` carries this one:

```text
HTTP/2 200
…
server: Fly/<version>
via: 1.1 fly.io, 1.1 fly.io
…
```

Both servers answer for the same path, so the connector spreads requests across both addresses.

The answers come in no fixed order. Each data center balances on its own, so a run of requests can favor one address, and the share varies from run to run. For more information, refer to [Load Balancer](/en/documentation/platform/connectors/how-it-works/#load-balancer).

---

## Take an address out of rotation

An address with **Active** off stays on the connector and receives no requests. Use it to take a server out before maintenance, without deleting the address. This stage takes `httpbingo.org` out.

**Console**

To take the address out in Azion Console:

1. **Open your connector**

   Access [Azion Console](https://console.azion.com/) > **Connectors**, and select `my-connector`.

2. **Turn off the second address**

   Under **Address Management**, on `httpbingo.org`, turn off **Active**.

3. **Select Save**

The Console shows `Connector has been updated`. The address `httpbingo.org` is inactive.

**CLI**

To take the address out with the Azion CLI, edit the `connector.json` you saved. Add `"active": false` to the `httpbingo.org` address:

```json
{
  "name": "my-connector",
  "active": true,
  "type": "http",
  "attributes": {
    "addresses": [
      {
        "address": "httpbin.org",
        "modules": { "load_balancer": { "server_role": "primary", "weight": 2 } }
      },
      {
        "address": "httpbingo.org",
        "active": false,
        "modules": { "load_balancer": { "server_role": "primary", "weight": 1 } }
      }
    ],
    "connection_options": {
      "transport_policy": "force_http",
      "host": "${host}"
    },
    "modules": {
      "load_balancer": { "enabled": true, "config": { "method": "round_robin" } }
    }
  }
}
```

Update the connector from the file:

```bash
azion update connector --connector-id <connector-id> --type http --file connector.json
```

The output confirms the update:

```text
Updated Connector with ID <connector-id>
```

The address `httpbingo.org` is inactive, and it keeps its role and weight.

**API**

To take the address out with the API, send a `PATCH` request with both addresses, and `"active": false` on `httpbingo.org`:

```bash
curl --request PATCH \
  --url https://api.azion.com/v4/workspace/connectors/<connector-id> \
  --header 'Accept: application/json' \
  --header 'Authorization: Token [TOKEN VALUE]' \
  --header 'Content-Type: application/json' \
  --data '{
  "attributes": {
    "addresses": [
      {
        "address": "httpbin.org",
        "modules": { "load_balancer": { "server_role": "primary", "weight": 2 } }
      },
      {
        "address": "httpbingo.org",
        "active": false,
        "modules": { "load_balancer": { "server_role": "primary", "weight": 1 } }
      }
    ]
  }
}'
```

The body lists every address the connector keeps, because `addresses` replaces the list. The API answers with `202` and `"state": "pending"`. The address `httpbingo.org` is inactive, and it keeps its role and weight.

The change reaches data centers at different times, as when you added the address. For several minutes, some requests still reach `httpbingo.org`. Send this request several times:

```bash
curl -s -D - https://<workload-domain>/anything/get
```

Once every data center holds the change, every answer carries the `httpbin.org` header:

```text
HTTP/2 200
…
server: gunicorn/19.9.0
…
```

No answer carries the `Fly` header. The connector sends every request to the active address, and `httpbingo.org` stays on the connector with its role and weight.

---

## Next steps

- [Balancing methods](/en/documentation/platform/connectors/load-balancer/balancing-methods.md): Compare Round Robin, Least Connections, and IP Hash, and see how weight and server role shape the choice.
- [Balance traffic across multiple origins](/en/documentation/guides/application-performance/availability/multiple-origins.md): Plan primary and backup servers for an application that must keep answering through an outage.
- [Connectors limits](/en/documentation/platform/connectors/limits.md#load-balancer): The bounds on addresses, weight, retries, and timeouts with Load Balancer on.
- [Connector settings](/en/documentation/platform/connectors/settings.md#load-balancer): Every Load Balancer field, with its default in the API and in the Console.
