Effectively managing session state is essential to a seamless user experience. A retail website that can’t maintain an up-to-date shopping cart as a user bounces around the site, adding and removing items, isn’t particularly useful and isn’t likely to lead to a sale.
Unfortunately, managing session state - the ability to store and quickly retrieve the data users create as they navigate applications - is no easy feat. In many cases, the “user” isn’t a person manually navigating a website. Instead, its another application or service generating streams of data that need to be managed. The difficulty gets ratcheted up further, as developers must program to multiple session state protocols beyond the standard HttpSession.
A Common Abstraction for Session State Management
That’s where Spring Session comes in. Spring Session is a common abstraction for session state management. It supports many popular protocols, including HttpSession, WebSockets and WebSession. Just like other Spring Framework projects, Spring Session is opinionated. It aims to simplify session state management, so you spend less time mucking with multiple session protocols, and more time on business logic.
Experimenting with reactive programming in Spring 5.0? Great! Spring Session supports this model. (More on this later.)
“You can use Spring Session’s notion of a session as opposed to javax.servlet.http session,” says John Blum, a Spring engineer at Pivotal. “You can treat your session a little more consistently and abstractly. With Spring Session, developers can manage session state, regardless of protocol, in a single user interface using a common abstraction.”
Spring Session: How It Works
If you're familiar with the Java EE servlet specifications, then you already understand how Spring Session works.
“Spring Session is basically a servlet filter. Behind the scenes, it replaces the session that the container hands you with a session abstraction provided by Spring Session. It integrates with the container session,” Blum says. “It does this by plugging in a filter written in common Java EE standard nomenclature. Spring Session then allows the developer to easily plug in a backing store like Pivotal GemFire or Apache Geode to manage the session data in a very resilient and highly replicated way. This pattern ensures reliability and speed.”
Spring Session also provides a repository abstraction for looking up and querying session data. With this nifty feature, stateless web and mobile applications can connect sessions across multiple devices, such as a chat application. The user can start a chat on his desktop and pick up right where he left off on a mobile device!
Easily Swap Backing Stores - No Code Changes Needed
As mentioned, Spring Session supports Pivotal GemFire and Apache Geode as backing stores. Prefer Redis, Hazelcast or MongoDB? Spring Session has you covered. Which brings us to a key benefit of Spring Session: developers can switch from one backing store to another, without making any code changes. You may want to start with Redis as the caching layer. As your traffic and user base grows, you may want to switch to GemFire to take advantage of its its multi-site WAN replication capabilities. It’s a breeze with Spring Session!
Finally, with Spring Framework 5.0 brings reactive programming support to Spring Session. Reactive programming is an increasingly popular model of development in which microservices only run when triggered by specific outside events or messages.
“People are thinking in reactive functional terms,” Blum said. “So it makes sense to have a reactive front end and a reactive backend. Otherwise, you just end up with a blocking application, the lowest common denominator."
Try Spring Session Today
If you’re a Spring developer struggling with session state management, definitely go check out Spring Session. You can download Spring Session here, and dig into the documentation here. Try it out and let us know what you think.
About the AuthorFollow on Twitter Follow on Linkedin More Content by Jeff Kelly