When building cloud-native applications, load balancing across multiple replicas is essential for scalability and resilience. But what happens when your application needs session affinity, ensuring that a user’s requests are consistently routed to the same replica? This is where sticky sessions come in.
I recently published a demo project on GitHub that shows how to configure and test session affinity in Azure Container Apps (ACA) using a Spring Boot application and Nginx for local load balancing. 👉 View the repo here.
Why Session Affinity Matters
Imagine a shopping cart application. Without sticky sessions, a user’s requests might bounce between replicas, losing track of their cart state. With sticky sessions enabled, the load balancer ensures that all requests from the same client go to the same replica, preserving continuity.
This demo illustrates both scenarios—sticky sessions ON and OFF—so you can see the difference in behavior.
Local Development and Testing
The project includes a Docker Compose setup with Nginx acting as the load balancer.
- Round-robin mode: Requests are distributed across replicas.
- Sticky mode: Requests from the same client are pinned to one replica.
You can toggle between these modes by editing the docker-compose.yml file to mount either nginx.roundrobin.conf or nginx.sticky.conf.
Testing is straightforward with curl and a cookie jar. With sticky sessions enabled, you’ll see the same replicaHost across requests and an incrementing hitCountsInThisSession. Without sticky sessions, requests will bounce between replicas.
Building and Publishing Images
The demo includes instructions for building the Docker image and pushing it to your preferred registry—GitHub Container Registry, Azure Container Registry, or Docker Hub. This flexibility makes it easy to integrate into your existing CI/CD pipelines.
Deploying to Azure Container Apps
The real power of this demo is in showing how ACA handles session affinity in production:
- A deployment script (
azure-deploy.sh) automates resource group creation, environment setup, and application deployment. - The app is scaled to two replicas to demonstrate load balancing.
- Session affinity can be toggled dynamically using the Azure CLI:
stickyfor enabling affinitynonefor disabling affinity
Expected outputs are clearly documented, so you can validate whether sticky sessions are working as intended.
Managing and Cleaning Up
The demo also covers how to:
- Check current affinity settings with
az containerapp ingress sticky-sessions show. - Clean up resources with a single
az group deletecommand.
This ensures you can experiment freely without leaving behind unused infrastructure.
đź”— Explore the full code and instructions here: Session Affinity Demo on GitHub.