Search code examples
load-balancinghaproxy

Load balancing: when should a user always connect to the same server?


I came across the "source" load balancing algorithm in HAProxy, which ensures that a user will connect to the same server, by choosing server based on a hash of the source IP.

Why and when is it important for a user to connect to the same server? I cannot think of a reason, assuming that all candidate servers serve identical content.

Furthermore, if there was the need for a user to always connect to the same server, then wouldn't load balancing be completely irrelevant for this user?


Solution

  • It is important for a user to connect to the same server if we want to achieve session persistence.
    For example, when talking about a HTTP session, there are information/variables (think about a shopping cart) specific to the session in question.
    This dynamic information is not shared by the candidate servers in case they are not configured to do so and it is simpler to deal with it at the load-balancing level.

    The preferred way to deal with this in HAProxy is by using cookies, but this only works in HTTP mode. HAProxy offers the source load balancing algorithm, in case cookies can't be used. This can be used in TCP mode or with HTTP clients that refuse cookies.

    Load balancing will be irrelevant for the user in question right, until the cookie expires. But we generally need load balancing when dealing with many users so that they can be served by multiple servers with each user sticking with one of them.