In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the detailed analysis of Redis master-slave synchronization, the content is clear, interested friends can learn, I believe it will be helpful after reading.
First, the principle of master-slave synchronization of Redis
1.1 the process of master-slave synchronization of Redis
After configuring the master to which the slave server connects, slave establishes a connection to the master and sends the sync command. Whether it is a connection established at the first synchronization or a reconnection after a disconnection, master starts a background process to save the database snapshot to a file. At the same time, the master main process starts to collect new write commands and cache them. When the background process finishes writing the file, master sends the snapshot file to slave,slave, saves the file to disk, and then loads it into memory to restore the database snapshot to slave. After slave finishes restoring the snapshot files, master forwards all cached commands to slave,slave to update the in-memory database. Subsequent write commands received by master are sent to slave through the connection that was initially established. Commands for synchronizing data from master to slave use the same protocol format as commands sent from client to master. When the connection between master and slave is disconnected, slave can automatically reestablish the connection. If master receives synchronous connection commands from multiple slave at the same time, only one process is started to write database mirroring and then sent to all slave.
1.2 the characteristics of master-slave synchronization of Redis
Master-slave synchronization has obvious distributed cache characteristics, which mainly includes these aspects:
1) A master can have multiple slave, and a slave can have multiple slave
2) slave can connect not only to master,slave but also to other slave to form tree structure.
3) Master-slave synchronization will not block master, but it will block slave. That is, when one or more slave synchronizes data with master for the first time, master can continue to process requests from client. On the contrary, slave blocks requests that cannot be processed by client when synchronizing data for the first time
4) Master-slave synchronization can be used to improve the scalability of the system. We can use multiple slave to specifically handle client read requests, or it can be used for simple data redundancy or persistence only on slave to improve the overall performance of the cluster.
1.3 Redis active synchronization setting method
There are two ways to complete synchronization settings for master and slave Redis servers. Both need to be done on the slave server to specify the Redis server to which the slave needs to connect (either master or slave).
1.3.1 set in the configuration file
Set in the configuration file (redis.conf) of the Redis server that is the slave.
Conf code
Slaveof 10.1.1.102 6379 # specify the ip and port of master
Obviously, this setup is very simple, but the configuration file needs to be modified, and the configuration file is loaded when the server starts. Therefore, the server cannot be modified if it is not started, and the operation is not flexible.
This configuration is suitable for the initial configuration at deployment time.
1.3.2 setting in the Redis client
Here we take Jedis, which is officially recommended by Redis, as an example, and the later tests are also based on Jedis. Here the jedis object instance belongs to slave, and the parameters are the address and port of the server.
Java code
SlaveJdedis.slaveOf ("10.1.1.102", 6379); # specify the ip and port slaveJdedis.slaveofNoOne of master (); # Unspecify master and become a master
Through the way specified by the client, the master-slave relationship between master and slave server can be easily modified. So this approach is very suitable for tuning master and slave servers online as needed.
1.3.3 problems existing in current master-slave synchronization
Because the master and slave servers are not automatically elected by Redis and require manual participation, master-slave switching can not be completed automatically. There is a question of when and who will trigger the switch. I took a look at the client does not have this ability, if necessary, you need to add your own.
Jedis currently randomly chooses which Redis server to read, so to achieve automatic distributed reading, we need to re-encapsulate the Jedis.
1) A mechanism needs to be developed to detect the working status of master and slave as soon as possible.
2) an automatic switching strategy for master and slave needs to be defined.
3) A mechanism that can read any Redis server randomly needs to be defined.
These functions can be implemented on the client side, but the results will not be very good. It would be perfect if the server itself can support it, but from the introduction of Redis's official website, it seems that no one has mentioned such a requirement or such a plan.
II. Introduction of mainstream Redis clients
On the official website of Redis, there are five Redis java client software listed. Jedis is the official java client recommended by Redis, and this one has been maintained and updated. At present, the latest stable version of the server is Redis2.4.17, and the latest test version, Redis 2.6.0 RC7.
2.1 Jedis
Jedis is the client version of Java officially recommended by Redis. The latest version is Jedis 2.1.0-5, which is fully compatible with Redis 2.0.0. This client is maintained and updated all the time.
2.2 JRedis
JRedis has not been updated for a long time and is fully compatible with Redis version 2.0.0. It will be compatible with the latest Redis2.6.0 test version after being updated before May today.
2.3 JDBC-Redis
JDBC-Redis is the JDBC driver for Redis, the NoSQL database. It can only be downloaded to the jdbc-redis_0.1_beta version released in March 2009, which is no longer maintained.
2.4 RJC
RJC provides Apache DBCP-style connection pooling. The update was stopped a year ago and is fully compatible with Redis version 2.0.0.
2.5 redis-protocol
This update is the fastest and most frequent and is compatible with the latest version of Redis 2.6.0. However, it is positioned to fully support the Redis protocol and exchange data with the Redis server more efficiently. Therefore, the function of redis server is not fully utilized.
2.6 overall evaluation of each Java client
On the whole, each client basically implements the basic functions defined by the Redis protocol. Redis-protocol updates recently have the most complete support for the Redis protocol; Jedis provides more configuration operations for the Redis server, which is the most convenient to use. Other clients are rarely maintained and the functionality is general.
If you want to expand the functionality of the client a little, it is the quickest to develop based on Jedis.
If you want maximum compatibility and extension of client functionality, based on Redis-protocol is the best choice.
III. Suggestions on the use of master-slave synchronization in Redis
Redis master-slave synchronization is not well supported by all Java clients at present. The main reason should be the limitation of the implementation mechanism of the Redis server itself. It is also possible if you have to do it, but the effect may be reduced.
3.1Encapsulation of Jdedis
1) add a new management class, which is responsible for maintaining the server topology relationship of the Redis server cluster.
2) add a new monitoring class, which is responsible for monitoring and maintaining the running status of servers in the Redis server cluster
3) add a new Master selection policy class, which is responsible for determining the switching time between master and slave, and selecting the most appropriate Redis server to act as master.
4) add a proxy class to take over the read and write operations of the current Jedis client to the Redis server. The application layer uses the Jedis client through proxy classes. The proxy class needs to ensure that the Redis server cluster is transparent to the application layer.
After reading the above content, do you have a further understanding of the detailed analysis of Redis master-slave synchronization? if you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.