Implement Redis Cache in Spring Boot Application — Part I (Theory)

Paul issack minoltan
4 min readMay 22, 2022

--

Hello Everyone…!

Why?

Most of the time if we are looking for the best performance from out application we are looking into many techniques. Apart from several other solutions, we also look for a caching technique to make DB calls faster. In order to achieve, we have Redis Cache technique in place. Redis cache helps us by minimizing the number of network calls while accessing the data from DB.

How?

Spring Boot supports this feature via the dependency ‘Spring Data Redis’. In addition, we need to download Redis Server to make Redis Cache functional in the Spring Boot Application. Moreover, apart from Cache, Redis can also be used as a database and Message Broker. However, in a real time application, Redis is popular for a Cache Manager as compared to database & Message Broker. Let’s start discussing the topic of our article ‘How to implement Redis Cache in Spring Boot Application’ and its related concepts.

Where we can use?

We can use Redis from most programming languages. Redis is written in ANSI C and works in most POSIX systems like Linux, *BSD, and OS X, without external dependencies. Linux and OS X are the two operating systems where Redis is developed and tested the most.

What is Redis?

Redis is an open source(BSD licensed) in-memory remote data structure store(database) that offers high performance, replication, and a unique data model. The full form of Redis is Remote Directory Server. Moreover, we are using it in multiple forms.

Multiple forms

We can use Redis in the following forms.

  1. In-Memory Database : As an In-Memory database, We will get some empty memory to perform database operations. Moreover, it acts as No-SQL database and there are No Tables, No Sequences, No Joins concept. We can store data in the form of String, Hash Operations***, List, Set etc. In-built services will be available.
  2. Cache : We can also use Redis as a Cache to increase our application performance.
  3. Message Broker(MQ) : Another use of Redis is as a Message Broker.

In real time application, Redis is popular for a Cache Manager as compared to database. As a cache manager, It reduces network calls and improves the performance of an application.

What is Redis Cache?

Redis Cache is nothing but a Cache Management feature offered by Redis. Redis is normally used as a cache to store repeatedly accessed data in memory so that the user can feel the better performance of the application. The Redis Cache offers various features like how long you want to keep data, and which data to remove first, and some other bright caching models.

What is the advantage of using Redis Cache in your application?

Like any other Caching Technique, Redis Cache also minimizes the number of network calls made by your application, which in return improves performance of the application as a whole. One request from an application to the DB is similar to one network call.

How does the Redis Cache work in the Application?

When we perform a DB retrieve operation via an Application, the Redis Cache stores the result in it’s cache. Further, when we perform the same retrieve operation, it returns the result from the cache itself and ignore the second call to database. Similarly, when we perform a DB update operation, the Redis Cache also updated the result in its cache. Needless to say, for delete operation also it deleted the data from the cache accordingly. In this way, there are no chances of getting incorrect data.

What is Redis Database?

Redis Database is an in-memory database that persists on disk. It means when we use Redis Database, we occupy a memory on the disk to use as a Database. The data model is key-value, but many several kind of values are supported such as Strings, Lists, Sets, Sorted Sets, Hashes, Streams, HyperLogLogs, Bitmaps etc.

What is Redis Server?

The full form of Redis is REmote DIctionary Server. When we use Redis in any form such as database, cache or Message Broker, we need to download a Redis Server in our system. People in the industry just call it Redis Server. Also we can use the docker and pull the redis image rather than download.

Note : To pull using docker you have to install docker and run it on your local machine. To learn more about docker, visit Introduction to Docker.

What are the important annotations to enable Redis Cache in the Application?

Generally, there are four important annotations that we apply to implement Redis Cache feature in our application. They are as below:

@EnableCaching

We apply this annotation at the main class (starter class) of our application in order to tell Spring Container that we need Caching feature in our application.

@Cacheable

@Cacheable is used to fetch(retrieve) data from the DB to application and store in Redis Cache. We apply it on the methods that get (retrieve) data from DB.

@CachePut

We use @CachePut in order to update data in the Redis Cache while there is any update of data in DB. We apply it on the methods that make modifications in DB.

@CacheEvict

We use @CacheEvict in order to remove data in the Redis Cache while there is any removal of data in DB. We apply it on the methods that delete data from DB.

Conclusion

I hope that after going through all the theoretical concepts, you got better understanding about Redis Cache in a Spring Boot Application. To get practical experience please look in to Implement Redis Cache in Spring Boot Application — Part II

Cheers…! Bye..!

--

--