Sunday, March 19, 2023

Spring Data Redis


The Spring Data Redis (SDR) framework makes it easy to write Spring applications that use the Redis key-value store by eliminating the redundant tasks and boilerplate code required for interacting with the store through Spring’s excellent infrastructure support.

Redis Requirements

Spring Redis requires Redis 2.6 or above and Spring Data Redis integrates with Lettuce and Jedis, two popular open-source Java libraries for Redis.

Configuring the Lettuce Connector

<!--Lettuce -->

Create a new Lettuce connection factory:
public class AppConfig {

public LettuceConnectionFactory redisConnectionFactory() {

return new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost", 6379));

Configuring the Jedis Connector


Create a new Jedis connection factory:
public class AppConfig {

public JedisConnectionFactory redisConnectionFactory() {

RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("localhost", 6379);
return new JedisConnectionFactory(config);

Simple configuration in application.yaml

# Server config
port: 9000

# Redis Config
port: 6379

Overall, Lettuce's event-driven, non-blocking architecture and thread-safety make it a good choice for high-concurrency environments, whereas Jedis' simplicity and ease of use make it a good choice for simpler applications. However, the choice between Lettuce and Jedis ultimately depends on the specific requirements of the application and the environment in which it will be deployed.


The template is, in fact, the central class of the Redis module, due to its rich feature set. The template offers a high-level abstraction for Redis interactions. 

public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;

Use the RedisTemplate to store and retrieve values
private final RedisTemplate<String, Object> redisTemplate;

public My_class(RedisTemplate<String, Object> redisTemplate) {
this.userRepository = userRepository;

The opsForValue() method of the RedisTemplate to interact with Redis as a key-value store.


  • Spring Boot 3.0.4
  • Spring Data Redis 3.0.4
  • H2 Database
  • Java 17
  • Docker
  • Maven 
  • IntelliJ IDEA

Downloading and installing Redis

docker run --name my-redis -p 6379:6379 -d redis

Configuring Redis Cache Spring Boot

Use the @EnableCaching annotation on Spring Boot main class:

public class SpringBootRedisCacheApplication implements CommandLineRunner {

private final Logger LOG = LoggerFactory.getLogger(getClass());

private final PersonRepository personRepository;

public SpringBootRedisCacheApplication(PersonRepository personRepository) {
this.personRepository = personRepository;

public static void main(String[] args) {, args);

public void run(String... args) throws Exception {

//Populating embedded database here"Current user count is {}.", personRepository.count());
Person p1 = new Person("p1","test", 25);
Person p2 = new Person("p2", "test",28);
Person p3 = new Person("p3", "test", 60);;;;"Data: {}.", personRepository.findAll());



public interface PersonRepository extends JpaRepository<Person, Long> {


@Cacheable annotation:

  • @GetMapping("/{personByRedisTemplate}") using RedisTemplate, retrieve the last person id.
  • @GetMapping("/{personId}") save in RedisTemplate and method will put a person into a cache named as ‘persons’, identifies that person by the key as ‘personId’ and age < 26.
  •  @CachePut updating Cache.
  • @CacheEvict clearing Cache.

public class PersonController {

private final RedisTemplate<String, Object> redisTemplate;

private final PersonRepository PersonRepository;

public PersonController(RedisTemplate<String, Object> redisTemplate, PersonRepository PersonRepository) {
this.redisTemplate = redisTemplate;
this.PersonRepository = PersonRepository;

public Person getPersonByRedisTemplate(){
var person = (Person) redisTemplate.opsForValue().get("personId");
return person!=null?person:null;

@Cacheable(value = "persons", key = "#personId", unless = "#result.age < 29")
public Person getPerson(@PathVariable Long personId) {

var person = PersonRepository.findById(personId).get();
redisTemplate.opsForValue().set("personId", person);
return person;


@CachePut(value = "persons", key = "")
public Person updatePersonByID(@RequestBody Person person) {;
return person;

@CacheEvict(value = "persons", allEntries=true)
public void deletePersonByID(@PathVariable Long personId) {
PersonRepository.delete(new Person(personId));


Run & Test

Run Spring Boot application with command: mvn spring-boot:run. by console, IntelliJ etc.

By curl request or Postman.
  • curl
  • curl
  • curl --location --request PUT '' \ --header 'Content-Type: application/json' \ --data '{"id" : 3, "firstname" : "p4", "lastname" : "test", "age" : 30 }'
  • curl --location --request DELETE ''

No comments:

Post a Comment

Spring Boot 3 Spring Data GraphQL CRUD

GraphQL is an open-source query language and runtime for APIs (Application Programming Interfaces). It was developed by Facebook and releas...