Comparison of Jedis, Redisson, and Lettuce and Their Integration with Spring Boot
This article compares the three popular Java Redis clients—Jedis, Redisson, and Lettuce—detailing their differences in threading, I/O, and feature sets, and provides comprehensive Spring Boot configuration and code examples for using StringRedisTemplate, RedissonClient, and Spring Cache annotations to implement caching and distributed locks.
Jedis, Redisson, and Lettuce are Java APIs for interacting with Redis; they share the same purpose but differ in implementation details such as blocking I/O for Jedis, distributed lock and collection support in Redisson, and asynchronous, Netty‑based, thread‑safe operations in Lettuce.
The article outlines the specific characteristics of each client: Jedis uses synchronous blocking I/O and requires a connection pool because its instances are not thread‑safe; Redisson offers distributed locks, collections, and delayed queues; Lettuce provides thread‑safe, asynchronous calls, supports clustering, Sentinel, pipelining, and uses Netty for event‑driven communication.
Configuration examples are given for Maven dependencies (spring‑boot‑starter‑data‑redis, redisson, redisson‑spring‑boot‑starter) and for application‑dev.yml settings, showing host, port, password, and database selection.
org.springframework.boot
spring-boot-starter-data-redis
org.redisson
redisson
3.8.2
true
org.redisson
redisson-spring-boot-starter
LATESTSample usage of StringRedisTemplate is demonstrated, including a method that retrieves a hash entry, updates the cache if missing, and converts JSON to a Java object.
@Resource
private StringRedisTemplate stringRedisTemplate;
@Override
public CustomersEntity findById(Integer id) {
// cache lookup
String toString = stringRedisTemplate.opsForHash().get(REDIS_CUSTOMERS_ONE, id + "").toString();
if (toString != null) {
return JSONUtil.toBean(toString, CustomersEntity.class);
}
// fallback to DB and cache result
Optional
byId = customerRepo.findById(id);
if (byId.isPresent()) {
CustomersEntity customersEntity = byId.get();
stringRedisTemplate.opsForHash().put(REDIS_CUSTOMERS_ONE, id + "", JSONUtil.toJsonStr(customersEntity));
return customersEntity;
}
return null;
}For Redisson, the article provides Maven coordinates, a YAML configuration file ( redisson-config.yml ) and its JSON equivalent, and a Java @Configuration class that loads the configuration via Config.fromYAML or Config.fromJSON .
# Redisson configuration (YAML)
singleServerConfig:
address: "redis://192.168.1.140:6379"
password: null
clientName: null
database: 15
idleConnectionTimeout: 10000
pingTimeout: 1000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
...A REST controller example shows how to use RedissonClient for basic string operations, hash storage, atomic long counters, and distributed locks, with full source code wrapped in ... tags.
@RestController
@RequestMapping("/")
public class TeController {
@Autowired
private RedissonClient redissonClient;
@GetMapping("/set/{key}")
public String s1(@PathVariable String key) {
RBucket
bucket = redissonClient.getBucket(key);
bucket.set(key + "1-v1");
return key;
}
// additional endpoints for get, hash set/get, atomic operations, and lock testing
}The article also covers Spring Cache integration: a RedisCacheConfig class configures key/value serializers, TTL, and disables null caching; service methods are annotated with @Cacheable, @CachePut, and @CacheEvict to demonstrate caching of single entities, lists, and cache invalidation.
@EnableCaching
@Configuration
public class RedisCacheConfig {
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer
redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer
jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
// object mapper configuration omitted for brevity
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(timeToLive)
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
return RedisCacheManager.builder(factory).cacheDefaults(config).build();
}
}Finally, the article lists additional resources, images, and a disclaimer stating that the material is collected from the internet for learning purposes only.
IT Architects Alliance
Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.
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.