Redis -a key-value based datastore

Redis is a key-value based in-memory datastore and data structured server, which also offers several other data structures to save and retrieve the data. It is an open sources project written in ANSI-C. It is having persistence capability and it support several other data structures except only key-value pair.

We are exploring redis for some master data structure in our current project. So below are some of the usage of redis commands which we are trying to incorporate in our project.

After downloading the redis zip in our linux box, we have started the redis server using –

./redis-server (Executable File)

Then we had turned the redis cli (command line interface) using

./redis-cli
Which gives us the command prompt –

127.0.0.1:6379> (Default port for redis server)

Now some of the redis commands –

Testing Redis –

Command – ping

Result – PONG (So the server is started and it is looking healthy)

Storing some value in a key –

Command – set name “First redis command”

Result – OK

Fetching the particular value from redis

Command – get name

Result – “First redis command”

We can set an expiration time value for a key in redis.

Command – expire name 120

Here the name key will have its value stored for 120 secs.

If we want to see the time left of the key to be expired, then –

Command – ttl name

It will show the no of secs left for expiring the redis key “name”

Redis provide us the feature of incrementing value and store it –

For example –

Command sets –

set incrementme 100
incr incrementme
get incrementme

will result the value of the key “incrementme” as 101

Here INCR is the command responsible for increment

Also

incrby incrementme 5

will increment the value of the key “incrementme” by 5

The reverse commands are decr and decrby. They are responsible for decrementing the value for any given key in redis, which is eligible for increment or decrement.

Redis lists are implemented by lineked list data structure.

There are 2 commands related to redis lists – RPUSH and LPUSH

LPUSH adds element in the top of a list, while RPUSH adds element in the bottom of a list

So the commands –

rpush messages “RPUSH MSG 1”
rpush messages “RPUSH MSG 2”
rpush messages “RPUSH MSG 3”
lpush messages “RPUSH MSG 4”
lpush messages “RPUSH MSG 5”
lpush messages “RPUSH MSG 6”

will result –

1) “RPUSH MSG 6”
2) “RPUSH MSG 5”
3) “RPUSH MSG 4”
4) “RPUSH MSG 1”
5) “RPUSH MSG 2”
6) “RPUSH MSG 3”

for the following command –

lrange messages 0 5

here lrange command is responsible for showing the elements. Also the top and bottom element addition in the list can be viewed with the above example.

Redis sets are equivalent to redis lists, except the element in a set can occur only once.

The command for set are –

sadd states “West Bengal”
sadd states “Rajasthan”
sadd states “Uttar Pradesh”
sadd states “Delhi”

To view the values in set, the command is –

Command – smembers states

It is to be noted that, the set are not sorted by default.

There are commands like scard to have no of elements in the set and srem to remove element from state

We can use sorted set in redis also with following commands –

zadd states 1 “West Bengal”
zadd states 2 “Rajasthan”
zadd states 3 “Uttar Pradesh”
zadd states 4 “Delhi”

To view the sorted lists, we should use –

Command – zrange states 0 -1

Another important data structure in redis is hash. With hash, we can we can assign the values of fields in particular records.

Below are the commands –

hset state1 name “West Bengal”
hset state1 country “India”

To view all of the values related to “state1” key, we should use –

hgetall state1

which will result –

1) “name”
2) “West Bengal”
3) “country”
4) “India”

Now to view all of the command examples in redis, reader should refer to –

http://redis.io/commands

If you find this article helpful, you can connect us in Google+ and Twitter.

Enter your email address:

Delivered by FeedBurner

2 thoughts on “Redis -a key-value based datastore

Leave a Reply

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