Load Balancing (HAProxy or other) - Sticky Sessions

后端 未结 2 522
鱼传尺愫
鱼传尺愫 2020-12-05 05:43

I\'m working on scaling out my app to multiple servers, and one requirement is that a client is always communicating with the same server (too much live data is used to allo

相关标签:
2条回答
  • 2020-12-05 05:53

    HAProxy supports modifying or inserting a cookie to provide session persistence with the cookie parameter.

    In either backend or listen sections, add the following:

    cookie COOKIENAME prefix
    

    This example will modify an existing cookie by adding the name of the server to a cookie called COOKIENAME. Your client will see something like server1~someotherdata but your application will only see the someotherdata part. So you can use this on existing cookies. Additionally this method allows you to only enforce session persitence when that cookie exists, meaning you can still evenly balance people around the static portions of your site and only enforce stickyness when needed, but adding that cookie name to the session.

    Also name your servers, so your server lines look like the following:

    server server1 1.2.3.4 cookie server1
    

    More detail is in the HAProxy config guide, it also looks like you can use the appsession config parameter as well.

    Once you've done this, you can pick your own balance method from the list, I tend to use roundrobin but leastconn might give you a better balance once sticky sessions are taken into account.


    More from documentation to make it easier to find reference section:

    cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ]
                  [ postonly ] [ preserve ] [ domain <domain> ]*
                  [ maxidle <idle> ] [ maxlife <life> ]
      Enable cookie-based persistence in a backend.
      May be used in sections :   defaults | frontend | listen | backend
                                     yes   |    no    |   yes  |   yes
    
    0 讨论(0)
  • 2020-12-05 06:02

    You can adjust the balancing algorithm in HA-Proxy there are some available though. Like e.g. roundrobin or leastconn.

    But you need to adjust in general your balancing according to the domain of users for whom content is served. Most of the times you need to do empirical tests and reiterate your decision according to your findings.

    0 讨论(0)
提交回复
热议问题