# Upstreams

In this document, you will learn the basic concept of an upstream object in APISIX and why you would want to use it. You will be introduced to a few relevant features, including load balancing, service discovery, and upstream health checking.

Explore additional resources at the end for more information on related topics.

## Overview[​](#overview "Direct link to Overview")

An *upstream* object in APISIX is a logical abstraction of a set containing one or more upstream addresses. Routes and services typically use an upstream to specify **where** requests flow and **how** they are distributed. An upstream can be omitted when a plugin produces the response or otherwise completes the request before proxying.

There are different ways to configure upstream addresses, one of which is to explicitly configure them in every route/service. Here is an example of such a configuration in routes, where the same upstream address is repeated across three different routes:

<br />

![Upstreams Diagram showing three routes with different plugins and the same hard-coded upstream address](https://static.api7.ai/uploads/2023/02/24/6gZ6zw2R_upstream-before.svg)

<br />

<br />

This approach, however, leads to data redundancies and poses a risk for update anomalies (i.e. inconsistent data resulting from a partial update).

A better approach is to extract the repetitive upstream configurations into a separate upstream object and embed its `upstream_id` into routes/services, such as the following:

<br />

![Upstreams Diagram showing three routes with different plugins pointing to the same upstream object with the desired upstream address](https://static.api7.ai/uploads/2023/02/24/vPjoKWdE_upstream-after.svg)

<br />

As you can probably see, large-scale systems with many routes/services would benefit significantly from configuring identical groups of upstream addresses in upstream objects, reducing redundant information and operational costs.

## Load Balancing[​](#load-balancing "Direct link to Load Balancing")

An important configuration option in upstreams is the [load balancing](https://docs.api7.ai/apisix/getting-started/load-balancing.md) algorithm.

APISIX offers four load-balancing algorithm options to choose from:

* `roundrobin` - weighted round robin
* `chash` - consistent hashing
* `ewma` - exponentially weighted moving average
* `least_conn` - least connections

You can also build your own load balancing algorithm by adding the source file under `apisix/balancer` and importing it with `require("apisix.balancer.your_balancer_name")`, where needed.

When application instances need time to warm up, slow start can gradually increase their effective weight. It applies to newly added nodes and nodes returning after APISIX observes them unavailable. See [Configure Upstream Slow Start](https://docs.api7.ai/apisix/how-to-guide/traffic-management/configure-upstream-slow-start.md) for supported configurations and lifecycle behavior.

To learn more about how you can configure load balancing in APISIX, see the [load balancing tutorial](https://docs.api7.ai/apisix/getting-started/load-balancing.md) and [Admin API reference](https://docs.api7.ai/apisix/reference/admin-api.md#tag/Upstreams/paths/~1apisix~1admin~1upstreams~1%7Bid%7D/put).

## Service Discovery[​](#service-discovery "Direct link to Service Discovery")

While it is straightforward to figure upstream addresses statically, in microservice-based architectures, upstream addresses are often dynamically assigned and therefore changed during autoscaling, failures, and updates. Static configurations are less than ideal in this case.

Service discovery comes to the rescue. It describes the process of automatically detecting the available upstream services, keeping their addresses in a database (called a service registry) for others to reference. That way, an API gateway can fetch the latest list of upstream addresses through the registry instead of relying on static addresses.

APISIX supports integrations with many service registries, such as Consul, Eureka, Nacos, Kubernetes service discovery, and more.

For more details about how to integrate with third-party service registries, such as HashiCorp Consul, please see the [how-to guide](https://docs.api7.ai/apisix/how-to-guide/service-discovery/consul-integration.md).

## TLS and mTLS[​](#tls-and-mtls "Direct link to TLS and mTLS")

An upstream can use TLS to encrypt traffic from APISIX and let APISIX verify the identity of the selected node. When an upstream also requires a client identity, APISIX can present a certificate through mTLS.

See [Configure HTTPS Between APISIX and Upstream](https://docs.api7.ai/apisix/how-to-guide/traffic-management/tls-and-mtls/configure-upstream-https.md) and [Configure mTLS Between APISIX and Upstream](https://docs.api7.ai/apisix/how-to-guide/traffic-management/tls-and-mtls/configure-mtls-between-apisix-and-upstream.md) for configuration and verification steps.

## Upstream Health Checking[​](#upstream-health-checking "Direct link to Upstream Health Checking")

APISIX provides active and passive health checking options to determine whether upstream nodes are healthy. APISIX excludes unhealthy nodes while at least one healthy node remains. If all nodes are unhealthy, APISIX falls back to the original node set so that a reachable node can still serve traffic.

Upstream health checking can be configured in the `checks` parameter in an upstream object.

For more details about how to configure active and passive upstream health check, please see the [how-to guide](https://docs.api7.ai/apisix/how-to-guide/traffic-management/health-check.md).

## Additional Resources[​](#additional-resources "Direct link to Additional Resources")

* Getting Started - [Load Balancing](https://docs.api7.ai/apisix/getting-started/load-balancing.md)
* [Configure Upstream Health Checks](https://docs.api7.ai/apisix/how-to-guide/traffic-management/health-check.md)
* [Integrate with HashiCorp Consul for Service Discovery](https://docs.api7.ai/apisix/how-to-guide/service-discovery/consul-integration.md)
* Admin API - [Upstreams](https://docs.api7.ai/apisix/reference/admin-api.md#tag/Upstreams)
