Aeturnum
Published in

Aeturnum

Redis cache implementation using Jedis for SpringBoot application

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.

--

--

--

Aeturnum is a software services organization based in Boston, USA

Recommended from Medium

Vue & Typescript: Handling Types for Injected Properties

ColorLS icons don’t show on Hyper.js terminal.

How To Manage State in React with Redux

How to host multiple domains on Amazon ec2 with Nginx reverse proxy

[Jogos em Directo]!!!@!!!Assistir Porto x Juve ao vivo online Tv

Enterprise React in 2018–2019

Essentials of Array

Tools, projects, and examples for FeathersJS developers in 2020

Screenshot of feathersresources.dev

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chanaka MBK

Chanaka MBK

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

More from Medium

FDC Disk #1 —Boot Disk Plus

Kubernetes Inter-pod anti-affinity and de-schedule

Integrating Compliance in SSO Application Build

About Kubernetes architecture (2)