APISIX Load Balancing Guide

APISIX Load Balancing Overview

APISIX Load Balancing Overview

Introduction

Load balancing is a critical component in modern API architectures that helps distribute incoming network traffic across multiple backend servers. APISIX provides robust load balancing capabilities with various algorithms and configuration options to ensure optimal performance and reliability.


Load Balancing Algorithms

APISIX supports several load balancing algorithms, each suited for different use cases:

Round Robin

Distributes requests sequentially across the server pool.

upstream:
  type: roundrobin
  nodes:
    "backend1:8080": 1
    "backend2:8080": 1

Weighted Round Robin

Distributes requests based on server weights.

upstream:
  type: roundrobin
  nodes:
    "backend1:8080": 2
    "backend2:8080": 1

Consistent Hashing

Routes requests based on a hash of the key.

upstream:
  type: chash
  key: remote_addr
  nodes:
    "backend1:8080": 1
    "backend2:8080": 1

Least Connections

Routes to server with fewest active connections.

upstream:
  type: least_conn
  nodes:
    "backend1:8080": 1
    "backend2:8080": 1

Health Checks

Health Checks Diagram

Health Check Mechanism in APISIX

APISIX provides active and passive health checks to ensure reliability:

upstream:
  type: roundrobin
  nodes:
    "backend1:8080": 1
    "backend2:8080": 1
  checks:
    active:
      http_path: "/health"
      healthy:
        interval: 2
        successes: 1
      unhealthy:
        interval: 1
        http_failures: 2
Note: Health checks are crucial for maintaining system reliability. Configure them based on your application's specific needs.

Dynamic Load Balancing

APISIX supports dynamic upstream configuration through its Admin API:

curl http://127.0.0.1:9080/apisix/admin/upstreams/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "type": "roundrobin",
    "nodes": {
        "backend1:8080": 1,
        "backend2:8080": 1
    }
}'

Advanced Configuration

Advanced Configuration

Advanced Load Balancing Configuration Options

Timeout Settings

upstream:
  type: roundrobin
  nodes:
    "backend1:8080": 1
  timeout:
    connect: 6
    send: 6
    read: 6

Retry Configuration

upstream:
  type: roundrobin
  retries: 2
  retry_timeout: 10
  nodes:
    "backend1:8080": 1
    "backend2:8080": 1

Best Practices

  • Choose the appropriate algorithm based on your use case
  • Always configure health checks
  • Set appropriate timeout values
  • Monitor load balancer performance
  • Implement proper error handling
Pro Tip: Use weighted load balancing when backend servers have different capabilities or resources.

Monitoring Load Balancing

Load Balancing Metrics Dashboard


Read Next

Service Discovery

Learn about service discovery integration

Read More

Rate Limiting

Configure rate limiting in APISIX

Read More