Let’s continue playing with Redis cluster (part 1)

Redis cluster operation practice

Distribution of data


Data distribution

From the above operation, you can see that when storing data, a slot is allocated, and this slot belongs to a certain Master. This means that you need to understand that the data is distributed among the Redis cluster.

Online horizontal capacity expansion

An important feature of Redis Cluster is that nodes can be added online to achieve horizontal capacity expansion without affecting services. Here, I will add two more Redis nodes to the original six. Conf and start the two redis instances 8007 and 8008.

Modify the redis.conf configuration


Start the two newly added redis instances


State of the cluster

In fact, the two new Instances of Redis are currently started, but they are not part of the cluster. Now, let’s add them to the cluster.

Add – node command

. / redis – trib. Rb add -node 192.168.99.121:8007 192.168.99.121:8006

/redis-trib.rb add-node An existing node in the new node cluster

When adding node A to the cluster, you need to provide information about node B that already exists in the cluster. Because you know B, you know the whole cluster. Why do you say that? Come on, let’s look at a file and you’ll see.

nodes-xxx.conf

Each node in the cluster has a file that stores information about each node in the cluster: role of the node, node ID, connection status, slot range, IP/PORT information, and so on. If you look closely at the figure, you can see that the new 8007 node is actually the master node by default and has no slot assigned! This means that the newly added node cannot store data yet, so we need to allocate slots for the new node.

[root@mydream121 bin]#./redis-trib.rb reshard 192.168.99.121:8001

Remember that slots are assigned to the master, so all we really need to do is reassign them from the masters of the cluster. The command above needs to specify a master node for Reshard shard.

Slot redistribution

In the figure, 500 slots are allocated to the new node 8007, and the allocation method is “all”. All indicates that slot 8007 is allocated evenly from all existing masters. Of course you can specify a particular master to assign by saying “done”. Note that the assignment to 8007 is specified by the node ID.

Cluster information after allocation

Following the above operations, I add 8008 node to the cluster. My idea is to make 8008 the slave node of 8007. Since it is a slave node, there is no need to assign slots. Note that the node added in Add-Node mode is a master node by default, so we need to use replicate to specify the primary node.

[root@mydream121 bin]#./redis-trib.rb add-node 192.168.99.121:8008 192.168.99.121:8001


Specify the primary node for the slave node

So at this point, we have completed the horizontal expansion of the Redis cluster online. So how do you remove a node? What happens to the data when a node is deleted? For secondary nodes, it doesn’t matter if you delete them. The key is the primary node, because there are slots on the primary node. Therefore, before deleting the master node, we need to reassign the slots on the master node to complete data migration. I’m not going to show you, but I’m going to give you the command.

Delete the primary node: reshard + then del-node

Deleting a secondary node: Directly del-node

Redis implements Session sharing

Redis can be used for Session sharing, but it is now easier for CAS to implement single sign-on. (CAS will be introduced later)

The Redis cluster stores session information

Session information is not lost when Nginx or Tomcat fails. In implementation, there are ready-made plug-ins, such as: https://github.com/jcoleman/tomcat-redis-session-manager

Java operating Redis

In a single Redis environment: Jedis

There is nothing to say about this, just instantiate the Jedis operation for a given IP/PORT.

Jedis jedis = new Jedis(“192.168.99.121”, 8001);

In a multi-redis environment: ShardedJedis+ShardedJedisPool

ShardedJedis way

This is a way to slice redis and hash it evenly among the Redis machines in the pool.

In Redis cluster environment: JedisCluster

JedisCluster

With Spring integration

Let’s go straight to the configuration file!

Redis cluster integration with Spring

In contrast to the code that Java used to operate the Redis cluster above, XML is actually a mapping of the code. Spring just helped us configure a bean:redisCluster. We can get a redisCluster by injection, and then we can do whatever we want, that’s it

This is the end of the Redis cluster section, what is the next article about, I won’t tell you, haha ~





Author: Zhang Fengzhe


Link: https://www.jianshu.com/p/ee18dbd45d02


Source: Jane Book


Brief book copyright belongs to the author, any form of reprint please contact the author to obtain authorization and indicate the source.