Share via

load balancing / cookie affinity

stephane clavel 66 Reputation points
2026-05-13T14:51:49.5266667+00:00

Hello

I'm asked to find an Azure load balancing solution that would fit the following requirements:

  • app runs on VMs
  • 2 instances of the app run on each VM, listening to a different TCP port (say 9000 and 9001)
  • cookie based affinity is required

My take:

  • Azure LB could meet the requirement for 2 instances on single VM
  • Azure App GW could meet requirement for cookie affinity
  • but I fail to find a solution that meets all requirements Can you please help me ? Thanks
Azure Load Balancer
Azure Load Balancer

An Azure service that delivers high availability and network performance to applications.


2 answers

Sort by: Most helpful
  1. Ravi Varma Mudduluru 11,955 Reputation points Microsoft External Staff Moderator
    2026-05-13T15:15:06.4433333+00:00

    Hello @ stephane clavel,

    Thank you for reaching out to Microsoft Q&A.
    You’re right that no single Azure “L4” load-balancer can do cookie-based affinity (that’s an L7 feature), so you have two main Azure options depending on whether your app is HTTP/HTTPS or truly raw TCP:

    If your app speaks HTTP/HTTPS on ports 9000 and 9001

    • Use Azure Application Gateway (v2)

    • Create two HTTP listeners (one on port 9000, one on 9001)
    • Point both listeners to the same backend pool (your VMs)
    • In each HTTP setting, set the backend port (9000 or 9001) and enable cookie-based affinity
    • App Gateway will insert its affinity cookie (ApplicationGatewayAffinity) on the first response and stick subsequent requests to the same VM+port • Pros: full L7 termination, cookie-based “sticky sessions,” SSL offload, WAF, path-based routing, etc. • Docs: – https://docs.microsoft.com/azure/application-gateway/configuration-overview#cookie-based-affinity

    If you need a global front-end or additional edge acceleration, still HTTP/HTTPS:

    Use Azure Front Door Standard/Premium

    Define an Origin Group with your VMs on ports 9000/9001

    • Enable “Cookies-based session affinity” at the Origin Group level -Front Door will add ASLBSA and ASLBSACORS cookies to keep traffic on the same origin+port.

    Reference list

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Was this answer helpful?


  2. AI answer

    2026-05-13T14:52:02.6833333+00:00

    Azure Load Balancer alone cannot meet the cookie-based affinity requirement because it is a Layer 4 (TCP/UDP) load balancer and does not support cookie-based session affinity. Cookie affinity requires a Layer 7 (HTTP/HTTPS) load balancer that can inspect and set cookies.

    To satisfy all requirements (VM-based app, multiple instances per VM on different ports, and cookie-based affinity), use Azure Application Gateway in front of the VMs and configure:

    1. Application Gateway as a Layer 7 load balancer with cookie-based session affinity enabled in the HTTP settings.
    2. Backend pool containing the VMs.
    3. HTTP settings that:
      • Enable Cookie based affinity.
      • Forward traffic to the appropriate backend ports (9000 and/or 9001). This can be done with:
        • Separate HTTP settings per port (one for 9000, one for 9001) and corresponding routing rules, or
        • Path-/host-based routing if the application can distinguish instances that way.

    Application Gateway supports:

    • Layer 7 routing and cookie-based session affinity.
    • Round-robin distribution across backend instances.
    • Hosting multiple “sites” or routes behind a single gateway, which can be mapped to different ports on the same VM.

    If the application absolutely requires TCP-level access without HTTP/TLS termination, Azure Load Balancer can be used, but then cookie-based affinity is not possible. In that case, either remove the cookie requirement or use a different affinity mechanism (for example, X-Forwarded-For–based stickiness behind a Layer 7 proxy as described for Entra Application Proxy scenarios).

    In summary, the Azure-native way to meet all listed requirements is to front the VM-based application with Azure Application Gateway, configure backend HTTP settings per port, and enable cookie-based session affinity.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.