Use the Redis Cluster

Tram Ho

Redis is a useful tool for developing your applications. You can use Redis to cache, manage sessions in your application quickly. Redis has a much faster write and read speed than other databases. It also builds a pub / sub message borker feature. This means you can use Redis to listen and broadcast events. For example, a notification system subscribe a channel on the redis message broker, it can receive notifications from publishers when an event is broadcast to this channel. In addition, Redis supports many different data types and methods for accessing data logically.

Redis is like other databases, Redis needs configuration to ensure its availability and operation with Redis Cluster and Redis Sentinel.

Redis Sentinel: used when speed is not your concern, it is a great choice to meet the availability of Redis.

Redis Cluster : it provides high availability with clustering solution. It is a great choice to ensure availability while still having fast query speeds on your data.

This article I will introduce the use of clusters in Redis

Understand the Redis cluster

Each Redis Cluster node requires 2 TCP connections to be opened. The normal TCP Redis port is used to serve clients, for example 6379, plus the addition gained by adding 10000 to the data port we get port 16379. Make sure you open both ports in your firewall, if No Redis cluster nodes will not be able to communicate with each other.

The Redis Cluster uses hash slots with 16384 hash slots.

Each node in the cluster will be responsible for a subset of hash slots positions, for example if we have a cluster with 3 nodes:

  • Button A contains hash slots from 0 to 5500.
  • Button B contains hash slots from 5501 to 11000.
  • Button C contains hash slots from 11001 to 16383.

Create and use Redis Cluster

The Redis cluster uses a master-slaver configuration to support a distributed environment. In this example we will create 3 masters and 3 slaves. Each master will have 1 slaver.

Install Redis

We are configuring the cluster with 6 nodes (3 masters and 3 slaves) so we will create 6 directories with names corresponding to that node’s port.

In each directory, download and create the Redis package using the following commands:

Run the Redis cluster

After installing Redis, create the redis.conf file in the src directory of each node. This file will contain the cluster’s configuration information. For example:

Meaning of the param in the configuration file:

  • port: the node’s port operates in the cluster.
  • cluster-enabled: enable redis cluster.
  • cluster-config-file: The file containing the configuration for this node is stored, by default nodes.conf. This file is automatically generated when the cluster runs.
  • cluster-node-timeout: node’s timeout period.
  • appendonly: store Redis data to disk using AOF.
  • Change the port in the redis.conf file that corresponds to each node’s port.

Finally run each node with the command ./src/redis-server ./src/redis.conf

Create the cluster

Run the following command in the src directory of node 6001

./redis-cli --cluster create 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 127.0.0.1:6006 --cluster-replicas 1

cluster-replicas 1 means we will create one slave for each master. Other values ​​are the address and port of each node.

After the cluster is configured we will get the result as shown below.

The nodes running on port 6001, 6002, 6003 are the masters and the remaining nodes are the corresponding slaves.

The nodes in the cluster are already active, we can try setting and getting data on node 6001.

We see the data has been moved to node 6003 with the corresponding hash slot.

We try to get data on the slave of 6003 which is node 6006

The data has been backed up to the slave.

Now you can run your own redis cluster 😃 .

Share the news now

Source : Viblo