Redis is a redis cluster. Redis is a redis cluster.
(1) Copy
Generally, the master sends data to the slave actively, and the slave receives data stored in the current instance. The ultimate purpose is to ensure data consistency between the two parties and reduce the pressure on the master.
- Copy the way
Master – Slave mode
Slave – Slave
Flowchart for master slave replication
- Slave sends the sync command to the master.
- Master enables the child process to bgSave writes to RDB files and caches the write commands received by the child process.
- When the child process finishes writing, the parent process learns and sends the RDB file to the slave.
- After sending the RDB file, the master sends the cached commands to the slave.
- The master incrementally sends the write command to the slave.
(2) Master/slave replication
Start two Redis, one master and one slave, on the same machine.
cp redis.conf redis-slave.conf
pwd
vi redis-slave.conf
# change slaveof
# slaveof 192.168.70.100:6379
# port 6380
vi redis.conf
# bind 0.0.0.0
#master
./bin/redis-server ./redis.conf &
#slave
./bin/redis-server ./redis.conf &
#slave client
./src/redis-cli -p 6380
# Master client
./src/redis-cli -p 6379
Copy the code
- test
Can the slave client obtain the set value of the master client? 6380 is the slave node port. 6379 is the master node port
#master
set aaa 'aaaa'
#slave
get aaa
config get 'slaveof*'
Copy the code
(2) Cluster
An assembly that provides data sharing between multiple Redis (distributed) nodes.
Cluster deployment Redis The key space of the cluster is divided into 16384 hash slots. The maximum number of nodes in the cluster is 16384: Cluster >node> Slot >key.
- shard
Redis Cluster is not designed with Consistency Hashing, but with data sharding introducing hash slot. A Redis Cluster contains 16384 (0 to 16383) hash slots into which all keys stored in a Redis Cluster are mapped. Each key in the Cluster belongs to one of the 16384 hash slots. The cluster uses public slot=CRC16 (key) /16384 to calculate which slot the key belongs to. The CRC16(key) statement is used to calculate the CRC16 checksum of the key.
Shard by slot. By assigning a different number of slots to each node, you can control the amount of data and requests that different nodes are responsible for.
The current cluster has three nodes, and slots are evenly divided by default
- Node A (6381) contains hash slots 0 through 5499.
- Node B (6382) contains hash slots 5500 to 10999.
- Node C (6383) contains hash slots 11000 to 16383.
This structure makes it easy to add or remove nodes. For example, if I want to add A new node D, I need to take some slots from nodes A, B, and C and add them to D. If I want to remove node A, I need to move the slots in NODE A to nodes B and C, and then remove node A without any slots from the cluster. Since moving a hash slot from one node to another does not stop service, adding or deleting or changing the number of hash slots on a node does not make the cluster unavailable.
- Data migration
Data migration can be understood as slot and key migration. This feature is very important because it greatly facilitates linear scaling and smooth scaling.
Slot 1, slot 2, and Slot 3 on Master A will be migrated to Master B. Slot 1, 2, and 3 are in the MIGRATING state on Master A, and in the IMPORTING state on Master B.
IMPORTING state; The state of the migrated slot in the target Master B node. During the migration from Mater A to Master B, the state of the migrated slot becomes IMPORTING first.
Key space migration.
The nodes of the cluster have replication and high availability features built in.
The characteristics of
- Automatic Node discovery
- Slave -> Master election, fault tolerant cluster
- Hot resharding: Indicates online sharding
- Cluster management based on configuration (nodes-port.conf)
- The client is directly connected to the Redis node without intermediate proxy layer.
- All redis nodes are ping-pong with each other and use binary protocols internally to optimize transmission speed and bandwidth.
PS: Set up a Redis cluster environment using multiple VMS next time. This time, we mainly understand the principle of cluster synchronization and cluster expansion ability.