Skip to content
evanx edited this page Jan 1, 2015 · 1 revision

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 have a "score" which is a floating point number. Members are ranked by this score, and/or the member (string) in the case of equal scores. They can be fetched by rank, or a range of their scores. All scores might be set to 0 to rank via the member string lexographically.

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 integer ids, or serialized objects e.g. JSON.

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

The whole "database" is stored in memory. This deliberate "feature" gives it fantastic performance compared to on-disk databases. But it's also its "bug" that limits the database size.

Considering this limitation, trimming the data set is critical - i.e. removing older data we don't actually need in memory for our use-case. (This might be archived into a disk-based database.)

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 various use-cases, besides a database per se:

  • memory cache in place of 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 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 extra management and integration effort is required when using RDBMS and SQL, compared to Redis, for such use-cases.

The killer feature is that Redis might be 100x or 500x 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, then clearly an RDBMS is an appriopriate technology.

However, a mix of SQL/NoSQL technologies is increasing. 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 relation store, and/or NoSQL document store (e.g. MongoDB, RethinkDB, ElasticSearch).

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. Then just use the redis-cli command-line. See http://redis.io.

Clone this wiki locally