# How to perform a load balance between DNS records

import Tabs from '~/components/tabs/Tabs'
import Code from '~/components/Code/Code.astro'

You can use an [Edge DNS zone](/en/documentation/products/secure/edge-dns/) to create multiple records and perform a DNS load balance. Assigning different weights to each record for the same zone helps distribute incoming network traffic, ensuring optimal resource utilization and preventing overload on a single server.

In this guide, you'll learn an example using a record type `A`, which accepts values of IPv4 address format.

---

<Tabs client:visible>
    <Fragment slot="tab.console">Console</Fragment>
    <Fragment slot="tab.api">API</Fragment>

<Fragment slot="panel.console">

1. Access [Azion Console](/en/documentation/products/guides/how-to-access-azion-console/) > **Edge DNS**.
2. Choose the zone in which you want to add records from the list or [create a zone](/en/documentation/products/guides/secure/edge-dns-configure-main-settings/#creating-a-new-zone).
3. Select the **Records** tab.
4. Click the **+ Record** button.
5. In **Name**, provide the new record's name as a subdomain.
6. In **Type**, select the type of record `A`.
7. In **Value**, add the items for the DNS response to the registered record in IPv4 address format. Example: `192.111.0.1`.
8. In **TTL (seconds)**, choose the time, in seconds, that a response can be cached on a resolver server. Maximum value: `2147483647`.
9. In **Policy**, select **Weighted**.
    - In **Weight**, specify the weight for the record, considering that you'll create other records for the same zone with different weights. Accepts values from `0` to `255`.
    - In **Description** (optional), you can add a short text that differentiates the records you'll create for the load balance. Accepts up to *45 characters*.
10. Click the **Save** button.
11. Repeat steps 4 to 10 to create the other balanced DNS records needed while observing Edge DNS limits, always using the same **Name** and setting the desired addresses in **Value** and weights for each record in **Weight**.

If you add 3 records, you can, for example, specify a weight of **50** for the first record, **20** for the second record, and **30** for the third record.
</Fragment>

<Fragment slot="panel.api">

1. Run the following `GET` request in your terminal, replacing `[TOKEN VALUE]` with your [personal token](/en/documentation/products/guides/personal-tokens/) to retrieve your `<hosted_zone_id>`:

<Code lang="bash" code={`
curl --request GET \
  --url https://api.azion.com/v4/edge_dns/zones \
  --header 'Accept: application/json' \
  --header 'Authorization: Token [TOKEN VALUE]'
`} />

2. You'll receive a response with all your existing zones. Copy the value of the `<id>` that you want to use.
3. Run the following `POST` request, replacing `[TOKEN VALUE]` with your [personal token](/en/documentation/products/guides/personal-tokens/) and the `<hosted_zone_id>` value you copied:

<Code lang="bash" code={`
curl --request POST \
  --url https://api.azion.com/v4/edge_dns/zones/{zoneId}/records \
  --header 'Accept: application/json' \
  --header 'Authorization: Token [TOKEN VALUE]' \
  --header 'Content-Type: application/json' \
  --data '{
    "type": "A",
    "name": "lbexample",
    "rdata": ["192.111.0.1", "192.111.0.2"],
    "ttl": 20,
    "policy": "weighted",
    "weight": 50,
    "description": "policy weight 50"
  }'
`} />

4. You'll receive a response similar to this:

<Code lang="json" code={`
{
  "state": "executed",
  "data": {
    "id": <record_id>,
    "description": "policy weight 50",
    "name": "lbexample",
    "ttl": 20,
    "type": "A",
    "rdata": [
      "192.111.0.1",
      "192.111.0.2"
    ],
    "policy": "weighted",
    "weight": 50
  }
}
`} />

Wait a few minutes for the changes to propagate and your records will be created in the hosted zone you chose.

5. Repeat the steps to create the other balanced DNS records needed while observing Edge DNS limits, always using the same `entry` and setting the desired addresses in the `answers_list` parameter and weights for each record in the `weight` parameter.

:::tip
Check the [Azion API documentation](https://api.azion.com/) to know more about what the Azion API can offer.
:::
</Fragment>

</Tabs>
---

## Testing your balanced record

After configuring your balanced record, you can test and see if it's working via terminal commands:

1. Run `dig +short [your balanced hostname]` a couple of times in a row.
    - You'll see the address list vary based on the record selected by the Edge DNS load balancer each time.
2. The response will be similar to:

```bash
dig +short lbexample @ns1.aziondns.net
192.111.0.1
```

```bash
dig +short lbexample @ns1.aziondns.net
192.111.0.2
```

```bash
dig +short lbexample @ns1.aziondns.net
192.111.0.3
```
---