Skip to content
Evan edited this page Feb 21, 2015 · 43 revisions

What is Redis?

Redis is a persistent in-memory key-value store. It was developed by Salvatore Sanfilippo. (Thank you so much!)

The values of keys can be strings, lists, sets, sorted sets or hash tables.

  • Hash tables can store structured data, i.e. different fields of a record (or columns of a row).

  • Lists are queues with left and right sides (or head and tail), from which members can be pushed and popped

  • Sets have unique members, i.e. no duplicates.

  • Sorted sets' members can be given a numeric "score." Members are ranked by this score, and/or the member string in the case of equal scores.

Keys, and "members" of the above data types, are strings. However strings are "binary-safe" and so can be any type of data. Often strings (as keys, values and members) are stringified ids e.g. "user:1234", or serialized objects e.g. JSON.

Incidently, there is an operation to treat a string as an integer to be incremented - e.g. for counting events, or generating sequential ids.

The whole "database" is stored in memory. This gives it fantastic performance compared to on-disk databases. However, this feature limits the database size. Therefore trimming the data set is required, i.e. removing older data we don't need in memory for our use-case, perhaps even archiving it to disk.

Besides snapshots, Redis has an "append-only log" which is configurable to sync to disk every second, or after every write (slow). This enables one to trade off the level of performance vs durability, e.g. potentially losing the last second of writes in the event of a power failure or crash.

Actually Redis has some use-cases other than as a database:

  • memory cache ala memcached: Redis supports expiry times on keys for this purpose.

  • message queue: Redis' list data type can be used as a queue, since it offers blocking pop operations. Redis also supports pubsub i.e. for asynchronous decoupled message passing.

An important use-case especially for microservices and distributed systems in general, is using Redis for "persistent shared memory."

  • "persistent" because the data is still available when our application is restarted, or indeed Redis is restarted e.g. after a reboot or power failure.

  • "shared" because it's a server which can be accessed remotely over TCP/IP by multiple application instances.

Redis is typically used to share data required by microservices running in different application containers, which is like different hosts, with different filesystems.

For web applications, user session state can be stored in Redis, so that it is accessible by various microservices, eg. via the session ID contained in the HTTP request. (Authentication and authorisation could happen before the request reaches this service.)

If not using Redis, one might use a RDBMS for persistent session state and queues. However Redis requires less integration and management effort than an RDBMS.

Besides its convenience, Redis' killer feature is its performance, which is orders of magnitude faster than an RDBMS, considering the latency of hard disk access compared to RAM.

In general, web developers are increasingly finding NoSQL technologies attractive, in terms of ease of integration, performance and/or scalability.

Nevertheless if one's data is relational, with foreign key relationships that must exist, and strict ACID compliance is required e.g. for financial transactions, then clearly an RDBMS is an appriopriate technology.

A combination of SQL/NoSQL technologies is potentially advantageous. Redis is good for prototyping, shared memory, messaging, caching and maximum performance. As such, it might be used orthogonally and/or complementary to your SQL relational store, and/or NoSQL document store. For example, Redis might be used to cache dimensional aggregates of relational data for analytical purposes.

For comparative information on various NoSQL databases, see http://kkovacs.eu/cassandra-vs-mongodb-vs-couchdb-vs-redis.

If you want to play with Redis, it's easy to install and start without any configuration required, e.g. apt-get install redis-server and service redis-server start on Ubuntu, or yum install redis and service redis start on CentOS. Then just use the redis-cli command-line.

Visit the Redis homepage at http://redis.io.

Clone this wiki locally