Exploring NoSQL: Redis

Redis differs so dramatically from traditional databases that you’d be forgiven for not recognizing the family resemblance. Indeed, it’s possible to run Redis as a key-value memory cache, mimicking the functionality of Memcached, which itself is not a database. Like Memcached, Redis neither indexes nor searches upon values. And like Memcached, Redis may store anything as a value—a string, an integer, a serialized object, a video.

So what makes Redis a databases? It supports persistence. Actually, Redis supports two modes of persistence. One mode is snapshotting. At regular time intervals, Redis takes a snapshot of the data in its memory and stores it in an RDB file, a compact, point-in-time representation of the data. The other mode, AOF (append-only file), persists changes to a file either per command or at a regular interval. According to the Redis documentation, persisting per command is slow, but reliable. Redis recommends persisting once per second to provide good performance and reasonable reliability (at most, one second worth of data can be lost). It is possible to turn on one, both, or neither of these persistence options. Using both together would most closely match the persistence of a relational database.

Moreover, Redis does support transactions. Redis executes each command within its own transaction. The increment command, which both retrieves and adds to a value, is guaranteed to operate atomically. No two clients may retrieve and increment a value in such a way that one increment overwrites the other. And Redis supports multi-command transactions. A programmer need only issue a multi command, the set of commands required in the transaction, and a then an exec command, which triggers the running of the commands within a single transaction.

Redis also supports replication from a master to one or more slaves. However, unlike MongoDB, Redis does not support the notion of a replication set. If a master fails, the system will not fail over without intervention. The Redis team may include automatic failover in Redis Cluster scheduled for release by the end of 2012. (See http://redis.io/download.)

Unlike MongoDB and Memcached, Redis does not support scalability in an automated fashion. In general, scaling a key-value data store is accomplished through distributing objects across multiple servers based on keys. Memcached automates this functionality through hashing algorithms implemented in client drivers. Redis provides no such automatic key distribution. Application programmers need to take on this work themselves. Again, Redis Cluster may include this capability.

While Redis, like Memcached, neither indexes nor queries on values, it is not quite accurate to say Redis understands nothing about the values stored in its memory. It cannot parse JSON documents in the manner of MongoDB, but Redis provides several data types of its own, all highly useful and familiar to any student of computer science. Indeed, the documentation describes Redis as a “data structures server,” obviously not a phrase dreamed up by a marketing executive.

Each data type includes a set of commands traditionally associated with the data structure. Here is a complete list of the types and a partial list of the commands supported by each. Note that each command takes a key as its first argument.

  • The String data type stores binary data.
    • SET         Associates a value with a key
    • GET        Retrieves value based on a key
    • DEL         Removes a value based on a key
    • INCR      Increments a numeric value
    • DECR     Decrements a numeric value
  • The List data type stores a sequence of ordered elements.
    • RPUSH  Adds value to end of list
    • LPUSH   Adds value at beginning of list
    • LRANGE               Retrieves values from beginning to end of specified range
    • LLEN      Length of list
    • RPOP     Return and remove item from end of list
    • LPOP     Return and remove item from beginning of list
  • The Set data type stores a set of unordered, non-duplicate elements.
    • SADD   Add element to set
    • SREM   Remove element from set
    • SUNION            Combine two sets
    • SISMEMBER    Check whether item is a member of the set
    • SMEMBERS     Return all members of set
  • The Sorted Set stores a set that is sorted by an associated score.
    • ZADD     Adds element to a set with a score
    • ZRANGE               Returns elements from set from beginning to end of specified range
    • SRANGEBYSCORE             Returns elements from set based on score
  • The Hash data type maps string fields and string values.
    • HSET      Set a value based on a key
    • HGET     Retrieve a value based on a key
    • HDEL      Delete a value based on a key
    • HKEYS   Retrieve all keys
    • HVALS   Retrieve all values

The easiest way to get a sense of the API is to try out the online tutorial at http://try.redis-db.com. Redis, which is free and open source, has really clear documentation at http://redis.io/documentation. And getting Redis up and running on Ubuntu Linux took me just a few minutes (http://redis.io/download).

Related Posts:
Exploring NoSQL: MongoDB
Exploring NoSQL: Memcached
Exploring NoSQL: Riak
Exploring NoSQL: Couchbase

I'm the Director of Threat Solutions at Shape Security, a top 50 startup defending the world's leading websites and mobile apps against malicious automation. Request our 2017 Credential Spill Report at ShapeSecurity.com to get the big picture of the threats we all face. See my LinkedIn profile at http://www.linkedin.com/in/jamesdowney and follow me on Twitter at http://twitter.com/james_downey.

Posted in NoSQL, Redis

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: