---
title: "Sticky Sessions with Apache APISIX: Session Affinity Guide - API7.ai"
description: "Learn when sticky sessions help, their availability and scaling trade-offs, and how to configure cookie-based session affinity with Apache APISIX."
url: "https://api7.ai/blog/sticky-sessions-theory"
---

[Blog](https://api7.ai/blog)

[Products](https://api7.ai/category/products)[Technology](https://api7.ai/category/technology)[Ecosystem](https://api7.ai/category/ecosystem)[Case Study](https://api7.ai/category/usercase)[Team & Community](https://api7.ai/category/community)

Search

⌘ + K

[Blog](https://api7.ai/blog)

Technology

# Sticky Sessions with Apache APISIX: Session Affinity Guide

[![Nicolas Fränkel](https://avatars.githubusercontent.com/u/752258?v=4)]()

Nicolas Fränkel

July 27, 2023

[Technology](https://api7.ai/category/technology)

Sticky sessions, also known as session affinity, route requests carrying the same session identifier to the same upstream node. They can help a stateful application reuse node-local session data, but they do not make that data durable. This guide explains the trade-offs and shows how to configure cookie-based affinity with Apache APISIX.

## Why Sticky Sessions?

Sticky sessions became popular when we stored the state on the upstream node, not the database. I'll use the example of a simplified e-commerce shop to explain further.

The basic foundations of a small e-commerce site can consist of a web application and a database.

![Foundation architecture of an e-commerce app](https://static.api7.ai/uploads/2023/07/25/0Z8I0Em6_foundation-ecommerce.svg)

If the business is successful, it will grow, and you'll need to scale this architecture at some point. Once you cannot scale vertically (bigger machines), you must scale horizontally (more nodes). With additional app nodes, you'll also need a load balancer mechanism in front of the web app nodes to distribute the load among them.

![Load Balancing architecture for additional nodes](https://static.api7.ai/uploads/2023/07/25/STWTE4Pj_load-balancer-ecommerce.svg)

Going to the database every time is an expensive operation. It's okay for data that is accessed infrequently. However, we want to display the cart's content for every request. A couple of alternatives are available to speed things up. If we assume that the web app uses Server-Side Rendering, the classical solution is to keep cart-related data in memory on the web app node.

However, if we store user X's cart on node 1, we need to ensure that we forward every request of user X to the same node. Otherwise, they will feel as if they lost their cart's content. Sticky sessions, or session affinity, is the mechanism that consistently routes the same user to the same node.

## Limitation of Sticky Sessions

Before going further, consider a significant limitation: if a web application node is the only place that stores session data and that node fails, the data are lost. In the e-commerce example, users could lose their carts.

Do not treat affinity as a substitute for state durability. Depending on the application, use session replication, a shared session store, or a stateless design. These approaches let another node continue serving the session when the preferred node is unavailable.

While session replication exists in all tech stacks, there's no related specification. I'm familiar with the JVM, so here are a couple of options:

- Tomcat offers [session replication](https://tomcat.apache.org/tomcat-10.1-doc/cluster-howto.html) out-of-the-box
- [Hazelcast](https://github.com/hazelcast/hazelcast-wm) offers a clustered in-memory solution that you can integrate at different levels
- [Spring Session](https://spring.io/projects/spring-session) is an abstraction layer upon specific solutions

When data is replicated on all nodes (or a remote cluster), you may think you no longer need sticky sessions. It's true if one accounts only for availability and not for performance. It's about data locality: fetching data on the current node rather than from somewhere else via the network is faster.

## Sticky Sessions on Apache APISIX

For applications that benefit from session affinity, Apache APISIX can combine a consistent-hash upstream with a stable request value, such as an application session cookie. Use this approach only when the application accepts the durability and load-distribution trade-offs described above.

Apache APISIX binds a *route* to an *upstream*. An upstream consists of one or more nodes. When a request matches the route, Apache APISIX must choose among all available nodes to forward the request to. By default, the algorithm is weighted round-robin. Round-robin uses one node after the other, and after the last one, get back to the first one. With a weighted round-robin, the weight affects how many requests Apache APISIX forwards to a node before it switches to the next one.

However, other algorithms are available:

- Consistent hashing
- Exponentially weighted moving average (EWMA)
- Least connection
- A custom-made one

Consistent hashing allows forwarding to the same node depending on some value: an NGINX variable, an HTTP header, a cookie, etc.

HTTP is stateless, so many application servers set a cookie to associate later requests with a session. To hash on that cookie, you need its exact name. Common examples include:

- `JSESSIONID` for JVM-based servers
- `PHPSESSID` for PHP
- `ASPSESSIONID` for ASP.Net
- etc.

I shall use a regular Tomcat, so the session cookie is `JSESSIONID`. Henceforth, the Apache APISIX documentation for two nodes is the following:

```
routes:
  - uri: /*
    upstream:
      nodes:
        "tomcat1:8080": 1            #1
        "tomcat2:8080": 1            #1
      type: chash                    #2
      hash_on: cookie                #3
      key: JSESSIONID                #4
```

1. Define the upstream nodes
2. Choose the consistent hashing algorithm
3. Hash on cookie
4. Define the case-sensitive cookie name to hash on

APISIX expects the cookie name in `key` when `hash_on` is `cookie`. In APISIX 3.18, if a request does not contain that cookie, APISIX falls back to `remote_addr` as the hash key. The initial request can therefore reach one node, while the next request can reach another after the application sets `JSESSIONID`. Applications that need continuity from the first request should establish a stable cookie before creating node-local session state or use shared or replicated session storage.

The configuration is equivalent to using the NGINX variable for that cookie internally. Once requests carry the cookie, expose a test response that identifies the serving node, or inspect the application logs, then send repeated requests with the same cookie value:

```
for request in 1 2 3; do
  curl -s http://127.0.0.1:9080/ \
    -H 'Cookie: JSESSIONID=test-session'
  echo
done
```

All three requests should identify the same healthy upstream. Then verify that both upstreams are healthy and sample several distinct cookie values:

```
for session in $(seq 1 20); do
  printf 'session-%s: ' "$session"
  curl -s http://127.0.0.1:9080/ \
    -H "Cookie: JSESSIONID=session-$session"
  echo
done
```

Different cookie values can still hash to the same node. The goal is to confirm stable routing for each repeated key and, across a sufficiently large sample, observe that more than one healthy upstream participates.

When [upstream health checks](https://api7.ai/blog/tips-for-health-check-best-practices) mark the selected node unhealthy, APISIX can choose from the remaining healthy nodes. That failover does not recover session data stored only on the failed node.

## Conclusion

Sticky sessions can reduce remote session lookups and support applications that still keep state on individual nodes. Use them only with an explicit durability and failover strategy, then monitor load distribution because affinity can concentrate traffic unevenly.

**To go further:**

- [Load Balancing](https://en.wikipedia.org/wiki/Load_balancing_\(computing\))
- [Apache APISIX upstream configuration](https://apisix.apache.org/docs/apisix/terminology/upstream/)
- [The illusion of statelessness](https://blog.frankel.ch/illusion-statelessness/)

Tags:

[APISIX Basics](https://api7.ai/tags/apisix-basics)

[API Gateway Concept](https://api7.ai/tags/api-gateway-concept)

[Sticky Sessions](https://api7.ai/tags/sticky-sessions)

Share article link

Share

Related Posts

[![Top 10 API Management Trends for 2023](https://static.api7.ai/uploads/2023/04/12/PtHsoEJS_top-10-trends.png)](https://api7.ai/blog/10-api-management-trends-2023)

## [Top 10 API Management Trends for 2023](https://api7.ai/blog/10-api-management-trends-2023)

[![etcd vs PostgreSQL](https://static.api7.ai/uploads/2024/12/25/wOH8qXLg_etcd-vs-postgresql.png)](https://api7.ai/blog/etcd-vs-postgresql)

## [etcd vs PostgreSQL](https://api7.ai/blog/etcd-vs-postgresql)

Table of Contents

- [Why Sticky Sessions?](#why-sticky-sessions)
- [Limitation of Sticky Sessions](#limitation-of-sticky-sessions)
- [Sticky Sessions on Apache APISIX](#sticky-sessions-on-apache-apisix)
- [Conclusion](#conclusion)

## Related Posts

[![How API Gateways Fit into API Management](https://static.api7.ai/uploads/2024/12/25/zQTHfZGV_api-gateway-vs-api-management.jpg)](https://api7.ai/blog/api-gateway-and-api-management-comparison)

Technology

September 1, 2023

#### [How API Gateways Fit into API Management](https://api7.ai/blog/api-gateway-and-api-management-comparison)

Learn how the API gateway runtime fits into API management across lifecycle workflows, developer portals, governance, analytics, security, and operations.

[API Gateway Concept](https://api7.ai/tags/api-gateway-concept)

[API Management](https://api7.ai/tags/api-management)

[APISIX Basics](https://api7.ai/tags/apisix-basics)

[![Custom Plugin Development For APISIX With Lua And ChatGPT](https://static.api7.ai/uploads/2024/12/25/MssMP69w_custom-plugin-development.png)](https://api7.ai/blog/plugin-development-with-lua-and-chatgpt)

Technology

June 12, 2023

#### [Custom Plugin Development For APISIX With Lua And ChatGPT](https://api7.ai/blog/plugin-development-with-lua-and-chatgpt)

This article will guide you through the process of developing a new custom plugin called file-proxy for APISIX using Lua and ChatGPT.

[API Gateway Concept](https://api7.ai/tags/api-gateway-concept)

[APISIX Basics](https://api7.ai/tags/apisix-basics)

[Plugin](https://api7.ai/tags/plugin)

[![API Gateway vs Kubernetes Gateway vs Service Mesh](https://static.api7.ai/uploads/2023/06/07/KS3wtoLx_cover.png)](https://api7.ai/blog/gateway-and-mesh)

Technology

June 9, 2023

#### [API Gateway vs Kubernetes Gateway vs Service Mesh](https://api7.ai/blog/gateway-and-mesh)

Compare API gateways, Kubernetes Gateway API, and service meshes by traffic scope, ownership, policy, deployment model, and when to use them together.

[APISIX Basics](https://api7.ai/tags/apisix-basics)

[API Gateway Concept](https://api7.ai/tags/api-gateway-concept)

[Kubernetes](https://api7.ai/tags/kubernetes)
