Popular searches
//

Spring Data – Part 6: Redis

26.4.2012 | 4 minutes reading time

Redis

Redis [1] is a NoSQL [2] key/value datastore. Think of it as a big, very fast persistent hashmap. Redis offers a master/slave data replication [3] and also a built-in publish/subscribe messaging system [4].

It is implemented in C and can be built on your favourite platform. Just grab the tar from the download page [5] or use the precompiled binaries for Windows [6] (that’s what I did).

After compiling or downloading, open a shell in your Redis folder an start the server by typing:

Open another shell and start the Redis client by typing:

We ping the server and set and lookup a string based key/value pair.

Publish/Subscribe

Now let’s have a look at the publish/subscribe messaging. Type:

The current client is now subscribed to a channel c1 and is listening to incoming messages. Start yet another shell and send a message to channel c1:

Doing this, the first client receives our message:

There are much more commands than this. Try them by yourself.

Spring Data Redis

Spring Data Redis does not offer a repository abstraction like other Spring Data proejcts (e.g. Spring Data Mongo DB or Spring Data JPA ). Instead it relies on the well-known template approach often used in Spring.

Let’s start with a very simple example. We will store key/value pairs of type String/String. Our Spring configuration looks basically like this:

We are using the Jedis connection factory to connect to our Redis node via TCP/IP and define the RedisTemplate we are using in our StringStringRepository. We introduce the repository in order to encapsulate the template itself, so that our developers do not need to know anything about the persistence layer:

The usage of that template looks like this:

Object storage

In a real world application, your business objects often are more complex than a simple string. Since the key/value pairs in Redis consist of byte arrays (both key and value), you can store anything you want. Spring Data Redis supports this by using an appropiate serialization mechanism.

Let’s assume we want to persist simple user object like this:

The serializer itself is a property of the RedisTemplate:

We use a JSON serializer and provide the class to serialize (redis.User). An appropriate repository may look like this:

The full source code of all the examples can be found at github .

Summary

We had a look at the basic features like writing and reading a key/value pair with Redis. I also showed you the publish/subscribe feature of Redis.

Then we persisted the same data with help of the Spring Data Redis API.

This is the last part of this blog series on the Spring Data project. I hope some of the articles were useful or at last a little bit interesting.

Spring Data Project

These are my other posts covering the Spring Data project:

Part 5: Spring Data Neo4j
Part 4: Geospatial Queries with Spring Data Mongo DB
Part 3: Spring Data Mongo DB
Part 2: Spring Data JPA
Part 1: Spring Data Commons

References

[1] Redis Homepage
[2] NoSQL databases
[3] Redis Replication
[4] Redis Messaging
[5] Redis Download
[6] Win32/64 Precompiled binaries

//

More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.