Spring Boot and Redis
From my previous blog I had explained what is caching and how it improve our program performance and scalability. Now let’s apply redis with spring boot for your basic understanding. For my later blogs I will show the actual implementation of redis as the connection between database and customer. If updated data is exists in cache then we will not open connection for database and it means that, the performance will increase.
Note that, this blog is written just for your basic understanding for how to use redis with spring boot!
Before writing code, make sure that you have already installed redis on you local machine. In my case, I am using Ubuntu and it is easy to configure redis:
You can download and install Redis on Ubuntu using the following steps:
- Install the Redis package:
sudo apt-get update
sudo apt-get install redis-server
2. Start the Redis service:
sudo service redis-server start
3. Verify the installation:
redis-cli ping
Expected output: PONG
By default, Redis is configured to listen on localhost
only. To change the configuration, edit the /etc/redis/redis.conf
file.
4. Restart the Redis service after making any changes to the configuration:
sudo service redis-server restart
But in my case localhost will be remained so the last step is optional.
These steps will install and start Redis on your Ubuntu system. But if you are using MacOS or Windows there are bunch of videos and tutorials that explain how to configure redis on your local machine. Go ahead, download and install it and then continue to our tutorial :)
Here’s an example of how to use Redis in a Spring Boot application:
Go to https://start.spring.io/ and create project with maven, default spring boot version and java version that you preffered. Choose web and lombok dependencies.
- Add Redis dependency in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. Unzip the project and open it your preffered IDE (in my case IntelliJ IDEA)
3. Configure Redis in application.properties:
spring.data.redis.port=6379
spring.data.redis.host=localhost
4. Create a RedisTemplate bean in a Configuration class:
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
5. Use RedisTemplate in a Service class to perform Redis operations:
@Service
public class RedisService {
private final RedisTemplate<String, Object> redisTemplate;
public RedisService(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
}
That’s it! You can now use Redis as a cache in your Spring Boot application. You can customize the RedisTemplate and the Redis operations as per your requirements.
Just for testing purpose let’s write simple endpoints for get and set methods that we have written above:
import io.shixseyidrin.caching.service.RedisService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
public class RedisController {
private final RedisService redisService;
@GetMapping(path = "/set")
public void set(
@RequestParam(name = "key") String key,
@RequestParam(name = "value") String value
) {
redisService.set(key, value);
}
@GetMapping(path = "/get")
public String get(@RequestParam(name = "key") String key) {
return (String) redisService.get(key);
}
}
Run your application and open chrome to test the endpoints:
http://localhost:8080/set?key=name&value=Eltac
First copy above url and send it and then call:
http://localhost:8080/get?key=name
If you see Eltac in chrome tab then it works find as we expected.
So this was basic example that how to configure redis with spring boot and in my next blogs we will apply it to real world example to show you how it increase the performance of application!
Follow me to get more about my blog updates :)