In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Redis is a key-value storage system. Similar to Memcached, it supports a relatively large number of stored value types, including string, list, set, zset, and hash. These data types all support push/pop, add/remove, and take intersection union and subtraction, as well as richer operations, all of which are atomic. On this basis, redis supports a variety of different ways of sorting. As with memcached, data is cached in memory for efficiency. The difference is that redis periodically writes updated data to disk or changes to additional log files, and on this basis achieves master-slave synchronization.
overview
80% of existing enterprises use redis stand-alone service, and redis of a single node is vulnerable to risks in actual scenarios.
problems faced
1. Machine malfunction. We deployed to one Redis server, and when the machine failed, we needed to migrate to another server and make sure the data was synchronized. And data is the most important thing, if you don't care, you basically won't use Redis.
2. Capacity bottleneck. When we need to expand Redis memory, from 16G memory to 64G, stand-alone is definitely not enough. Of course, you can buy a new 128-gigabyte machine.
solutions
To achieve greater storage capacity for distributed databases and to withstand high concurrent access, we would replace the centralized data
The database data is stored on other network nodes.
In order to solve this single node problem, Redis will also deploy multiple copies of data replication to other nodes for replication to achieve high availability of Redis and redundant backup of data, thus ensuring high availability of data and services.
master-slave replication
What is master-slave replication?
Master-slave replication refers to copying data from one Redis server to other Redis servers. The former is called the master node, the latter is called the slave node, and the replication of data is unidirectional, only from the master node to the slave node.
By default, each Redis server is a master; a master can have multiple slaves (or no slaves), but a slave can have only one master.
master-slave replication
1. Data redundancy: Master-slave replication realizes hot backup of data and is a data redundancy method other than persistence.
2. Failure recovery: When the master node has a problem, the slave node can provide services to achieve rapid failure recovery; in fact, it is a redundancy of services.
3. Load Balancer: On the basis of master-slave replication and in coordination with read-write separation, master nodes can provide write services, and slave nodes can provide read services (i.e., connect master nodes when writing Redis data, and connect slave nodes when reading Redis data) to share server load; especially in scenarios where there are fewer writes and more reads, read load can be shared by multiple slave nodes, which can greatly improve the concurrency of Redis servers.
4. Read-write separation: can be used to achieve read-write separation, master library write, slave library read, read-write separation can not only improve the load capacity of the server, but also change the number of slave libraries according to changes in demand;
5. High availability cornerstone: In addition to the above roles, master-slave replication is also the basis for sentinel and cluster implementation, so master-slave replication is the basis for Redis high availability.
Master-slave replication enabled
There are three ways to start master-slave replication from a node:
1. Configuration file: Add the following to the configuration file of the slave server:
slaveof
2. Start command: redis-server Add after start command
--slaveof
3. Client command: After Redis server starts, execute the command directly through the client:
slaveof
The Redis instance becomes a slave node.
The process of master-slave replication can be roughly divided into three stages: connection establishment stage (i.e. preparation stage), data synchronization stage, and command propagation stage. After executing the slaveof command from the node, the replication process begins. As you can see in the following diagram, the replication process is roughly divided into six processes.
This process can also be seen in logging after master-slave configuration
1) Save master node information.
Redis prints the following log after executing slaveof:
2) The slave maintains replication related logic through timed tasks running every second. When a timed task finds a new master node, it will try to establish a network connection with that node.
Slave node establishes network connection with master node
The slave node creates a socket on port 51234, which is dedicated to receiving replication commands from the master node. Print the following log after successful connection from node:
If the slave node fails to establish a connection, the timed task retries indefinitely until the connection succeeds or executes a slaveof noone to cancel replication. For connection failures, you can perform info replication on the slave node to view the master_link_down_since_seconds metric, which records the system time when the connection to the master node failed. from
When the node fails to connect to the master node, the following log will be printed every second to facilitate the discovery of problems:
# Error condition on socket for SYNC: {socket_error_reason}
3) Send ping command.
After the connection is successfully established, the node sends a ping request for the first communication. The main purpose of the ping request is as follows:
·Detect whether network sockets are available between master and slave.
·Check whether the master node can currently accept processing commands.
If the slave node does not receive a pong reply from the master node after sending the ping command or timeout occurs, such as network timeout or the master node is blocking and cannot respond to the command, the slave node will disconnect the replication connection and initiate a reconnect at the next scheduled task.
The ping command sent from the node successfully returns, Redis prints the following log, and continues the subsequent replication process:
4) Authorization verification. If the master node has set the requirepass parameter, password authentication is required, and the slave node must be equipped with
Set the masterauth parameter to ensure that the password is the same as that of the master node before it can pass authentication; if authentication fails, replication will end.
The replication process is restarted from the node.
5) Synchronized data sets. After the master-slave replication connection communicates normally, the master node will control the scenario where replication is established for the first time.
Some data is sent to the slave node, which is the longest step.
6) Command continuous replication. When the master synchronizes the current data to the slave node, the replication process is completed.
Then the master node will continue to send write commands to slave nodes to ensure data consistency between master and slave.
The above is Redis master-slave copy introduction and principle detailed content, more please pay attention to other related articles!
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.