Spring Boot Redis Cache With Examples

Spring Boot Redis Cache

In this post, we will setup up a sample Spring boot application and integrate it with Redis Cache. While Redis is an Open source in-memory data structure store, used as a database, cache and message broker, this lesson will demonstrate only the caching integration.

We will make use of Spring Initializr tool for quickly setting up the project.

Spring Boot Redis Project Setup

We will make use of Spring Initializr tool for quickly setting up the project. We will use 3 dependencies as shown below:
Download the project and unzip it. We have used H2 database dependency as we will be using an embedded database which loses all data once the application has been stopped.

Spring Boot Redis Cache Maven Dependencies

Though we already completed the setup with the tool, if you want to set it up manually, we use Maven build system for this project and here are the dependencies we used:

Make sure to use stable version for Spring Boot from the maven central.

Defining the Model

To save an object into the Redis database, we define a Person model object with basic fields:

It is a standard POJO with getters and setters.

Configuring Redis Cache

With Spring Boot and the required dependency already in work with Maven, we can configure local Redis instance with only three lines in our application.properties file as:

Also, use the @EnableCaching annotation on Spring Boot main class:

We have added a CommandLineRunner as we want to populate some sample data in the embedded H2 database.

Defining the Repository

Before we show how Redis works, we will just define a Repository for JPA related functionality:

It has no method calls as of now as we don’t need any.

Defining the Controller

Controllers are the place where Redis cache is called for action. Actually, this is the best place to do so because as a cache is directly associated with it, the request won’t even have to enter the service code to wait for cached results.

Here is the controller skeleton:

Now, to put something into the cache, we use @Cacheable annotation:

In the above mapping, getUser method will put a person into a cache named as ‘users’, identifies that person by the key as ‘userId’ and will only store a user with followers greater than 12000. This makes sure that cache is populated with users who are very popular and are often queried for.

Also, we have intentionally added a log statement in the API call. Let’s make some API calls form Postman at this moment. These are the calls we made:

If we notice the logs, these will be it:

Notice something? We made four API calls but only three log statements were present. This is because the User with ID 2 is having 29000 followers and so, it’s data was cached. This means that when an API call was made for it, the data was returned from the cache and no DB call was made for this!

Updating Cache

Cache values should also update whenever their actual objects value are updated. This can be done using @CachePut annotation:

With this, a person is again identified by his ID and is updated with the results.

Clearing Cache

If some data is to be deleted from actual Database, there won’t be a point to keep it in cache anymore. We can clear cache data using @CacheEvict annotation:

In the last mapping, we just evicted cache entries and did nothing else.

Running Spring Boot Redis Cache Application

We can run this app simply by using a single command:

Redis Cache Limits

Although Redis is very fast, it still has no limits on storing any amount of data on a 64-bit system. It can only store 3GB of data on a 32-bit system. More available memory can result into a more hit ratio but this will tend to cease once too much memory is occupied by Redis.
When cache size reaches the memory limit, old data is removed to make place for new one.


In this lesson, we looked at what power Redis Cache provides us with fast data interaction and how we can integrate it with Spring Boot with minimal and yet powerful configuration. Feel free to leave comments below.

By admin

Leave a Reply