NoSQL is all the rage for Web developers who are tired of building complex SQL. Redis, a NoSQL database server much like Cassandra or MongoDB, provides a powerful and speedy alternative to relational database servers for PHP developers. Redis is similar to Memcached in terms of speed, but the data isn’t volatile. That means that when your server reboots, your Redis data will still be there. You can back it up too.
A raging debate in the Web development community is whether NoSQL databases like Redis will replace traditional relational databases. But if you’ve been a programmer for more than a few years, you know that rarely does a technology come along and completely wipe another off the technology landscape. What’s likely to happen is that NoSQL databases will work alongside traditional relational database servers.
Key-value NoSQL databases are very appealing though, particularly because of their speed, flexibility and scalability. Because Redis is a key-value database not a relational one, you don’t have to spend an inordinate amount of time building a SQL schema and worrying about relational stuff such as foreign keys and primary keys. Of course, you still need to design your database, but you don’t have to think about whether to use second normal form or third normal form.
Installing Redis is really easy. If you’re running Debian, you can install the
redis-serverpackage. It’s in the testing aptitude source. Installing from the current source is really a no-brainer. You just have to download and extract it, run
make, and then type
./redis-server. That’s it. You can customize the server by editing the
redis.confand starting the server with
What makes Redis different than other key-value databases is that every value has a type. Redis supports strings, lists, sets and sorted sets. Each type has a unique set of commands that you can call to access and manipulate that type.
Strings are simply a single key with one value. For example, the key
yourfirstnamecould have the value
Boomer. Redis strings are binary safe. You can store images, serialized objects or whatever you want, as long as the data is less than 1GB in length. The single elements of lists, sets and sorted sets are strings.
A Redis list is like an array in PHP. You just push elements onto the head or onto the tail, depending on how you want the elements ordered in the list’s stack. A list is indexed starting with 0 then 1, 2, 3 and so on — an array. The command
LPUSHinserts the element on the head, and
RPUSHon the tail. To determine the length of the list, you call the
You can think of a Redis set as an associative array (or in Perl-speak, a hash). Redis sets are unordered hashes and can contain more than 4 billion elements. The current version of Redis resizes the set synchronously whenever elements are added or removed. This is a blocking operation, so you should be careful when performing mass importing or deleting of set elements.
Redis sorted sets are similar to sets, but every element can have an associated score that is used to set the ordering of the elements in the hash. As with sets, take care if you perform a mass import or deletion (millions of records), because resizing the hash is a blocking operation.
There are several PHP client interfaces available for Redis. You should check out Rediska, but for this tutorial you’ll use a modified version of the Redis client that comes with the Retwis application. Retwis is a Twitter-like application written in PHP to demonstrate how to build applications with Redis. For this tutorial, you should use the
redis.phpversion because the one included with Retwis doesn’t support sorted sets for some reason.
Getting to Know Redis
Download the PHP source code for this tutorial and extract it in a Web server document directory. In the
redis-tutorialdirectory you will find two files. One is the PHP client script called
redis.php, which you’ll use to interact with the Redis server. The other file is called
index.php, which performs basic Redis operations to demonstrate how easy it is to work with Redis.
The application you’re building for the purpose of this tutorial is a favorite movie Web application. It’s a really simple application, but it serves the purpose of showing how to interact with Redis using PHP.
Let’s start writing some code. Connecting to Redis is a snap.
$r = new Redis();
That’s all there is to it — it doesn’t get much easier than that.