Skip to main content
Version: 3.18.0

Load Balancing

Load balancing is a technique used to distribute network request loads. It is a key consideration in designing systems that need to handle a large volume of traffic, allowing for improved system performance, scalability, and reliability.

Apache APISIX supports a number of load balancing algorithms, one of which is the weighted round-robin algorithm. This algorithm distributes incoming requests over a set of servers in a cyclical pattern.

In this tutorial, you will create a route with two upstream services and use the round-robin load balancing algorithm to load balance requests.

Prerequisite(s)​

  1. Complete Get APISIX to install APISIX in Docker.
  2. Understand APISIX routes and upstreams.
  3. Install ADC or APISIX-MCP if you are using these tools.

Enable Load Balancing​

Create a route with two upstream services, httpbin.org and mock.api7.ai, to distribute requests across. Both services respond with the request headers when receiving a request at /headers:

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
"id": "getting-started-headers",
"uri": "/headers",
"upstream" : {
"type": "roundrobin",
"nodes": {
"httpbin.org:443": 1,
"mock.api7.ai:443": 1
},
"pass_host": "node",
"scheme": "https"
}
}'

❶ type: use roundrobin as the load balancing algorithm.

❷ nodes: upstream services.

❸ pass_host: use node to pass the host header to the upstream.

❹ scheme: use https to enable TLS with upstream.

You should receive an HTTP/1.1 201 Created response if the route was created successfully.

Verify​

Generate 50 consecutive requests to APISIX /headers route to see the load-balancing effect:

resp=$(seq 50 | xargs -I{} curl "http://127.0.0.1:9080/headers" -sL) && \
count_httpbin=$(echo "$resp" | grep "httpbin.org" | wc -l) && \
count_mockapi7=$(echo "$resp" | grep "mock.api7.ai" | wc -l) && \
echo httpbin.org: $count_httpbin, mock.api7.ai: $count_mockapi7

The command keeps count of the number of requests that were handled by the two services respectively. The output shows that requests were distributed over to the two services:

httpbin.org: 23, mock.api7.ai: 27

The distribution of requests across services should be close to 1:1 but might not always result in a perfect 1:1 ratio. The slight deviation is due to APISIX operating with multiple workers.

What's Next​

You have learned how to configure load balancing. In the next tutorial, you will learn how to configure key authentication.