Redis is an open source, network-enabled, memory-based and persistent logging, key-value database written in ANSI C, and provides multiple language apis. As of March 15, 2010, the development of Redis is hosted by VMware. Development of Redis has been sponsored by Pivotal since May 2013. A wave of fan welfare arrangements, prepared in the work, we take

10 years of architects share advanced PHP architecture information to help everyone achieve 30K

zhuanlan.zhihu.com](zhuanlan.zhihu.com/p/340304217)

My official group click here. Overview In existing enterprises, 80% companies mostly use redis stand-alone service. In actual scenarios, single-node RedIS is prone to risks. 1. Machine failure. We deployed to one Redis server, and in the event of a machine failure, we needed to migrate to another server and keep the data synchronized. Data is the most important thing, and if you don’t care, you probably won’t use Redis. 2. Capacity bottleneck. When we need to expand the Redis memory, from 16G to 64G, it is definitely not enough for a single machine. Sure, you can buy a new 128GB machine. In order to achieve greater storage capacity and withstand high concurrent traffic of distributed database, we will separately store the data of the original centralized database on multiple network nodes. In order to solve the problem of single node, Redis will also deploy multiple copies of data replication to other nodes for replication to realize high availability of Redis and redundant backup of data, thus ensuring high availability of data and services. Primary secondary Replication What is primary secondary replication

Primary/secondary replication refers to the replication of data from one Redis server to other Redis servers. The former node is called the master node and the latter is called the slave node. Data replication is one-way and can only be transmitted from the master node to the slave node. By default, each Redis server is the primary node; And a master node can have multiple slave nodes (or none), but a slave node can only have one master node. Functions of primary/secondary replication 1. Data redundancy: Primary/secondary replication implements hot backup of data and provides data redundancy other than persistence. 2. Fault recovery: When the primary node is faulty, the secondary node provides services for rapid fault recovery. It’s actually redundancy of services. 3. Load balancing: On the basis of master/slave replication and read/write separation, the master node provides write service and the slave node provides read service (that is, when Redis data is written, the application connects to the master node and when Redis data is read, the application connects to the slave node) to share server load. Especially in the scenario of less write and more read, the concurrency of the Redis server can be greatly increased by sharing the read load with multiple slave nodes. 4. Read and write separation: can be used to achieve read and write separation, master library write, read from the library, read and write separation can not only improve the load capacity of the server, but also according to the change of demand, change the number of slave library; 5. High availability cornerstone: In addition to the above, master-slave replication is the foundation upon which sentry and clustering can be implemented, hence master-slave replication is the foundation of High availability in Redis.

Enable Primary/secondary replication There are three ways to enable primary/secondary replication on a secondary node: 1. Configuration file: Add slaveof to the configuration file of the secondary server. 2. Start command: redis-server Start command and add –slaveof 3. Client command: After the Redis server is started, run slaveof on the client to make the Redis instance become the slave node. Using the info replication command, you can view some replication information. Master/slave replication Principle Master/slave replication can be divided into three phases: connection establishment (preparation), data synchronization, and command transmission. After executing the slaveof command from the node, the replication process starts, as can be seen in the following diagram, which shows that the replication process is roughly divided into six processes

This process can also be seen in logging after master/slave configuration. 1) Save master information. After executing slaveof, Redis will print the following log:

2) The replication logic is maintained internally by the scheduled tasks running every second on the slave node. When a scheduled task discovers a new master node, it tries to establish a network connection with the slave node

Establishing a network connection between the secondary node and the primary node The secondary node establishes a socket, and the secondary node establishes a socket with port 51234 to receive the replication command sent by the primary node. The following log is displayed after the secondary node is successfully connected:

If the slave node fails to establish a connection, the scheduled task will retry indefinitely until the connection succeeds or run slaveof no one to cancel replication. You can view the master_link_down_since_seconds indicator by executing Info Replication on the secondary node, which records the system time when the connection to the primary node fails. If the secondary node fails to connect to the primary node, the following logs are also generated every second for troubleshooting: # Error condition on socket for SYNC: {socket_error_reason} 3) Send the ping command. After the connection is successfully established, the secondary node sends a ping request for the first communication. The main purposes of the ping request are as follows: · Check whether the network socket between the primary and secondary nodes is available. · Check whether the master node can currently accept processing commands. If the secondary node does not receive a pong reply from the primary node or times out after the ping command is sent, for example, the network times out or the primary node is blocking and cannot respond to the command, the secondary node will disconnect the replication connection and the next scheduled task will initiate a reconnection.

The ping command sent from the node is returned successfully. Redis prints the following log and continues the subsequent replication process:

4) Permission verification. If the requirepass parameter is set on the primary node, password authentication is required. The masterauth parameter must be set on the secondary node to ensure that the password is the same as that on the primary node. Replication is terminated if validation fails and the replication process is re-initiated from the node. 5) Synchronize data sets. After the primary node communicates with the secondary node, the primary node sends all data to the secondary node for the first time. This is the most time-consuming step. 6) Command continuous replication. When the master node synchronizes the current data to the slave node, the replication process is completed. Then the primary node continuously sends the write command to the secondary node to ensure data consistency between the primary and secondary nodes. Interview questions for big companies

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

zhuanlan.zhihu.com](zhuanlan.zhihu.com/p/340637391)

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc.