Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:


1. Overview

This article is an introduction to Spring Data Redis, which provides the abstractions of the Spring Data platform to Redis – the popular in-memory data structure store.

Redis is driven by a keystore-based data structure to persist data and can be used as a database, cache, message broker, etc.

We'll be able to use the common patterns of Spring Data (templates, etc.), while also having the traditional simplicity of all Spring Data projects.

2. Maven Dependencies

Let's start by declaring the Spring Data Redis dependencies in the pom.xml:



The latest versions of spring-data-redis and jedis can be downloaded from Maven Central.

3. The Redis Configuration

To define the connection settings between the application client and the Redis server instance, we need to use a Redis client.

There is a number of Redis client implementations available for Java. In this tutorial, we'll use Jedis – a simple and powerful Redis client implementation.

There is good support for both XML and Java configuration in the framework; for this tutorial, we'll use Java-based configuration.

3.1. Java Configuration

Let's start with the configuration bean definitions:

JedisConnectionFactory jedisConnectionFactory() {
    return new JedisConnectionFactory();

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

The configuration is quite simple. First, using the Jedis client, we're defining a connectionFactory.

Then, we defined a RedisTemplate using the jedisConnectionFactory. This can be used for querying data with a custom repository.

3.2. Custom Connection Properties

You may have already noticed that the usual connection-related properties are missing in the above configuration. For example, the server address and port are missing in the configuration.The reason is simple: for our example, we're using the defaults.

However, if we need to configure the connection details, we can always modify the jedisConnectionFactory configuration as follows:

JedisConnectionFactory jedisConnectionFactory() {
    JedisConnectionFactory jedisConFactory
      = new JedisConnectionFactory();
    return jedisConFactory;

4. Redis Repository

Let's use a Student entity for our examples:

public class Student implements Serializable {
    public enum Gender { 
        MALE, FEMALE

    private String id;
    private String name;
    private Gender gender;
    private int grade;
    // ...

4.1. The Spring Data Repository

Let’s now create the StudentRepository as follows:

public interface StudentRepository extends CrudRepository<Student, String> {}

5. Data Access Using StudentRepository

By extending CrudRepository in StudentRepository, we automatically get a complete set of persistence methods that perform CRUD functionality.

5.1. Saving a New Student Object

Let's save a new student object in the data store:

Student student = new Student(
  "Eng2015001", "John Doe", Student.Gender.MALE, 1);;

5.2. Retrieving an Existing Student Object

We can verify the correct insertion of the student in the previous section by fetching the student data:

Student retrievedStudent = 

5.3. Updating an Existing Student Object

Let's change the name of the student retrieved above and save it again:

retrievedStudent.setName("Richard Watson");;

Finally, we can retrieve the student's data again and verify that the name is updated in the datastore.

5.4. Deleting an Existing Student Data

We can delete the above-inserted student data:


Now we can search for the student object and verify that the result is null.

5.5. Find All Student Data

We can insert a few student objects:

Student engStudent = new Student(
  "Eng2015001", "John Doe", Student.Gender.MALE, 1);
Student medStudent = new Student(
  "Med2015001", "Gareth Houston", Student.Gender.MALE, 2);;;

We can also achieve this by inserting a collection. For that, there is a different method – saveAll() – which accepts a single Iterable object containing multiple student objects that we want to persist.

To find all inserted students, we can use the findAll() method:

List<Student> students = new ArrayList<>();

Then we can quickly check the size of the students list or verify for a greater granularity by checking the properties of each object.

6. Conclusion

In this tutorial, we went through the basics of Spring Data Redis. The source code of the examples above can be found in a GitHub project.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Newest Most Voted
Inline Feedbacks
View all comments
3 years ago

Can anyone help with an example for checking a key exists in RedisTemplate?

I am using redisTemplate.keys(“key”); to check whethre my key exists. Redis guys are telling not to use keys *

Eugen Paraschiv
3 years ago
Reply to  prashanth-g

So, skipping over the broader question of “should you use keys” – the RedisKeyCommands has a simple exists API you can use. Hope that helps.

Pramod Bhargava
Pramod Bhargava
3 years ago

There is a annotaion @EnableRedisRepository what it do?
how can i configure this in my configuration xml?

Grzegorz Piwowarek
Grzegorz Piwowarek
3 years ago

This is a Spring Data annotation that pretty much connects all the wires between Redis and Java code. Is there any reason why you need to stick to XML and not to Java config?

Pramod Bhargava
Pramod Bhargava
3 years ago

My application is still using .xml configurations .so we dont have @configuration which comes with spring boot

Eugen Paraschiv
3 years ago

You’ll need to use some Java configuration to be able to use the annotation. However, keep in mind that moving to Java config is not an all or nothing choice. You can gradually make the move and still keep most of your XML configuration intact, and then slowly move that when you can.

Bartosz Jaszczak
Bartosz Jaszczak
3 years ago

I’ve got one question, is there a proper way to test Redis repositories/connections/entities? I’m using Spring Data Redis, and trying to write some tests for it. I’ve found projects like com.github.kstyrc.embedded-redis, but i dont like idea of alpha version of library with last commit 10 months ago in production dependencies. Should i use real/test redis instance? Is there any spring supported way to use embedded one? (I’ve moved my question from topic about client)

Eugen Paraschiv
3 years ago

I’ve never tried to run an embedded instance, so I’m not sure how stable/mature they are. I’ve always used an actual instance, but I’d be curious to know if there’s good embedded support as well.
Not much help on this one.

Comments are closed on this article!