Home » Java » Enterprise Java » Using Redis with Spring

About Andrey Redko

Andrey Redko
Andriy is a well-grounded software developer with more then 12 years of practical experience using Java/EE, C#/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).

Using Redis with Spring

As NoSQL solutions are getting more and more popular for many kind of problems, more often the modern projects consider to use some (or several) of NoSQLs instead (or side-by-side) of traditional RDBMS. I have already covered my experience with MongoDB in this, this and this posts. In this post I would like to switch gears a bit towards Redis, an advanced key-value store.
Aside from very rich key-value semantics, Redis also supports pub-sub messaging and transactions. In this post I am going just to touch the surface and demonstrate how simple it is to integrate Redis into your Spring application. As always, we will start with Maven POM file for our project:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">







Spring Data Redis is the another project under Spring Data umbrella which provides seamless injection of Redis into your application. The are several Redis clients for Java and I have chosen the Jedis as it is stable and recommended by Redis team at the moment of writing this post.
We will start with simple configuration and introduce the necessary components first. Then as we move forward, the configuration will be extended a bit to demonstrated pub-sub capabilities. Thanks to Java config support, we will create the configuration class and have all our dependencies strongly typed, no XML anymore:
package com.example.redis.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

public class AppConfig {
 JedisConnectionFactory jedisConnectionFactory() {
  return new JedisConnectionFactory();

 RedisTemplate< String, Object > redisTemplate() {
  final RedisTemplate< String, Object > template =  new RedisTemplate< String, Object >();
  template.setConnectionFactory( jedisConnectionFactory() );
  template.setKeySerializer( new StringRedisSerializer() );
  template.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
  template.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
  return template;
That’s basically everything we need assuming we have single Redis server up and running on localhost with default configuration. Let’s consider several common uses cases: setting a key to some value, storing the object and, finally, pub-sub implementation. Storing and retrieving a key/value pair is very simple:
@Autowired private RedisTemplate< String, Object > template;

public Object getValue( final String key ) {
    return template.opsForValue().get( key );

public void setValue( final String key, final String value ) {
    template.opsForValue().set( key, value );

Optionally, the key could be set to expire (yet another useful feature of Redis), f.e. let our keys expire in 1 second:

public void setValue( final String key, final String value ) {
    template.opsForValue().set( key, value );
    template.expire( key, 1, TimeUnit.SECONDS );

Arbitrary objects could be saved into Redis as hashes (maps), f.e. let save instance of some class User

public class User {
 private final Long id;
 private String name;
 private String email;
    // Setters and getters are omitted for simplicity

into Redis using key pattern “user:<id>”:

public void setUser( final User user ) {
 final String key = String.format( "user:%s", user.getId() );
 final Map< String, Object > properties = new HashMap< String, Object >();

 properties.put( "id", user.getId() );
 properties.put( "name", user.getName() );
 properties.put( "email", user.getEmail() );

 template.opsForHash().putAll( key, properties);

Respectively, object could easily be inspected and retrieved using the id.

public User getUser( final Long id ) {
 final String key = String.format( "user:%s", id );

 final String name = ( String )template.opsForHash().get( key, "name" );
 final String email = ( String )template.opsForHash().get( key, "email" );

 return new User( id, name, email );
There are much, much more which could be done using Redis, I highly encourage to take a look on it. It surely is not a silver bullet but could solve many challenging problems very easy. Finally, let me show how to use a pub-sub messaging with Redis. Let’s add a bit more configuration here (as part of AppConfig class):
MessageListenerAdapter messageListener() {
 return new MessageListenerAdapter( new RedisMessageListener() );

RedisMessageListenerContainer redisContainer() {
 final RedisMessageListenerContainer container = new RedisMessageListenerContainer();

 container.setConnectionFactory( jedisConnectionFactory() );
 container.addMessageListener( messageListener(), new ChannelTopic( "my-queue" ) );

 return container;
The style of message listener definition should look very familiar to Spring users: generally, the same approach we follow to define JMS message listeners. The missed piece is our RedisMessageListener class definition:
package com.example.redis.impl;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

public class RedisMessageListener implements MessageListener {
 public void onMessage(Message message, byte[] paramArrayOfByte) {
  System.out.println( "Received by RedisMessageListener: " + message.toString() );
Now, when we have our message listener, let see how we could push some messages into the queue using Redis. As always, it’s pretty simple:
@Autowired private RedisTemplate< String, Object > template;

public void publish( final String message ) {
  new RedisCallback< Long >() {
   @SuppressWarnings( "unchecked" )
   public Long doInRedis( RedisConnection connection ) throws DataAccessException {
    return connection.publish(
     ( ( RedisSerializer< String > )template.getKeySerializer() ).serialize( "queue" ),
     ( ( RedisSerializer< Object > )template.getValueSerializer() ).serialize( message ) );

That’s basically it for very quick introduction but definitely enough to fall in love with Redis.

Reference: Using Redis with Spring from our JCG partner Andrey Redko at the Andriy Redko {devmind} blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....




  1. Hi Andrey,
    Thanks for this post. could you help me to have a sample code for key expiration notification using spring data redis. I tried to look in to http://redis.io/topics/notifications, but it didn’t help me out. Your RedisMessageListener looks closer to what I was looking for. but I could not co-relate it with expire.

    • Hi Amit,

      Thank you for your comment. Right, key expiration notification became available in Redis 2.8.0, way after this blog post has been published. Essentially, I think once you run Redis with notifications enabled, you can subscribe on __keyspace@* or __keyevent@* topics to get notified about expired keys. Is it what you have tried and it didn’t work out?

      Thank you.

      Best Regards,
      Andriy Redko

      • Hi Andriy,
        Thanks for reply. No I couldn’t get that how to use it in my code. I am trying to write a service which stores all the social count for my site in redis for next two minutes and serves the result from redis as per request. But some how when my keys expire after 2 minutes, and my service doesn’t have way to know, if key is already expired and it should insert the new results. That is why I was planning to implement notification on expire. I read in redis documentation , if you want redis DB to expire the keys then we can try to access it and redis will delete these keys immediately if those are timed out. So I am planning to use this trick.. I am not sure, if it will be costly operation then notification implementation. So wanted your advice on the same. it will be great,if u can help me….

        Here is sample code. It will be great , if you can help me.

        private StringRedisTemplate redisTemplateObj;

        //Accessing KEY using StringRedisTemplate.getExpire so that it doesn’t try to insert same key again…..
        if (!(0 < redisTemplateObj.getExpire(key))) {


        String fbJsonString = utilObj.convertObjectToJson(getFbSocialObject());
        String twJsonString = utilObj.convertObjectToJson(getTwSocialObject());

        String key = CommonConstants.REDIS_KEY_PREFIX + pageURL;
        final Map properties = new HashMap();
        properties.put(CommonConstants.FB_COUNT, pgObj.getFbCount());
        properties.put(CommonConstants.TW_COUNT, pgObj.getTwCount());
        properties.put(CommonConstants.TOTAL_SOCIAL_COUNT, pgObj.getTotalSocialCount());
        properties.put(CommonConstants.FB_SOCIAL_OBJECT, fbJsonString);
        properties.put(CommonConstants.TW_SOCIAL_OBJECT, twJsonString);
        redisTemplateObj.opsForHash().putAll(key, properties);

        redisTemplateObj.expire(key, 2, TimeUnit.MINUTES);


        • Hi Amit,

          Thanks a lot for providing more details. It is actually very easy to enable Redis key expiration notifications in Redis using configuration or just running this command in Redis CLI:

          CONFIG SET notify-keyspace-events KEx

          On the Spring/Java side, you just create pattern topic and subscribe either to __keyspace or __keyevent, whatever is easier to you, for example:

          RedisMessageListenerContainer redisNotificationsContainer() {
          final RedisMessageListenerContainer container = new RedisMessageListenerContainer();

          container.setConnectionFactory( jedisConnectionFactory() );
          container.addMessageListener( messageListener(), new PatternTopic( “__keyspace@*” ) );

          return container;

          With that, you should be able to consume key expiration events from Redis (you also can limit it to one database, f.e. __keyspace@0__:*). Please let me know if it works for you.

          Thank you.

          Best Regards,
          Andriy Redko

Leave a Reply

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


Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Advanced Java Guide
  • Java Design Patterns
  • JMeter Tutorial
  • Java 8 Features Tutorial
  • JUnit Tutorial
  • JSF Programming Cookbook
  • Java Concurrency Essentials