Hi there,

if you are looking for integrating redis cache in spring boot application for faster read and write operations then this blog is for you.

First, what is Redis and in-memory database?
When your application scales, your API calls that fetching data from the relational databases like MySQL, Oracle, PostgreSQL etc. slows down as there’s a lot of data to pick from. Here Redis comes into picture, so Redis is:

  • NoSQL database where no SQL (Structured Query Language) is used to store data that is, In redis, there are no relation such as tables and relationship exist between the data.
  • It is in-memory database means, data reside in your main memory which is obviously faster to retrieve than compared to the disk storage.

Now you are saying, hey what it uses to store data? So,

  • It has four data structure: Strings, Sets, Hashes (which is map) and sorted sets (which is a set with score associated with it).
  • Set is a data structure which stores only unique values.
  • Hash is a data structure which stores a key-value pair.
  • sorted set is a set which is sorted with respect to score.

Now, you have clear idea of what redis is and why it is fast enough in terms of scalability, so for the installation click here

No matter what library you are using, you need Redis client to connect to redis. There are two popular clients available out there:

  • Lettuce and
  • Jedis

Use can quickly checkout in what features they differ, in my scenario, lettuce worked fine so I’ll be using spring data redis along with Lettuce client in this blog.

add these dependency to your pom.xml file,
path: src -> resources -> pom.xml


In this spring data redis, we have RedisTemplate interface which takes care of all redis things. It is easy to use, otherwise opening and closing connections in redis is painful, as it single threaded.
Now, lets create beans for RedisTemplate with lettuce client,

    public LettuceConnectionFactory lettuceConn(){
        return new LettuceConnectionFactory(new          RedisStandaloneConfiguration("localhost", 6379));

    public RedisTemplate<String, String> redisTemplate(){
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        return redisTemplate;

Now your redis server is listening on localhost:6379. You have successfully created bean for redisTemplate which you can @autowired in your repository. Lets take an example by creating a demo repository:

package com.demo.repository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.stereotype.Repository;

import java.util.*;

public class DemoRepository{

    RedisTemplate redisTemplate;

    public void addItemToSet(String setName, String value){
       redisTemplate.opsForSet().add(setName, value);
    public Set<String> getAllFromSet(String setName){
       return redisTemplate.opsForSet().get(setName);
    public void addItemToHash(String hashName, String hashKey, String value){
       redisTemplate.opsForHash().put(hashName, hashKey, value);
    public Map<String, String> getAllFromHash(String hashName){
       return redisTemplate.opsForHash().entries(hashName);
    public void addItemToZSet(String setName, String value, double score){
       redisTemplate.opsForZSet().add(setName, value, score);
    public Set<DefaultTypedTuple> getItemFromZSet(String setName){
       return redisTemplate.opsForZSet().rangeWithScore(setName, 0, -1);

these are the few commands in Redis, you can explore more here.

Hope this help! Happy coding 🙂

Leave a comment

Your email address will not be published. Required fields are marked *