Session Management With Spring Reactive

Some web applications need to store state and this can be done by using a session.

The idea is that a user's experience is not determined by the choice of session management technique.

Web applications generally lie behind a load balancer which sends requests in a round robin manner (assuming all servers have the same capacity) to — as the name suggests — balance the load.

Below is a common approach to load balancing a web application.

Image title

Given the above, we will look into some ways of handling a user’s session.

Sticky Sessions

When a user accesses the web application, the load balancer will send the request to one of the servers at which point a session is created, which resides on the server itself.

So if the user’s first request was sent to Server 1, then the session is created on Server 1. If the second request is also sent to Server 1 then the user's session is found and they can continue with what they were doing, however, if the second request was instead sent to Server 2 then a new session is created resulting in the user starting all over. This results in the user having two different sessions (one on each server) with the load balancer sending requests to both servers causing a chaotic user experience.

Sticky sessions can be used to avoid this undesirable scenario by configuring the load balancer to always send a session to the exact same server where it was created.

Pros

Cons

Client-Side Session Management

In this scenario, the session state is stored on the client (your web browser) in a cookie.

Since the session does not reside on the server, then sticky sessions are not needed and it also does not matter which server you are sent to as each request will have the session available to it in the form of a cookie.

An example of client-side session management can be found in the Play framework where the session is stored in a signed cookie (JWT) and added unto each subsequent request.

Spring Session does not provide a client-side session management solution. See here for more info.

Pros

Cons

Server-Side Session Management

As stated previously, one way to manage a server-side session is to use sticky sessions.

The sticky session was needed because the session resides on the actual server.

A couple of alternatives to the sticky session approach:

  1. Store the session in a backing repository like Redis, Hazelcast, or a database. This way whichever server the request ends up at will go to the backing repository to get the session state. Spring Session is a good place to start to find out more about this approach.
  2. Replicate the session across your web servers. In this case, the session still resides on the server but if one server was to go down then the session could be picked up by another running server since it was replicated. Spring Session Hazelcast is a good place to find out about this approach.

Pros

Cons

Session Clustering in Spring Webflux

Spring Webflux provides a WebSession instead of an HttpSession.

The Spring Session documentation provides support for Redis, but what if you don’t want the overhead of maintaining a Redis cluster.

What if your web application would suffice with in-memory session clustering like that provided by Hazelcast web session clustering?

This would be fine if we were using a servlet container, but we are not (Netty being the default for Webflux), and Spring Session does not currently provide a Hazelcast implementation of ReactiveSessionRepository.

But all is not lost as we can use ReactiveMapSessionRepository passing in Hazelcast’s IMap.

In-Memory Session Clustering With Hazelcast

You can find all the code in the accompanying GitHub repository here.

 

 

 

 

Top