Cloudflare Docs
Load Balancing
Visit Load Balancing on GitHub
Set theme to dark (⇧+D)

Session affinity

When you enable session affinity, your load balancer directs all requests from a particular end user to a specific origin server. This continuity preserves information about the user session — such as items in their shopping cart — that might otherwise be lost if requests were spread out among multiple servers.

Session affinity can also help reduce network requests, leading to savings for customers with usage-based billing.

Session Affinity automatically directs requests from the same client to the same origin web server:

  1. When a client makes its first request, Cloudflare sets a cflib cookie on the client (to track the associated origin web server).
  2. Subsequent requests by the same client are forwarded to that origin for the duration of the cookie and as long as the origin server remains healthy.
  3. If the cookie expires or the origin server becomes unhealthy, Cloudflare sets a new cookie tracking the new failover origin.

All sessions default to 23 hours unless you set a custom session Time to live (TTL).

The session cookie is secure when Always Use HTTPS is enabled. Additionally, HttpOnly is always enabled for the cookie to prevent cross-site scripting attacks.

​​ Session affinity by type

Session affinity specifies the type of session affinity the load balancer should use unless specified as "none" or "" (default).

On the first request to a proxied load balancer, a cookie is generated, encoding information of which origin the request will be forwarded to. Subsequent requests, by the same client to the same load balancer, will be sent to the origin server the cookie encodes for the duration of the cookie and as long as the origin server remains healthy. If the cookie has expired or the origin server is unhealthy, a new origin server is calculated and used.

This behaves similar to "cookie" except the initial origin selection is stable and based on the client’s IP address.

The initial origin selection is stable and based on specific HTTP headers found in the first request. Subsequent requests, by clients with the same HTTP headers to the same load balancer, will be sent to the same origin server for the duration of the session and as long as the origin server remains healthy. If the origin server becomes unhealthy, a different stable origin server will be used until the original origin server recovers. A new origin server is calculated and used only after the session has not been used for session_affinity_ttl seconds.


​​ Enabling Session Affinity from the Cloudflare dashboard

Enable Session Affinity when you create or edit a load balancer, during the Hostname step.

If you enable Session Affinity, choose one of the following options:

  • By Cloudflare cookie only: Sets a cflib cookie to track the associated origin web server
  • By Cloudflare cookie and Client IP fallback: Sets a cflib cookie, but also uses the client IP address when no session affinity cookie is provided

​​ Enabling Session Affinity via the Cloudflare API

Session affinity is a property of load balancers, which you can set with the following endpoints:

Customize the behavior of session affinity by using the session_affinity, session_affinity_ttl, and session_affinity_attributes parameters.

To enable session affinity by HTTP header, set the session_affinity value to header and session_affinity_attributes.headers must have at least one HTTP header name.

For more details on API commands in context, refer to Create a load balancer with the API.


​​ Origin Drain

Drain or remove all traffic from an origin without affecting any active customers using Origin Drain. For more details on origin drain, refer to Performing planned maintenance.

​​ Zero-Downtime Failover

Zero-Downtime Failover automatically sends traffic to origin servers within a pool during transient network issues. This helps reduce errors shown to your users when issues occur in between active health monitors.

You can enable one of three options:

  • None: No failover will take place and errors may show to your users.
  • Temporary: Traffic will be sent to other origin(s) until the originally pinned origin is available.
  • Sticky: The session affinity cookie is updated and subsequent requests are sent to the new origin moving forward as needed.