Pages

Tuesday, June 4, 2019

Redis - What and Why

Redis is a essentially a key-value data store. This is most commonly used for the purpose of caching. Caching is a process of storing some data in Cache. Cache is a temporary storage area where the data is stored that can be served back fast.

For example, At Pinterest redis is used to store information such as users who follow whom etc. Redis developed in 2009, is a flexible, open source key-value data store. Following in the footsteps of other NoSQL databases, such as Cassandra, CouchDB, and MongoDB, Redis allows the user to store vast amounts of data without the limits of a relational database.

In this article, we will see how to configure redis server and understand the basic usage of that.

Installation - Installing redis is quite easy. Download the package from here. 
1. wget http://download.redis.io/releases/redis-stable.tar.gz
2. tar xvf   xzf redis-stable.tar.gz
3. cd redis-stable
4. yum install gnutls-c++.x86_64 gnutls-utils.x86_64 gcc.x86_64
5. cd deps
6. make hiredis jemalloc linenoise lua geohash-int
7. cd ..
8. make
9. make install

Once the installation is done, we can see a utils directory in the same location. Go to utils and run the “install_server.sh” script to provide basic configuration details. This is how we configured the server as below,
[root@ip-172-31-23-59 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/bin/redis-server
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

We can keep the defaults for most of the options above. Now once the server is up and running, run the redis-cli tool to connect to the redis server as below,
[root@ip-172-31-23-59 bin]# /usr/local/bin/redis-cli
127.0.0.1:6379> exit

Run a ping command by connecting to the redis server to see the pong response as below,
[root@ip-172-31-23-59 bin]# /usr/local/bin/redis-cli
127.0.0.1:6379> ping
PONG

Configuration : In redis, there is a configuration file redis.conf available in the root directory where all configuration details are available. We can get and set the configuration using CONFIG command as below,
[root@ip-172-31-23-59 bin]# /usr/local/bin/redis-cli
127.0.0.1:6379> CONFIG SET loglevel "notice"
OK
127.0.0.1:6379> CONFIG GET loglevel
1) "loglevel"
2) "notice"

Authentication : While accessing redis server, it is very important to set authentication to the redis server. A Password can be set using,
[root@ip-172-31-23-59 bin]# /usr/local/bin/redis-cli
127.0.0.1:6379> config set requirepass redhat
OK
127.0.0.1:6379> exit

To login using the password redhat, use
127.0.0.1:6379> AUTH redhat
OK

Redis Data Types - As we already discussed, we will be storing a lot of data in the redis server. In order to store different types of data we need data types. Redis supports multiple data types.

String - String is the basic data type in redis which is a sequence of bytes. Some common commands associated with strings are:
SET: sets a value to a key
GET: gets a value from a key
DEL: deletes a key and its value
INCR: atomically increments a key
INCRBY: increments a key by a designated values
EXPIRE: the length of time that a key should exist (denoted in seconds)

String values can be set and get as below,
[root@ip-172-31-23-59 bin]# /usr/local/bin/redis-cli
127.0.0.1:6379> SET name "jagadish"
OK
127.0.0.1:6379> GET name
"jagadish"
127.0.0.1:6379> exit

List: List is a collection of ordered values. Here are few commands to the list in redis,
LPUSH: Add a value to the begining of a list
RPUSH: Add a value to the end of a list
LPOP: Get and remove the first element in a list
RPOP: Get and remove the last element in a list
LREM: Remove elements from a list
LRANGE: Get a range of elements from a list
LTRIM: Modifies a list so leave only a specified range

A List can be used as below,
[root@ip-172-31-23-59 bin]# /usr/local/bin/redis-cli
127.0.0.1:6379> lpush mylist jagadish
(integer) 1
127.0.0.1:6379> lpush mylist manachala
(integer) 2
127.0.0.1:6379> lpush mylist 37
(integer) 3
127.0.0.1:6379> lpush mylist hyderabad
(integer) 4
127.0.0.1:6379> lrange mylist 0 10
1) "hyderabad"
2) "37"
3) "manachala"
4) "jagadish"
127.0.0.1:6379> exit

Sets : Set contain a collection of unordered strings. If you want to combine string we can do that using the Redis sets. Some of the set command include,
Some common commands for Sets are:
SADD: Add one or members to a set
SMEMBERS: Get all set members
SINTER: Find the intersection of multiple sets
SISMEMBER: check if a value is in a set
SRANDMEMBER: Get a random set member

A set can be implemented as below,
[root@ip-172-31-23-59 bin]# /usr/local/bin/redis-cli
127.0.0.1:6379> SADD colors orange
(integer) 0
127.0.0.1:6379> SADD colors blue
(integer) 1
127.0.0.1:6379> SADD colors black
(integer) 1
127.0.0.1:6379> SMEMBERS colors
1) "orange"
2) "black"
3) "blue"
4) "red"

Hashes : Hashes in Redis are a useful tool to represent objects with many fields. They are set up to store vast amount of fields in a small amount of space. A hash can store more than 4 billion field-value pairs.
[root@ip-172-31-23-59 bin]# /usr/local/bin/redis-cli
127.0.0.1:6379> hmset user:1 username jagadish password 342swd age 37
OK
127.0.0.1:6379> hgetall user:1
1) "username"
2) "jagadish"
3) "password"
4) "342swd"
5) "age"
6) "37"
127.0.0.1:6379> exit

Connecting Python With Redis server : A simple code for connecting to the Redis server using python works as below,
[root@ip-172-31-23-59 ~]# cat sample-Connet.py
import redis

r = redis.Redis(
   host='localhost',
   port=6379,
   password='redhat')

value=r.get("name")
print value

If we run the above code, it connects to the redis server and get the value associated with the key name as below,
[root@ip-172-31-23-59 ~]# python sample-Connet.py
jagadish

Hope we got a basic understanding of what redis server is and its components.

No comments :

Post a Comment