Redis cache implementation using Jedis for SpringBoot application

Chanaka MBK
Aeturnum
Published in
4 min readJan 4, 2021

--

This post will show you how to integrate caching DB into your SpringBoot application. There are 2 ways to do this implementation. One is using the Jedis library and the other one is spring framework related dependency called spring-data-redis library. In this post, I’m going to use the Jedis library. I will post an article about using spring-data-redis library soon.

Redis is an open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Here we are using it for caching purposes.

In the industry, we are using Redis for caching purposes. It helps increase application efficiency. For example, let say we are going to retrieve specific data from 3rd party API or Database. If we can cache particular responses at the time of the first retrieving, we can send those cached data when the user requests the same data later.

So we don’t want to connect DB or 3rd party API again and again. It will directly impact application performance.

In this example, I’m going to use 2 endpoints called save employee object and retrieve employee object. Here I’m going to save employee objects to cache DB and retrieving from cache DB. Employee object will be the value and Employee Id will be a key in the cache DB. Please refer to the following sequence diagrams to get a better idea of this example. Please visit here to get a completed example from bitbucket.

Non-Cache Flow

Non-Cache flow

Cache Flow

Cache flow

Technologies

Java 1.8
Maven 3.6.1
Springboot 2.2.7.RELEASE
MongoDB
jedis 2.9.0
gson 2.8.3
log4j

Project Structure

Project Structure

First, we have to double-check whether controller endpoints are working fine or not. We can use Postman or any other third-party tool to execute endpoints.
After that, we have to add jedis dependency.

STEP 1: Maven Related Dependency (POM.xml)

STEP 2: Add a configuration file

We have to define a configuration class to initialize the Jedis object. To this initialization, I’m using a class called RedisCacheConfig.
First, need to initialize the JedisPoolConfig object and need to add custom values for Redis host, Redis Port, Redis Timeout(TTL), and Redis Maximum Active Connection Count, etc.
Those are picking up from the property file. Also, Redis host, Redis Timeout, and Redis Maximum Active Connection Count will change according to the application and the requirement. Please see the example class I have explained each line with a comment.

STEP 3: Add Service class for Caching implementation

Here I have defined 4 methods that we are going to use for the CRUD operations. Please see the RedisCacheService and RedisCacheServiceImpl.In this class, I have defined 4 methods

  • To store employee objects.
  • To retrieve an employee object using an employee id.
  • To flush an employee object using the id.
  • To flush all the data.

Please visit the classes to see the explanation in the comment section.

STEP 4: Service layer integration.

Now we can use store and retrieve methods inside of the EmployeeServiceImpl class.

Store into the Cache DB
Retrieve from the Cache DB

STEP 5: Testing

Once you are done with the changes you can test both endpoints using Postman. Very first endpoint call will be called to the MongoDB and later calls will access Redis cached data.

In this example, I have used the 1200 seconds for TTL(Time To Live) value. So after 1200 seconds, cached data will automatically be removed.

Postman requests for Create Employee and Retrieve Employee :

Cached Results :

Please visit a completed version from here and I have explained every possible step in the comment section. Please leave a comment if you have concerns or questions.

--

--

Chanaka MBK
Aeturnum

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry. Skilled in JAVA,Spring Boot, Angular.