introduce
A Redis cluster is a combination of multiple Redis services and a strategy that allows them to do their job. When a user accesses Redis, an algorithm sends the request to different Redis services.
While master-slave replication reduces read/write stress, clustering effectively alleviates memory stress.
Redis cluster achieves horizontal expansion of REDis, that is, start N REDis nodes, store the entire database in these N nodes, and each node stores 1/N of the total data.
Redis clusters provide a degree of Availability through partitions, allowing the cluster to continue processing command requests even if some nodes in the cluster fail or fail to communicate.
The CAP theory of Consistency, Availability, and Partition fault tolerance can be referred to as follows: 1. Redis Preface
Environment to prepare
- 2. Redis installation
- 12. Ruby installation
- Make six instances of Redis and modify the cluster configuration
Copy six copies of redis.conf from the redis installation directory to the /usr/local/redis-cluster path and rename the configuration files using the boot port as the identity.
Conf /usr/local/redis-cluster/redis6379.conf cp your redis installation directory /redis.conf /usr/local/redis-cluster/redis6380.conf cp your redis installation directory /redis.conf /usr/local/redis-cluster/redis6381.conf cp Your redis installation directory /redis.conf /usr/local/redis-cluster/redis6389.conf cp your Redis installation directory /redis.conf /usr/local/redis-cluster/redis6390.conf cp your redis installation directory /redis.conf /usr/local/redis-cluster/redis6391.conf
Use the Vim editor to modify the six configuration files one by one. Take redis6379.conf as an example.
- Open daemonize
Daemonize yes 2. Change the pidfile name. Pidfile /var/run/redis_6379.pid 3. Change the logfile name logfile “6379.log” 4. Changing port port 6379 5. Change the dbfilename filename dbfilename dump6379.rdb 6. Disable persistent AOF appendonly No or change the appendfilename filename appendfilename “appendonly6379.aof” 7. Enable cluster mode cluster-enabled yes 8. Set the node configuration file cluster-config-file nodes-6379.conf 9. Set the node disconnection time. After the time (milliseconds), the cluster automatically performs a primary/secondary switchover cluster-node-timeout 15000
After modifying the redis6379.conf file, modify the other five files in the same way, just change the 6379 shown above to their respective startup port numbers.
Start the
Start each of the six Redis instances.
redis-server redis6379.conf
redis-server redis6380.conf
redis-server redis6381.conf
redis-server redis6389.conf
redis-server redis6390.conf
redis-server redis6391.conf
To viewps -ef|grep redis
Check whether the startup is successful.Generated after successful startupnodes-xxxx.conf
File.
fit
Putting multiple Instances of Redis in a cluster is called aggregation.
Execute the combined command in the SRC directory of the redis installation path:
Refdis5.0 prior to release: ./redis-trib.rb create –replicas 1 your ip:6379 your ip:6380 your ip:6381 your ip:6389 your ip:6390 your ip:6391
Versions after Redis5.0 execute: Redis-cli –cluster create your IP :6379 your IP :6380 your IP :6381 Your IP :6389 your IP :6390 127.0.0.1:6391 –cluster-replicas 1
Your IP can be viewed using ifconfig or 127.0.0.1 instead.
Can I set the above configuration? (type ‘yes’ to accept): Indicates whether you are satisfied with the master/slave server and slot assignments assigned by the cluster.
The –replicas 1 or -cluster-replicas 1 parameter indicates that a secondary node is created for each primary node.
A cluster must have at least three primary nodes. The allocation principle tries to ensure that each master database runs on a different IP address, and each slave and master are not on the same IP address.
Conclusion by the startup information: host 6379 slots value: c1a9f0179ab1737ce167f0c14f0129f3ba9d2f9a, slot index range: (0-5460), 6389 to 6379.
Host 6380 slots value: 58 e4ac4f4299b79e9cb9a24c018bc566885cff76, slot index range: (5461-10922), 6390 to 6380.
Host 6381 slots value: 4 cdaa7eb6dcc18865b0726147d6be72a4d2fe374, slot index range: (10923-16383), 6391 to 6381.
What are slots? A Redis cluster contains 16384 hash slots. Each key in each library belongs to one of the 16384 hash slots. CRC16(key)%16384 Calculates which slot the key belongs to. CRC16(key) is used to calculate the CRC16 checksum of the key.
Connect the client to port 6379 redis service in cluster mode.
redis-cli -c -p 6379
-c Indicates automatic data redirection. And run set k1 v1 to save the data.
Algorithmic key K1 should be sent to slot 12706, within the host’s index of slot 6381, and then automatically redirected to 6381, where K1 is stored in the 6381 database.
The concept of a group can be defined by {group name} so that all keys of the same group are in the same database service.The keys name, age, and sex belong to the user group and are all in the 6380 service.
Question 1: If the primary node is down, can the secondary node become the primary node automatically?
The following uses the 6379 service as an example.
redis-cli -p 6379 shutdown
After the 6379 is down, use the client to connect to the 6380 service and view cluster information.
redis-cli -c -p 6380
Viewing Cluster Information
cluster nodes
Conclusion: After 6379 goes down, 6389 is automatically upgraded to the master library.
Question 2: What happens to the master/slave relationship after the host recovers?
Restart the 6379 service
redis-server redis6379.conf
Cluster-require-full-coverage Yes Indicates that services can be provided only when all slots (16379) are normal. The default value is yes. If a host is down, the corresponding slot is unavailable and services cannot be provided externally
Cluster command
CLUSTER INFO Displays CLUSTER information.
CLUSTER NODES Lists all known NODES in the CLUSTER and their information.
// Node CLUSTER MEET Adds the node specified by IP and port to the CLUSTER and makes it a part of the CLUSTER.
CLUSTER FORGET Removes the node with node_id from the CLUSTER.
CLUSTER REPLICATE sets the current node to the secondary node specified by node_id.
CLUSTER SAVECONFIG saves the node configuration file to the disk.
// slot CLUSTER ADDSLOTS [slot…] Assign one or more slots to the current node.
CLUSTER DELSLOTS [slot …] Removes one or more slots assigned to the current node.
The CLUSTER FLUSHSLOTS removes all slots assigned to the current node, making the current node a node with no slots assigned to it.
CLUSTER SETSLOT NODE assigns a slot to the NODE specified by node_id. If the slot has been assigned to another NODE, the other NODE deletes the slot before assigning the slot.
CLUSTER SETSLOT MIGRATING Migrates the slots of this node to the node specified on node_id.
CLUSTER SETSLOT IMPORTING imports slots from the node specified node_id into the node.
CLUSTER SETSLOT STABLE Cancels the import or migration of slots.
// Key CLUSTER KEYSLOT calculates the slot on which the key should be placed.
CLUSTER COUNTKEYSINSLOT Return slot Number of key/value pairs currently contained in slot.
CLUSTER GETKEYSINSLOT returns count of slot keys.
The reference: codekiller. Top / 2020/03/30 /…
The cluster advantages
- Implementation capacity
- Share the pressure
- The no-center configuration is relatively simple
Cluster faults
- Multi-key operations such as Mset and Mget are not supported.
- Multi-key Redis transactions are not supported.