In the previous blog, we discussed how to implement caching in spring boot using Redis.
But the database is clearly a single point is not enough, then how to use the Redis database to do the cache cluster it. Let's look into it today.
Redis supports Master-slave (master-slave) mode, where Redis server can be set up as a host (slave) for another Redis server, from which the data is periodically taken from the host computer. Special, a Slave can also be set to a Redis server host, so that the distribution of Master-slave is a directed acyclic graph DAG, so as to form a Redis server cluster, whether the host or slave are Redis server, can to provide services.
After configuration, the host master can be responsible for reading and writing services, from the machine slave only read. Redis improves this configuration so that it supports weak consistency of data, that is, eventual consistency. In the business, the choice of strong consistency or weak consistency should depend on the specific business needs, such as microblogging, the use of weak consistency model can be used, like Taobao, can choose strong consistency model.
principle Brief:
Redis Master-slave replication can be divided into full-volume synchronization and incremental synchronization based on whether it is full.
Full-Volume synchronization:
Redis full replication typically occurs during the slave initialization phase, when slave needs to copy all the data on master.
Incremental synchronization:
Redis incremental replication refers to the process of synchronizing a write operation that occurs on the primary server to the slave server when the slave starts to work correctly after initialization.
The process of incremental replication is primarily when the primary server executes a write command that sends the same write command to the server, receives and executes the received write command from the server.
After knowing the approximate structure and principle, let's start with it. How to implement Redis master-slave.
First go to the Redis directory and find the Redis configuration file. Linux is the redis.conf. Under Window is redis.windows.conf
Make a copy of this file. Make the following changes:
Modify Port:
Port 6380
Add slaveof under this #slaveof <masterip> <masterport> comment to indicate that this is the slave server for a server.
slaveof <masterip> <masterport>
slaveof 127.0.0.1 6379
In this way, a simple master-slave configuration is completed.
Perform the following tests:
Start Master First, and then start salve from the server.
You can see both of them print logs, master and slave configuration succeeded.
Opens two new terminal.
Run the corresponding client separately:
As you can see, the set key in master can get to value from the server slave. And slave is read-only by default and cannot be modified on the cache. Of course, you can modify this option (Slave-read-only No) and modify the slave permissions.
Multi-level Master-slave is the same.
Like Master->slave1->slave2.
Only need to execute a salve2, modify salveof as slave1 from the server can do.
It is important to note that a Redis server becomes a server salve, if multiple salveof are configured, whichever is the last.
The Redis cluster is configured to greatly reduce the burden on the cache server, and only the write operation needs to be performed on master, while other read operations can be read from salve, and we know that most of the database operations are read operations.