Redis-backed sessions¶
The spring-session-data-redis module stores sessions in Redis through a reactive Redis connection, so sessions survive application restarts and are shared between all instances that point at the same Redis.
Step 1: add the dependencies¶
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
Lettuce is the required driver for reactive Redis support (see Spring Data Redis).
Step 2: configure the Redis connection factory¶
@Configuration
public class RedisConfig {
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory();
}
}
The default LettuceConnectionFactory connects to localhost:6379. To start a Redis server for local development, use the docker-compose.yml in the repository root:
Step 3: enable Redis web session¶
@EnableRedisWebSession registers a reactive session repository backed by Redis automatically — no explicit ReactiveSessionRepository bean is needed. The controller and template code is identical to the in-memory example.
Verify¶
Start the application and submit the attribute form at http://localhost:8080. Then inspect Redis directly:
You will find the session keyed under the spring:session prefix, storing the attributes and metadata.
You can explore the complete codes in spring-reactive-sample/session-data-redis.