Last updated: 2024-04-26
Using Spring Session with Spring Boot
With Spring Session, we can provide our own session management instead of using it from the embedded server. This allows us to run multiple instances of our Spring Boot application in parallel, as a user's session can be reused even if another instance is processing a request.
Spring Boot provides an auto-configuration that takes effect as soon as one of the Spring Session libraries is found. Therefore, only minimal adjustments are required depending on the setup. We can enter one of the libraries listed below in our Gradle or Maven file without a version, as this is automatically provided by Spring Boot Dependency Management.
Spring Session with a database
With org.springframework.session:spring-session-jdbc
Spring Session will persist the sessions in the connected relational or embedded database. The configured DataSource
is automatically recognized and used from the application context.
Spring Session JDBC already provides an SQL script to create the required tables SPRING_SESSION
and SPRING_SESSION_ATTRIBUTES
. While this is executed automatically for embedded databases, we have to activate the script execution for relational databases.
Creation of the required tables for a relational database
For MongoDB, org.springframework.session:spring-session-data-mongodb
should be used as a dependency instead. The document sessions
is created and used here without any additional configuration.
If our database is able to handle the additional load well, we can use Spring Session JDBC / MongoDB and avoid integrating an additional external service.
Spring Session with Redis or Hazelcast
Redis and Hazelcast provide high-performance options for persisting sessions. For Redis, in addition to the org.springframework.session:spring-session-data-redis
library, we also need org.springframework.boot:spring-boot-starter-data-redis
to establish a connection altogether. This is configured with the following property.
Connection configuration in our application.properties
For Hazelcast we need org.springframework.session:spring-session-hazelcast
as well as com.hazelcast:hazelcast-spring
. We should also create the file src/main/resources/hazelcast.yml
, which contains the cluster and other settings.
Hazelcast configuration file
If we're using integration tests for Spring Boot, we should also add a proper Testcontainers setup to our BaseIT
class.
Conclusion
Thanks to the magic of Spring Boot, a lot happens automatically and we're very quickly ready to scale. An overview of further configuration options can be found here - the relevant settings start with spring.session
. It should also be noted that the name of the session cookie changes from JSESSIONID
to SESSION
with Spring Session.
The Bootify Builder is a tool for creating Spring Boot applications with their own database schema and complex configurations - directly in the browser. In the Professional plan, you can select one of the options for Spring Session as described in this article.
See Pricing
or read quickstart