1. Overview

One of the main attractions of Spring Boot is how it often reduces third-party configuration to just a few properties.

In this tutorial, we will see how Spring Boot simplifies working with Redis.

2. Why Redis?

Redis is one of the most popular in-memory data structure stores. For this reason, it can be used as a database, cache, and message broker.

In terms of performance, it is well known because of its fast response time. As a result, it can serve hundreds of thousands of operations per second and is easily scalable.

And it pairs well with Spring Boot applications. For example, our microservices architecture can use it as a cache. We can also use it as a NoSQL database.

3. Running Redis

To get started, let’s create a Redis instance using their official Docker image.

$ docker run -p 16379:6379 -d redis:6.0 redis-server --requirepass "mypass"

Above, we’ve just started an instance of Redis on port 16379 with a password of mypass.

4. Starter

Spring greatly supports connecting our Spring Boot applications with Redis using Spring Data Redis.

So, next, let’s make sure we’ve got the spring-boot-starter-data-redis dependency in our pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.7.11</version>    
</dependency>

5. Lettuce

Next, let’s configure the client.

The Java Redis client we’ll use is Lettuce since Spring Boot uses it by default. However, we could have also used Jedis.

Either way, the result is an instance of RedisTemplate:

@Bean
public RedisTemplate<Long, Book> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<Long, Book> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);
    // Add some specific configuration here. Key serializers, etc.
    return template;
}

6. Properties

When we use Lettuce, we don’t need to configure the RedisConnectionFactory. Spring Boot does it for us.

All we have left, then, is to specify a few properties in our application.properties file (for Spring Boot 2.x):

spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=16379
spring.redis.password=mypass
spring.redis.timeout=60000

For Spring Boot 3.x, we need to set the following properties instead:

spring.data.redis.database=0
spring.data.redis.host=localhost
spring.data.redis.port=16379
spring.data.redis.password=mypass
spring.data.redis.timeout=60000

Respectively:

  • database sets the database index used by the connection factory
  • host is where the server host is located
  • port indicates the port where the server is listening
  • password is the login password for the server, and
  • timeout establishes the connection timeout

Of course, there are a lot of other properties we can configure. The complete list of configuration properties is available in the Spring Boot documentation.

7. Demo

Finally, let’s try using it in our application. If we imagine a Book class and a BookRepository, we can create and retrieve Books, using our RedisTemplate to interact with Redis as our backend:

@Autowired
private RedisTemplate<Long, Book> redisTemplate;

public void save(Book book) {
    redisTemplate.opsForValue().set(book.getId(), book);
}

public Book findById(Long id) {
    return redisTemplate.opsForValue().get(id);
}

Lettuce will manage serialization and deserialization by default for us, so there’s nothing more to do now. However, it’s good to know that this also can be configured.

Another important feature is that RedisTemplate is thread-safe, so it’ll work properly in multi-threaded environments.

8. Conclusion

In this article, we configured Spring Boot to talk to Redis via Lettuce. And we achieved it with a starter, a single @Bean configuration, and a handful of properties.

To wrap up, we used the RedisTemplate to have Redis act as a simple backend.

The full example can be found on GitHub.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
res – Persistence (eBook) (cat=Persistence)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.