Session Affinity now Generally Available

Today we are announcing that Session Affinity routing is now generally available as a fully supported part of the Heroku Platform. When Session Affinity is enabled for an app, requests from a given browser will always be routed to the same Dyno. Under the hood, the Heroku Router will add a cookie to all incoming requests from new clients; this cookie allows subsequent requests from that client to return to the same Dyno. With specific clients bound to specific Dynos, apps that depend on ‘sticky sessions’ can still take advantage of Heroku’s flexible scaling.

We introduced Session Affinity in Heroku Labs last April. Since then, many customers have built apps with the new routing strategy and feedback has been overwhelmingly positive. It’s now battle-tested and stable, so that you can use it confidently in any of your Heroku apps.

When Should You Use Session Affinity?

Normally, Heroku Routers use a random selection algorithm for balancing requests across web dynos. Distributing requests to different Dynos allows all the servers in your app to balance the load, and is generally a good default.

However, stateful servers may need a different routing strategy. If you prefer not to keep state in Redis, a database, S3, or other storage, sticky sessions may be your solution. This has been a frequent request from Tomcat, Lift, and Express-Session users. Similarly, real-time systems like, SockJS, and Meteor require local state to emulate a persistent WebSocket connection.

With Session Affinity enabled, each request from a particular client will be handled by the same Dyno. When new Dynos are created or old Dynos are removed, attached clients will be provided with an updated cookie that points at an available Dyno. This allows real-time apps and apps with in-memory state to scale as usual on Heroku.

Keep in mind that Session Affinity is only one part of the persistence equation. For instance, with, using multiple nodes requires session affinity, Node-Cluster routing, and an intra-node messaging adapter.

Getting Started with Session Affinity

You can enable Session Affinity on an app by running heroku features:enable http-session-affinity -a {appname}. Session Affinity is available for applications using the Common Runtime and is not currently available in Private Spaces. To learn more, check out the docs.

Browse the blog archives, subscribe to the full-text feed, or visit the engineering blog.