The Redis Way

You may heard about NoSQL database systems before, the most popular one in MongoDB, so, what are the alternatives? and how the Mongo is not suited for our work sometimes?

The answer is , you may need to develop some fast serving db systems for your application sometimes, as an example , to implement a blog application you not need to serve your database very frequently, but when you are developing online chat application you may meed to call your database very often. For such kind of situations we can use Redis NoSQL as our alternative. Redis id key value pair store db system which use your RAM to store data structures and serve them in efficient manner.

Lets see how to install and configure redis in your machine and how to get the work done from Redis.

First install Redis on your linux/Mac using source http://redis.io/download

Once you download the tarball extract it in your /tmp directory and go to that directory, the execute

make 

After the make process is completed , using your admin privileges execute

sudo make install

Then start the server

redis-server

Screenshot from 2014-02-21 10:45:24

Then you are ready to use your Redis db system but what about configurations

copy redis.conf to the /etc/redis/

sudo cp redis.conf /etc/redis/

Then start the redis rerver

redis-server /etc/redis/redis.conf

If you find issues, just comment the lines on redis.conf that are given errors.

Use of Redis Command Line Interface

redis-cli

Set a simple key value pair in redis store.

redis 127.0.0.1:6379> SET simple-item "Hi Redis"
OK

You can find more data structures from http://redis.io/commands

Show all keys which are set in redis

redis 127.0.0.1:6379> KEYS *
 1) "Dum-7-channels"
 2) "simple-item"
 3) "auth:dev-Raj-7"
 4) "Y-7-msgs"

Get values set on a specific key

redis 127.0.0.1:6379> GET simple-item
"Hi Redis

Use of RPUSH/LPUSH (List like data structure)

RPUSH (Reverse push)

redis 127.0.0.1:6379> RPUSH simple-list1 "list data 1"
(integer) 2
redis 127.0.0.1:6379> RPUSH simple-list1 "list data 2"
(integer) 2

LPUSH

redis 127.0.0.1:6379> LPUSH simple-list2 "list data 1"
(integer) 1
redis 127.0.0.1:6379> LPUSH simple-list2 "list data 2"
(integer) 2

Retrieve list data

Get data stored in list1

redis 127.0.0.1:6379> LRANGE simple-list1 0 2
1) "list data 1"
2) "list data 2"

Get data stored in list2

redis 127.0.0.1:6379> LRANGE simple-list2 0 2
1) "list data 2"
2) "list data 1"