When using a load balancer to distribute traffic to VMs in a backend pool, it's important to consider session persistence or session affinity. Session persistence ensures that a user's requests are consistently directed to the same backend VM during their session. By default, the load balancer uses the client's IP address for session persistence, which means that requests from the same client IP will be directed to the same backend VM.
In the scenario you described, when an unhealthy VM is detected, the load balancer correctly redirects the users to other healthy VMs. However, since the session persistence is based on the client's IP address, the users may end up on a different VM than the one they initially connected to. This can result in a session expired page because the session state is not maintained across VMs.
To address this issue, you have a few options:
Change the session persistence to "None": If you change the session persistence to "None" or "Source IP Affinity," the load balancer will distribute the requests evenly across the available backend VMs without considering the client's IP address. This can help in avoiding the session expired issue, but it may also lead to uneven distribution of traffic.
Implement session state sharing: If your application relies on session state, you can consider implementing session state sharing across the backend VMs. This involves using a shared session state store, such as a database or a distributed cache, where the session data is stored and can be accessed by any of the backend VMs. This way, even if the user's request is redirected to a different VM, the session data can be retrieved and the progress can be maintained.
Sticky sessions: Another option is to enable sticky sessions or cookie-based session affinity. This involves the load balancer inserting a cookie into the client's browser upon the first request, and subsequent requests from the same client will be directed to the same backend VM based on the cookie. This ensures that the session remains on the same VM, maintaining the session state and avoiding session expired issues.
The choice of solution depends on the specific requirements and architecture of your application. Consider the trade-offs between distributing the traffic evenly, maintaining session state, and the complexity of implementing session state sharing.