Website: www.redis.cn
redis deployment
gcc and make are required for make If you need to execute this command in a pure environment
[root@server3 redis-6.2.4]# yum install make gcc -y
Comment out these lines
vim /etc/redis/6739.conf
2. Redis master-slave replication
Setting 11 is master 12 13 is slave
on 12
Other nodes infer from this
Currently on 11 master
The slave role can only read without modifying permissions
when on the master
Synchronization will be achieved in the slave
redis high availability
redis master-slave switch
Redis master-slave principle: based on rdb snapshots to realize the difference between it and MySQL's binary log (different data types)
When executing master-slave: slave and master need to do an authentication first to initiate a synchronous request, the master will execute a bgsave (asynchronous mode)/save (blocking mode), in asynchronous mode, bgsave will make an rdb snapshot, and then put The rdb snapshot is sent to the slave, and the slave will take an action to clear all the data on the slave side (flushall), and then the slave will load the snapshot again,
After the snapshot is completed, there will be changed data, so put it in the cache, and then use the replicationfeedslave() incremental function to send it to the slave one by one, and then synchronize the redis master-slave based on the rdb snapshot format for the slave side.
Redis master-slave implementation based on snapshot rdb snapshot format
One-master-multiple-slave architecture for high-availability switching comes with redis
2 means: how to judge that the master is out of order, two nodes must say that the master is out of order to initiate a failover
When the first node finds that the master fails, it will enter a subjective offline state
When the second node finds that the master fails, it will enter a subjective offline state again
Two subjective offlines will trigger an objective offline, at this time the master will actually start the master-slave switch
Three nodes are set to 2 and two nodes are set to 1
The action of copying files must be started before
Other hosts directly start the service without changing the configuration file
This is sentinel mode
At this time, open a terminal 11 11 is the master at this time
close the master
Redis cluster will automatically switch master
When the original master starts up again, it will join the cluster as a slave
Problems that may arise when switching
There is no problem from the client to the master, if the network from the master to the slave fails
At this time, the master will be kicked out to elect a new master slave.
When the network is normal again, the original master will synchronize the configuration of the current cluster and turn itself into a slave to connect to the new master. In this way, all data on the original master will be lost.
But it is good from the client to the master. At this time, the client will continue to write data to the master. The client has been writing, but the master to the slave fails. The master will become a slave and the data will be cleared.
how to solve it
It is necessary to ensure that there are two slaves on the backend that can write
Add min-slave-to-write=2 to the configuration file
Ensure that the client does not write to the master when the master-slave is switched. If the master finds that the two slaves cannot be connected, do not write data to it to avoid data loss.
Redis cluster
Decentralized design. Every node can be connected to the cluster. Both master and slave can read and write. After accessing the cluster, scheduling and redirection can be done, but its integration is too high, and the secondary development cost is too high.
The corresponding configuration files and data will be generated in the current directory
This creates a cluster
View command help
redis-cli --help
Get cluster status
Details of the master-slave relationship
redis-cli --cluster actually distributes a total of 16384 hash slots to the three masters for storage. Each node will be allocated a certain percentage of hash slots, which are used to store data.
connect cluster
Close the redis instance, the cluster automatically switches
The master of 30002 is closed, and the slave before 30005 will take over and become the master
At this point the data will be on 30005 and the data will not be lost
The above is the built-in high availability switching of redis
If you start the script again, the 30002 that was just closed will be pulled up again
After pulling it up, 30002 is the slave
The roles were reversed at this time
When it is set, it will cause the node entering the group to be unavailable
1 These 16384 hash slots are not available if the cluster is incomplete
2. Half of the masters of the same cluster hang up at the same time
Start two more redis instances
ps ax
Add cluster nodes online
The newly added node has no hash slot, and the role is master
Add slave node
Re-migrate the hash slot
When to delete which node
You have to migrate the hash slot on this node to another machine
Otherwise, the hash slot will be incomplete and the cluster will be shut down