Session affinity (2)
Last updated Apr 23, 2026 When you enable session affinity, your load balancer directs all requests from a particular end user to a specific endpoint. 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.
Note
Session Affinity is only supported by Public Load Balancers.
How it works
Session affinity automatically directs requests from the same client to the same endpoint:
- When a client makes its first request, Cloudflare sets a
__cflbcookie on the client (to track the associated endpoint). - Subsequent requests by the same client are forwarded to that endpoint for the duration of the cookie and as long as the endpoint remains healthy.
- If the cookie expires or the endpoint becomes unhealthy, Cloudflare sets a new cookie tracking the new failover endpoint.
flowchart LR
accTitle: Session affinity process
accDescr: Session affinity directs requests from the same client to the same server.
A[Client] --Request--> B{<code>__cflb</code> cookie set?}
B -->|Yes| C[Route to previous endpoint]
C --> O2
B ---->|No| E[Follow normal routing]
E --> O2
E --Set <code>__cflb</code> cookie--> A
subgraph P1 [Pool 1]
O1[Endpoint 1]
O2[Endpoint 2]
end
All cookie-based 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.
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/learning-paths/load-balancing/planning/session-affinity/#page","headline":"Session affinity","description":"Maintain user sessions on the same server.","url":"https://developers.cloudflare.com/learning-paths/load-balancing/planning/session-affinity/","inLanguage":"en","image":"https://developers.cloudflare.com/learning-paths/load-balancing/planning/session-affinity/og.png?v=37ccb08d57ce7937","dateModified":"2026-04-23","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
description: Compare Layer 7, DNS-only, and Layer 4 balancing. title: Types of load balancers image: https://developers.cloudflare.com/learning-paths/load-balancing/planning/types-load-balancers/og.png?v=171e6b851a48178e
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/learning-paths/llms.txt
Use this file to discover all available pages before exploring further.
Docs