Search code examples
cluster-computingreverse-proxytraefik

Persistent & clustered connections with traefik reverse proxy


Let's say I have a cluster of database replicas that I would like to make available under a frontend. These databases replicate with each other. Can I have Traefik serve the same backend to the same client IP if possible, such that the UI can be made consistent even when the DBs are still replicating the newest state?


Solution

  • What you seem to be asking for is sticky sessions (aka session affinity) on a per-IP address basis.

    Traefik supports cookie-based stickiness, which means that a cookie will be assigned on the initial request if the relevant Traefik option is enabled. Subsequent requests will then reach the same backend unless it fails to be reachable, at which point a new sticky backend will be selected.

    The option can be enabled like this:

    [backends]
      [backends.backend1]
        [backends.backend1.loadbalancer]
          sticky = true
    

    Documentation can be found here (search for "sticky sessions").

    If you are running one of the dynamic providers with Traefik (e.g., Docker, Kubernetes, Marathon), there are usually labels/tags/annotations available you can set per-frontend. The TOML configuration file documentation contains all the details.

    If you are looking for true IP address-based stickiness where the IP address space gets hashed and traffic evenly distributed across all backends: This isn't possible yet, although there's an open feature request.