← Main page

Redis

What Redis is

Redis is an in-memory database. Usually it is used as a secondary database.

Use cases

  1. Cache.
  2. Queue.
  3. Message broker.
  4. Rate-limiter.
  5. Atomic counters.

Protocol

Redis uses a simple text protocol for issuing and receiving commands.

Implementation

Redis uses a single thread to execute commands. It is a design choice.

Transactions

It is NOT the same transaction as in a relational database. If one command in a transaction fails all the other transactions WILL be executed (there is no rollback).

Using "Multi, commands list, Exec" Redis guarantees that these commands will be run one after another and all other commands will be run either before or after the commands.

Multi starts queuing consequent commands. Exec runs their execution.

WATCH in combination with EXEC is used for optimistic locking. If one of watched keys was modified EXEC will fail.

Transactions

Message broker

We can send a message to a channel. One channel has several subscribers. Commands are: subscribe, unsubscribe and publish.

Special data types

Bitmap for bit operations to save memory. HyperLogLog for probabalistic counting of unique elements in a set to save memory.

Storage

Redis database snapshots (RDB): Stores data in-memory and saves the memory snapshot to disk from time to time as an RDB file. It uses forked process to create a snapshot, so it does not affect performace of the original process. Fork uses copy-on-write, so it is fast.

Append-only file (AOF): Writes all commands issued against the Redis server. The file will be big because one value stored is only once but may be modified many times (there is a command to rewrite the file when it becomes too big). Durability depends how often you call fsync.

Restoring from RDB snapshot is faster than applying AOF.

Append-only file

Redis persistence demystified

Roundtrip time (RTT) and pipelinening

When a command is sent the client is blocked until we get a response from the redis server. If we want to avoid the roundtrip time we need to send all commands at a time or use batches. This feature is called pipelining.

Availability and failover

Redis uses replicas to support high availability. We need one primary node, one replica and at least 3 Sentinel instances to have a quorum. Sentinel instances may be run on the same machine as Redis. For big installations we may want to have 2 big Redis machines and 3 small Sentinel machines. Redis uses asynchronous replication: if master fails then replica may be slightly outdated.

Redis uses shards to scale horizontally. Cluster is a collection of shards.

Operating system support

  1. Linux is recommended.
  2. Mac OS.