Skip to main content

Version: latest

Upstreams and Load Balancing

An Upstream defines the backend target(s) for a Service. It specifies the destination nodes, how the gateway distributes traffic among them (load balancing), and how it monitors their availability (health checks).

In API7 Enterprise, Upstreams are typically configured within a Service's upstream field, or can be managed as standalone objects referenced by multiple Services.

Load balancing algorithms

API7 Gateway supports multiple algorithms to distribute traffic across backend nodes.

AlgorithmDescriptionBest for...
roundrobinDistributes requests sequentially across nodes based on their weights.Most general-purpose applications.
chashUses consistent hashing (by URI, cookie, etc.) to map requests to the same node.Stateful applications requiring session affinity.
least_connRoutes requests to the node with the fewest active connections.Workloads with varying request processing times.
ewmaExponentially Weighted Moving Average (Peak EWMA). Selects nodes with the lowest predicted latency.Dynamically responding to backend performance fluctuations.

Key fields

FieldTypeDescription
typestringLoad balancing algorithm: roundrobin (default), chash, least_conn, or ewma.
nodesarrayList of {host, port, weight, priority} objects for static backend targets.
schemestringProtocol for communicating with the backend: http, https, grpc, or grpcs.
retriesintegerNumber of retries when a request fails. Range: 0-65535.
timeoutobjectTimeout settings: {connect, send, read} in seconds. Default: 60 for each.
retry_timeoutnumberTimeout to continue retries. Default: 0 (disabled).
pass_hoststringpass (service host), node (node host), or rewrite (custom host).
keepalive_poolobjectConnection pool settings: {size, idle_timeout, requests}.
hash_onstringUsed with chash: vars, header, cookie, consumer, or vars_combinations.
keystringRequired for chash. The hash key (e.g., $remote_addr or a header name).

Service discovery

Instead of defining static nodes, you can use Service Discovery to dynamically fetch backend targets from a registry.

FieldDescription
service_nameThe service identifier in the registry (e.g., <registry_id>/public/group/my-service).
discovery_typeThe discovery mechanism: kubernetes, nacos, or consul.

Health checks

The gateway performs health checks to identify and remove unhealthy nodes from the load balancing rotation.

  • Active Health Checks: The gateway periodically sends heartbeat requests (HTTP or TCP) to the backend.
  • Passive Health Checks: The gateway monitors real-time traffic to identify failing nodes (e.g., observing consecutive 5xx errors).

TLS to upstream

If your backend requires secure communication, you can configure the following fields:

  • client_certificate: ID of the certificate used for client authentication (mTLS).
  • ca_certificates: List of CA certificate IDs to verify the backend's certificate.
  • tls_verify: If true, the gateway verifies the backend's certificate against the ca_certificates.

Next steps

API7.ai Logo

The digital world is connected by APIs,
API7.ai exists to make APIs more efficient, reliable, and secure.

Sign up for API7 newsletter

Product

API7 Gateway

SOC2 Type IIISO 27001HIPAAGDPRRed Herring

Copyright © APISEVEN PTE. LTD 2019 – 2026. Apache, Apache APISIX, APISIX, and associated open source project names are trademarks of the Apache Software Foundation