Three modes of Redis cluster
1. Master-slave mode
Through the persistence function, Redis ensures that no data will be lost (or a small amount of loss) even when the server is restarted, because persistence will save the data in the memory to the hard disk, and the restart will Load data from hard drive.
However, since the data is stored on a server, if the server has problems such as hard disk failure, data loss will also occur. In order to avoid single points of failure, a common practice is to replicate multiple copies of the database and deploy them on different servers, so that even if one server fails, other servers can still continue to provide services. To this end, Redis provides a replication function, which can automatically synchronize the updated data to other databases when the data in one database is updated.
In the concept of replication, databases are divided into two categories, one is the master database (master), and the other is the slave database (slave). The master database can perform read and write operations. When the write operation causes data changes, the data will be automatically synchronized to the slave database. The slave database is generally read-only and accepts data synchronized from the master database. A master database can have multiple slave databases, and a slave database can only have one master database.
Configuration of master-slave database
The master database does not need to be configured. You can load the slave database information from the redis conf file, or you can use redis- at startup. server --port 6380 --slaveof 127.0.0.1 6379
The slave database is generally read-only and can be changed to writable, but the written data can easily be synchronized by the master, so it can still be read-only.
You can also use the slaveof ip port command at runtime to stop the original master and switch to the master just set slaveof no one will turn itself into the master
Copy Principle
When starting from the database, a sync command will be sent to the main database. After receiving the sync, the main database starts to save the snapshot rdb in the background. The commands received during the snapshot are cached. When the snapshot is completed, The master database will send the snapshot and cached commands to the slave together. Copy initialization ends.
After that, every time the master receives a command, it will be sent to the slave synchronously.
When there is a disconnection and reconnection, versions after 2.8 will pass the commands during the disconnection to the reconnection database. Incremental replication
Master-slave replication is optimistic replication. When the client sends write execution to the master, the master immediately returns the results to the client after execution, and sends the command to the slave asynchronously, thus not affecting performance. You can also set the minimum number of slave masters to be synchronized before writing.
No hard disk copy: If the hard disk efficiency is low, it will affect the copy performance. After 2.8, you can set up no hard disk copy, repl-diskless-sync yes
##2. Sentinel
When the master database encounters an abnormal service interruption, developers can manually select a slave database to upgrade to the master database so that the system can continue to provide services. However, the whole process is relatively cumbersome and requires manual intervention, making it difficult to automate. To this end, Redis 2.8 provides the sentinel tool to implement automated system monitoring and fault recovery functions. The role of the sentinel is to monitor whether the redis master and slave databases are running normally. If the master fails, it will automatically switch from the slave database to the master database. Example: 1 master 2 slave 1 sentinelredis-server --port 6379 redis-server --port 6380 - -slaveof 192.168.0.167 6379 redis-server --port 6381 --slaveof 192.168.0.167 6379Sentinel configuration file sentinel.conf sentinel monitor mymaster 192.168.0.167 6379 1The 1 here represents 1 sentinelNote:
3. Cluster (cluster-enable)
To use the cluster, you only need to turn on the cluster-enable configuration of each database node. . At least three master databases are required in each cluster to function properly. Even if Sentinel is used, each redis instance is fully stored, and the content stored in each redis is complete data, which wastes memory and has a barrel effect. In order to maximize the use of memory, clusters can be used, which is distributed storage. That is, each redis stores different content. The cluster requires at least 3 masters and 3 slaves, and each instance uses a different configuration file. The master and slaves do not need to be configured, the cluster will choose itself. Modify the configuration file of each instance:cluster-enabled yes --Open the cluster cluster-config-file nodes-6382.conf --Cluster Configuration file name, Each instance must be configured differently, redis will automatically create a new one based on the file name
Operation of the cluster
Execute src of the redis installation directory./redis-trib.rb create --replicas 1
127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382
127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385
The master election here is similar to that of zookeeper
For more related knowledge, please visit PHP Chinese website! !
The above is the detailed content of What are the three modes of Redis cluster?. For more information, please follow other related articles on the PHP Chinese website!