Redis using Spring Data – Part 1/3

Before we start
In this blog, you will learn how to install a NoSQL database Redis. You will also learn about starting redis-server, using redis-cli.

In the upcoming series of this article, we will create a demo java application using spring-data-redis exploring various data structures supported by the redis and also go through publish subscribe messaging with Redis PubSub. I will also show you about the replication setup for Redis server.

Installing Redis
The official stable version of redis can be installed for unix like system only. Download, extract and compile Redis with following commands on the terminal:
$ wget
$ tar xzf redis-2.6.14.tar.gz
$ cd redis-2.6.14
$ make

Redis is now installed. Now run the redis using the command :

$ ./src/redis-server

By default, redis run on port 6379. Port can be configured in /redis.conf. Your terminal screen should have message like:

Screen Shot 2013-07-23 at 7.31.16 PM

A two-minute introduction to Redis data types using redis-cli
Any binary sequence can be used as the redis key, from string to jpeg content. Redis support following data types for value:
Redis command line interface can be started on the terminal by the command:

String type

This is the simplest data type. The simple string key/value data can be saved and retrieved as on redis-cli as :
$ set "Omanand Jha Vatsa"
$ get
"Omanand Jha Vatsa"

List type

Redis lists are ordered collection implemented using linked list. Adding an element to the head or tail of the list is performed in the constant time.The LPUSH command adds a new element into a list, on the left (at the head), while the RPUSH command adds a new element into a list, on the right (at the tail). Finally the LRANGE command extracts ranges of elements from lists:

$ rpush scores 30
(integer) 1
$ rpush scores 40
(integer) 2
$ rpush scores 50
(integer) 3
$ lrange scores 0 2
1) "30"
2) "40"
3) "50"
$ lrange scores -3 -1
1) "30"
2) "40"
3) "50"

Integer reply: the length of the list after the push operations.

Sets type

Redis Sets are a unordered collection of binary-safe strings. SADD command adds a new element to a set. SMEMBERS command gives the elements of the set. SISMEMBER command returns 1, if the element exists in the set otherwise 0.

$ sadd users "user1"
(integer) 1
$ sadd users "user2"
(integer) 2
$ sadd users "user3"
(integer) 3
$ sadd users "user1"
(integer) 0
$ smembers users
1) "user3"
2) "user1"
3) "user2"
$ sismember users "user1"
(integer) 1
$ sismember users "user4"
(integer) 0

Find the list of all the commands here and details of data types here.

We will start developing a demo Java application using spring-data-redis and maven in the second part of this tutorial.

Leave a Reply

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