Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

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're going to 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, we can use it as a cache in our microservices architecture. 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 gives us great support for 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.2.6.RELEASE</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:

spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=16379
spring.redis.password=mypass
spring.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);
}

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

Another important feature is since 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 over on GitHub.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!