Redis Cluster Tutorial-1
Redis Cluster Tutorial-1
Redis Cluster Tutorial-1
This document is a gentle introduction to Redis Cluster, that does not use difficult to
understand concepts of distributed systems . It provides instructions about how to setup
a cluster, test, and operate it, without going into the details that are covered in the Redis
Cluster specification but just describing how the system behaves from the point of view
of the user.
However this tutorial tries to provide information about the availability and consistency
characteristics of Redis Cluster from the point of view of the final user, stated in a
simple to understand way.
Note this tutorial requires Redis version 3.0 or higher.
If you plan to run a serious Redis Cluster deployment, the more formal specification is a
suggested reading, even if not strictly required. However it is a good idea to start from
this document, play with Redis Cluster some time, and only later read the specification.
This allows to add and remove nodes in the cluster easily. For example if I want to add
a new node D, I need to move some hash slot from nodes A, B, C to D. Similarly if I
want to remove node A from the cluster I can just move the hash slots served by A to B
and C. When the node A will be empty I can remove it from the cluster completely.
Because moving hash slots from a node to another does not require to stop operations,
adding and removing nodes, or changing the percentage of hash slots hold by nodes,
does not require any downtime.
Redis Cluster supports multiple key operations as long as all the keys involved into a
single command execution (or whole transaction, or Lua script execution) all belong to
the same hash slot. The user can force multiple keys to be part of the same hash slot by
using a concept called hash tags.
Hash tags are documented in the Redis Cluster specification, but the gist is that if there
is a substring between {} brackets in a key, only what is inside the string is hashed, so
for example this{foo}key and another{foo}key are guaranteed to be in the same hash slot,
and can be used together in a command with multiple keys as arguments.
As you can see, B does not wait for an acknowledgement from B1, B2, B3 before
replying to the client, since this would be a prohibitive latency penalty for Redis, so if
your client writes something, B acknowledges the write, but crashes before being able
to send the write to its slaves, one of the slaves (that did not receive the write) can be
promoted to master, losing the write forever.
This is very similar to what happens with most databases that are configured to flush
data to disk every second, so it is a scenario you are already able to reason about
because of past experiences with traditional database systems not involving distributed
systems. Similarly you can improve consistency by forcing the database to flush data on