Today, apart from relational tables, data is also available in other forms such as images, texts, blogs, lists, etc. Redis can be used as a database, cache and message broker. It is exceptionally fast, and can support five data types and two special types of data.
Redis is an open source, in-memory and key/value NoSQL database. Known to be an exceptionally fast in-memory database, Redis is used as a database, cache and message broker, and is written in C. It supports five data types— strings, hashes, lists, sets, sorted sets and two special types of data—Bitmap and HyperLogLog.
Since Redis runs in memory, it is very fast but is disk persistent. So in case a crash happens, data is not lost. Redis can perform about 110,000 SETs and about 81,000 GETs per second. This popular database is used by many companies such as Pinterest, Instagram, StackOverflow, Docker, etc.
There are several problems that Redis addresses, which are listed below.
1. Session cache: Redis can be used to cache user sessions. Due to its in-memory data structure as well as data persistent nature, Redis is a prefect choice for this use case.
2. Middle layer: If your program delivers data at a very fast rate but the process of storing data in a database such as MySQL is very slow, then you can use Redis as the middle layer. Your program will just put the data into Redis, and another program will pull the data and store it in the database.
3. Shows the latest items listed in your home page: Due to fast retrieval, you can show the important data to users from Redis. The rest of the data can be fetched from the database later.
4. It acts as a broker for Celery processes.
Installation of Redis
You can download a stable version of Redis from https://redis.io/download. We are going to install Redis on Centos 6.7 —you can use Ubuntu or Debian. Let us extract the tar package of Redis, for which you can use the following command:
$ tar -xvzf redis-3.2.8.tar.gz
After extraction, a directory redis-3.2.8 will be formed. Just use one command, make, to install the Redis. Sometimes, an error occurs while installing Redis:
In file included from adlist.c:34: zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory zmalloc.h:55:2: error: #error "Newer version of jemalloc required" make: *** [adlist.o] Error 1 make: Leaving directory `/root/redis/redis-3.2.8/src'
If the above error occurs, then use the command given below:
make MALLOC=libc && make install
Figure 1 shows that Redis has been successfully installed.
Just take a look at the directory redis-3.2.8 (Figure 2). You will find the redis.conf file in it. In order to start Redis, we need to configure this file.
Let us consider a main configuration of the file:
If you are using a static IP, then you can give the static IP address; otherwise, 127.0.0.1 is okay.
The default port of Redis is 6739. pidfile /var/run/redis_6379.pid
The above line gives the location of pidfile, and if you are not running as root then you can give your custom path:
By default, it is blank but you can give the log file path; try to give the absolute path.
Now, give the following commands:
logfile "/var/run/redis_6379.log" dbfilename dump.rdb
The above line gives the name of the dump file that stores the database.
The above option specifies the location of the dump.rdb file.
By default, the above option is set as ‘no’. When you run the Redis server, it shows progress on the terminal but, in a development environment, the Redis server must be run in the background. So, set it as ‘yes’.
Let us now start Redis. In Figure 2, there is a directory src, which contains Redis binaries.
The command to run the Redis server is as follows:
# src/redis-server redis.conf
See Figure 3, which shows that Redis is running.
Redis supports master-slave replication, which allows slave Redis servers to be exact copies of master servers. The master/slave replication is one way of achieving scalability. The slave is read-only and the master is read and write. When a data update happens on the master, it eventually affects the slave.
Creating a Redis slave
In order to start a slave, you will need a conf file. So make a copy of redis.conf as redis-slave.conf:
# cp redis.conf redis-slave.conf
Next, open redis-slave.conf. And change the settings as shown below:
pidfile /var/run/redis_6380.pid logfile "/var/run/redis_6379-s.log" port 6380 slaveof 127.0.0.1 6379
The above settings set the port number of the slave and tell the slave about its master.
Start the slave and follow the process, as shown in Figure 4.
The Redis master and slave are both running now.
To run commands on the Redis remote server, we will use the redis-cli command syntax:
# redis-cli –h <ip_address> -p <port number> [root@example redis-3.2.8]# src/redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> ping PONG 127.0.0.1:6379>
Let us take a look at some useful commands for a fresh start.
Set, get and del: These three commands set the key values. See the following example.
127.0.0.1:6379> set "one" 1 OK 127.0.0.1:6379> get one "1" 127.0.0.1:6379> del one (integer) 1 127.0.0.1:6379> get one (nil) 127.0.0.1:6379>
In the above example, ‘one’ is the key and 1 is the value.
Lpush, lrange, rpush: These are three more useful commands.
The lpush command pushes the values from the left side.
127.0.0.1:6379> lpush list1 "one" "two" (integer) 2 127.0.0.1:6379> lpush list1 "three" (integer) 3
In order to see the content, we will use lrange.
The syntax is # lrange key start end.
127.0.0.1:6379> lrange list1 0 5 1) "three" 2) "two" 3) "one" 127.0.0.1:6379>
The rpush command pushes the values from the right side.
127.0.0.1:6379> rpush list2 "one" "two" (integer) 2 127.0.0.1:6379> rpush list2 "three" (integer) 3 127.0.0.1:6379> lrange list2 0 5 1) "one" 2) "two" 3) "three" 127.0.0.1:6379>
Let us consider two more commands, lpop and rpop.
127.0.0.1:6379> lrange list2 0 5 1) "one" 2) "two" 3) "three" 127.0.0.1:6379> rpop list2 "three" 127.0.0.1:6379> lpop list2 "one" 127.0.0.1:6379> lrange list2 0 5 1) "two" 127.0.0.1:6379>
You can see that rpop pops the value from the right and lpop pops the value from the left.
There are many other useful commands that you will find in the documentation on Redis.
Using Redis with Python
In order to use Redis with Python, you will need redis-py. You can download redis-py from https://pypi.python.org/pypi/redis. I am using redis-2.10.5.tar.gz.
Extract the tar package, as follows:
# tar -xvzf redis-2.10.5.tar.gz
Use the following command to install it:
python setup.py install
Let us write a Python script to perform Redis operations as we did earlier with redis-cli.
I am going to write a simple Python script named redis1.py:
import redis r = redis.StrictRedis(host='localhost', port=6379, db=0) r.set("one",1) r.set("two",2) r.set("three",3) print r.get("one") print r.get("two") print r.get("three")
See the output in Figure 5.
Let us create another program redis3.py, which pops the value from the left as well as from the right.
import redis r = redis.StrictRedis(host='localhost', port=6379, db=0) r.rpush("list1","one") r.rpush("list1","two", "three", "four") print r.lrange("list1",0,-1) print "pop from right ", r.rpop('list1') print "pop from left ",r.lpop('list1') print r.lrange("list1",0,-1)
The above program is very easy to understand. The results we have achieved in the above program are the same as what we achieved in redis-cli mode. See the output in Figure 6.
While there are a lot of advantages of using Redis, there are some disadvantages too. You should have more memory than your data requires, because Redis forks on each snapshot dump, which can consume extra memory. Also, you can’t roll back a transaction.
In programming, you must always take care of the number of connections. Sometimes people are lazy while programming, leading to the ‘max client reached problem’.
Alternatives to Redis
Well, this totally depends upon the situation. If you just need fast retrieval, you can use Memcache, but this does not store data on the disk. NoSQL databases like MongoDB and Couchbase might be alternatives for some use cases.
You can compare the Redis alternatives at:
It is recommended that you also read up on Redis commands and Redis Sentinel connections.