Search code examples
http-redirecthaproxy

How to make internal redirect in HAProxy?


Our stateful service holds session in operative memory. It takes about a minute to save session or get it from memory, so we use Redis to detect on which node the session is currently loaded to lock requests to other nodes. But HAProxy sometimes switches sessions between nodes. When it happens, for example if session was on operative memory on first node and we are switching to the second node, the request waits, first node needs to save its state and the second one needs to restore it. While it happens, HAProxy probably thinks that the node is down, so HAProxy starts switching other requests, and the same process happens for other requests. We increased HAProxy waiting timeout but it didn't help. How can we make HAProxy switch this request and all forthcoming requests from specific session to specific node?

Something like

303
Location: 192.168.1.2

Solution

  • Okay so your application requires session stickiness.
    When you have already cookies in place for session handling then I suggest to use also cookie stickiness within HAProxy.

    In short here a config snipplet from this blog post https://www.haproxy.com/blog/load-balancing-affinity-persistence-sticky-sessions-what-you-need-to-know/

    backend bk_web
      balance roundrobin
      cookie SERVERID insert indirect nocache
      server s1 192.168.10.11:80 check cookie s1
      server s2 192.168.10.21:80 check cookie s2
    

    @Baptiste have a rather detailed explanation for the setup HAproxy 1.5.8 How do I configure Cookie based stickiness?

    When you use more the one HAProxy servers can you sync the state via the peers protocol which is described in this blog post.
    https://www.haproxy.com/blog/introduction-to-haproxy-stick-tables/