This site is now 100% read-only, and retired.

A brief introduction to the redis data-store

Posted by Steve on Thu 17 Jan 2013 at 14:36


Redis is one of the new breed of NO-SQL storage systems, which was directly inspired by memcached. Redis allows you to store and retrieve data at blazing speed and unlike memcached it has built-in support for many primitives such as sets, lists, and hashes. Read on to learn more.

As a refresher memcached is an in-RAM database which allows you to store keys and values. As the data is stored in-RAM only any data is lost when the server-process is restarted. These days memcached is used mostly for caching purposes - caching database queries, and similar things. We looked at it for speeding up websites a few years ago.

Redis is exactly like memcached in that you can store "keys" and their "values", but it differs in two regards:

  • The contents of a Redis server persist, if you restart the service your data is still present.
  • As well as simply storing "key=value" data you can also use native data-types such as hashes, lists and sets.

Together these differences make Redis a much more generally-useful storage system. Providing all your data fits inside your servers' RAM you'll love Redis.

Installing redis should be as simple as:

# aptitude update
# aptitude install redis-server

Once installed the daemon will start, listening on the loopback adapter only. If you wish to access the server from a remote location you'll need to update the file /etc/redis/redis.conf:

# Accept connections on the specified port, default is 6379
port 6379

# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for connections.

Other than that you should now be ready to proceed. (If you do wish to make the server available across a network you should firewall it such that malicious hosts cannot connect to your redis-server, and delete the data!)

Along with the server you'll find there is a command-line tool, redis-cli for setting, querying, and generally using the server. The redis-cli tool may be used either interactively, or as a one-off tool. (You might find that using keys/values with spaces is hard in the interactive mode, because the tool doesn't support quoting arguments as you'd expect).

Here is the most basic usage, setting the value of the key "steve" to be "kemp":

kvm:~# redis-cli set steve kemp
kvm:~# redis-cli get steve

Using the interactive tool is similar:

kvm:~# redis-cli
redis> get steve
redis> set steve kemp
redis> get steve
redis> exit

In both cases we've done the same thing:

  • Set the string value "kemp" to be the value of the key "steve".

This usage is identical to that you'd expect with memcached, but other than the persistance of the stored data, redis also differs in that it has support for more "types" than just simple strings with hashes, lists, and sets each supported natively.

To give you a flavour of how these work we're going to create a list of fruits:

redis> lpush fruit Apples
redis> lpush fruit Pears
redis> lpush fruit Oranges
redis> llen fruit
(integer) 3

Here we've used the "lpush" primitive to push the specified fruit at the head of the list called fruit. Once we've done that several times we've then used the llen primitive to show the length of the list.

Now we've got the list we can retrieve items using the lrange primitive, or remove items from the head or the tail of the list:

redis> lrange fruit 0 -1
1. Oranges
2. Pears
3. Apples

The range primitive uses the starting offset and ending offset and returns those in between. We've used -1 to indicate the end of the list, but we could retrive the second entry with this:

kvm:~# redis-cli  lrange fruit 1 1
1. Pears

Since the list starts at 0 we've got the second entry here. The first entry would be "lrange fruit 0 1".

To round-up the list discussion we should also mention it is possible to retrieve idems by their position in the list, using lindex primitive, and we can remove items from the head of the list with "lpop", or the tail with "rpop".

Language Libraries

Working with the server via the redis-cli tool is all very well for testing purposes, but in the real world you'll want to use it from your code. Happily there are redis-bindings for most of the common languages and environments out there.

If you wished to use Redis from a Perl environment, for example, you'd install the libredis-perl package - then use it from your code like this:

#!/usr/bin/perl -w
use strict;
use warnings;

use Redis;

my $r = Redis->new();
$r->set( "steve", "kemp" );

print "We got the value we expected : " . $r->get("steve") . "\n";

Learning More

Although redis supports more data-types we've only demonstrated the list-functionality with lpush, lrange, llen, etc. To learn about the other primitives you should examine the command documentation.

It should be noted that Redis also supports replication such that you can keep a "hot spare" on standby in the event of an outage of the primary server you can switch the backup.

In more recent versions of Redis you also have the power of Lua at your finger-tips, and the embedded Lua intepretter may be used to run scripting commands, allowing you even more flexibility with regard to data-types and server-side work.