concept sticky session in category cloud

appears as: sticky sessions
Cloud Native Patterns: Designing change-tolerant software

This is an excerpt from Manning's book Cloud Native Patterns: Designing change-tolerant software.

  • The user is then redirected back to the User Profile app, with the new auth token. This time, the router will send the user request to a different instance of the User Profile app. 4 (Spoiler alert: sticky sessions are bad in cloud-native software!)
  • I begin by covering the benefits of deploying multiple instances of an app, and you’ll immediately see what happens when this intersects with stateful apps. I do this in the context of the cookbook example we started with in the previous chapter, breaking that monolith into individual microservices that are then independently deployed and managed. I introduce local state into one of those microservices, specifically storing authentication tokens. Yes, sticky sessions are a common pattern for addressing this type of session state, but it’s a bad idea in the cloud, and I’ll cover why. I also introduce the notion of a stateful service, a special type of service that’s carefully designed to handle the complexities of state. And finally, I’ll show you how to keep separate the parts of your software that are stateful from the parts that are stateless.

    5.3. HTTP sessions and sticky sessions

    What about sticky sessions? For almost as long as we’ve been using load balancers to distribute requests across multiple instances of applications—and this is far longer than those apps have been designed in a cloud-native manner—we’ve been using sticky sessions. Why can’t we continue to use those to deal with stateful services?

    First, let me briefly explain the technique. Sticky sessions are an implementation pattern in which an app includes a session ID in the response to a first request from a user—a fingerprint for that user, if you will. That session ID is then included, usually via a cookie, in all subsequent requests. This effectively allows the load balancer to keep track of individual users who are interacting with an app. That load balancer, which is responsible for deciding where to send a specific request, will remember which instance was first accessed and will then do its best to route all requests carrying that session ID to the same app instance. If that app instance has a local state, requests consistently routed to that instance will have that local state available.

    Figure 5.10. A load balancer supporting sticky sessions will attempt to stick specific users to specific app instances, but may be forced to send a request to a different instance. In cloud-native software, you must assume that instances come and go with some regularity. If you don’t, the user experience suffers.

    Developers have, for some time, justified the use of sticky sessions, arguing that anomalies such as disappearing instances or network outages are rare, and that when they do occur, subpar user experiences, while undesirable, are acceptable. This is a poor argument for two reasons. First, the recycling of app instances is increasingly common because of either unanticipated or deliberate changes to the infrastructure. Second, it isn’t difficult to implement something better—namely, persisting session state in a connected backing-store, and this is an approach that brings with it many other advantages. Let’s have a look at that now.