Skip to content
Jeff Levesque edited this page Apr 30, 2015 · 16 revisions

##Overview

Redis is an open source, key-value cache, and store system. Often classified as NoSQL, it is more accurately referred to as, a data structure server. Though, redis is similar to Memcached (some argue faster), in general, it has more features, and greater flexibility.

###Installation on Ubuntu

Redis can be installed on an Ubuntu Machine as follows:

# Redis Client
sudo apt-get install python-pip
sudo pip install redis

# Redis Server
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:rwky/redis
sudo apt-get update
sudo apt-get install redis-server

Note: python-software-properties allows the command add-apt-repository to be executed.

###Datatypes

Though, similar to Memcached, in addition the the simple key-value store, Redis has the following datatypes:

###Data Persistence

Another great feature provided by Redis (not found in Memcached), is data persistence. By default, Redis implements snapshotting the dataset. This is defined within redis.conf:

...
save 900 1
save 300 10
save 60 10000
...

The first line implies, save the dataset into disk, as dump.rdb, after 900 seconds (15 minutes), if there is at least 1 change to the dataset. This allows dump.rdb to be loaded into memory, at each redis server start-up.

Alternatively, Redis has the capacity to store redis commands to an AOF file, or Append Only File. When the redis server boots, the AOF file reruns previous redis commands, which restores the previous dataset state.

Note: the term dataset refers to the full redis data stored in memory.

###Autostarting

Since implementing Redis entails both the redis-server, and the redis-py client, it is important to ensure the server is running when attempting to perform an operation (from the client). Luckily, the above installation technique, defines the /etc/init/redis-server.conf. Essentially, this file tells Ubuntu's Upstart, to start redis when the Ubuntu machine boots-up.

However, it is important the following line from /etc/init/redis-server.override is commented out:

#manual

Otherwise, the autostart feature will be overridden, and will require manual start. Trivially, if autostart is not preferred, simply uncomment the above line from /etc/init/redis-server.override, or comment out the contents of redis-server.conf, more specifically, the exec command:

#exec start-stop-daemon --start --chuid redis:redis --pidfile /var/run/redis/redis.pid --umask 007 --exec /usr/bin/redis-server -- /etc/redis/redis.conf

Note: the /etc/init/ directory contains configuration files used by Upstart. The contained files tells Upstart how and when to start, stop, reload the configuration, or query the status of a service.

Note: Older versions of Ubuntu loaded startup scripts from /etc/init.d/ via sysvinit. More recent versions of Ubuntu have phased our sysvinit with upstart. However, Ubuntu 15.04 will replace upstart with systemd.

###Server Commands

Whether or not the redis-server autostarts, the following are commands to start, restart, and stop the redis-server:

sudo start redis-server
sudo restart redis-server
sudo stop redis-server

###Client Commands

The redis-py client API is straightforward. An example implementation can be seen within redis_query.py. Though, the latter example, is a client interface with just a subset of all redis-py client commands.

###Connection Pools

A redis connection pool, manages a set of connection instances. By default, the maximum limit is 10,000 concurrent connections, and can be adjusted within redis.conf, in the maxmemory directive.

The following implementation defines a reusable connection pool, based on the supplied host, port, and (redis) database number (0-15):

pool        = redis.ConnectionPool(host=self.host, port=self.port, db=self.db_num)
self.server = redis.StrictRedis(connection_pool=pool)

This allows the redis client, redis.StrictRedis, to reuse a previous connection within the pool, which may be idle from pervious use. This saves a considerable load, since a new connection does not need to be made for a given query, when the client reuses a previous connection.

Note: The number of redis databases available, can be configured within redis.conf, by changing the database value. By default, it is set to 16.

Clone this wiki locally