Master Redis in Spring Boot: From RedisTemplate to Redisson Integration
This tutorial walks Java developers through using Redis in Spring Boot projects, covering the essential spring-boot-starter-data-redis dependency, RedisTemplate basics, the underlying RedisConnectionFactory design, custom connection configurations for single‑node and sentinel modes, and seamless integration of the Redisson client for advanced distributed features.
In modern application development Redis is a crucial speed‑up component for caching, ranking, session storage, message queues, rate limiting and more.
Dependency Introduction
The core dependency is spring-boot-starter-data-redis, which enables immediate use of RedisTemplate after inclusion.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.5.14</version>
</dependency>Principle Analysis
RedisTemplatecomes from the spring-data-redis library. It operates on a RedisConnectionFactory which supplies the actual RedisConnection similar to how JDBC provides a Connection for SQL.
public interface RedisConnectionFactory extends PersistenceExceptionTranslator {
RedisConnection getConnection();
RedisClusterConnection getClusterConnection();
boolean getConvertPipelineAndTxResults();
RedisSentinelConnection getSentinelConnection();
}The two main implementations are JedisConnectionFactory and LettuceConnectionFactory; Spring Boot defaults to Lettuce because of its superior performance.
How to Configure the Connection
Customizing RedisConnectionFactory and RedisTemplate parameters can be done via a @Bean method:
@Bean("stringObjectRedisTemplate")
public RedisTemplate<String, Object> stringObjectRedisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(RedisSerializer.json());
redisTemplate.setHashValueSerializer(RedisSerializer.json());
return redisTemplate;
}1. Single‑Node Mode
spring:
application:
name: myapplication
redis:
host: 127.0.0.1
port: 6379
database: 0
password: 123456
lettuce:
pool:
min-idle: 2
max-idle: 4
max-active: 10
max-wait: -1
time-between-eviction-runs: 12. Sentinel (Cluster) Mode
spring:
redis:
sentinel:
master: mymaster
nodes:
- 192.168.1.101:26379
- 192.168.1.102:26379
- 192.168.1.103:26379
password: yourpassword
database: 0Integrating Redisson Client
Redisson provides a richer API that resembles Java collections, useful for distributed locks, Bloom filters, maps, etc. Add the starter dependency:
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.38.1</version>
</dependency>Configure a RedissonClient bean:
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password);
return Redisson.create(config);
}Optionally expose a RedissonConnectionFactory for Spring Data Redis compatibility:
@Bean
public RedissonConnectionFactory redissonConnectionFactory(RedissonClient client) {
return new RedissonConnectionFactory(client);
}Version Compatibility
Spring Boot 2.x works well with redisson-spring-boot-starter 3.x. For Spring Data Redis 2.5.x, use redisson-spring-data-25 to avoid conflicts.
Utility Wrapper
To make Redis usage truly plug‑and‑play, a utility class can encapsulate common commands for strings, hashes, lists, sorted sets, bitmaps, and Lua scripts.
By combining RedisTemplate for simple key‑value operations and RedissonClient for advanced distributed structures, developers gain a flexible and powerful Redis integration in Spring Boot.
Lin is Dream
Sharing Java developer knowledge, practical articles, and continuous insights into computer engineering.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
