Implementation of multi-machine database
First, copy
Slaveof the primary server IP address. form a master-slave relationship.
1. Synchronization
From sending the sync command to the primary server.
The master server receives the Sync command to execute bgsave, generating the Rdb file, which simultaneously records all commands executed from now on.
The master server executes the Bgsave command, sends the RDB file to the slave server, receives and loads the Rdb file from the server, synchronizes to the primary server state
The primary server sends a buffer command to the slave server, executes the current command from the server, and maintains the final data consistency.
2. Command propagation
The first step is synchronous, similar to initialization.
Follow-up also needs, master-slave server real-time synchronization.
The master server sends the write command synchronously to the slave server for execution. Keep the master-slave consistent.
3, the breakpoint continues to pass
Psync is divided into full synchronization, partial synchronization
(1) Copy offset
-
-
- Each time the primary server wants to create n bytes of data from the server, it also adds its own copy offset to n.
- Receives n bytes of data from the server while updating its own offset plus n.
(2) Copy the backlog buffer
-
-
- The primary server sends the buffer command to the offset from the server while updating the replication backlog buffer, which marks the command byte.
- The master server chooses how to synchronize commands based on this backlog of offsets.
(3) Sync server ID
Choose a different synchronization method based on the ID and the stored ID comparison.
Second, Sentinel
Sentinel strategy is a redis high-availability solution (a Sentinel system consisting of one or more sentinel instances) that can monitor multiple primary servers.
(1) failover
-
-
- Monitoring the main server offline, the election of a slave server under the primary server upgrade to the primary server.
- Notifies the other from the server, updating the primary server address.
- Monitor the downline server and upgrade the offline server to the slave server.
Third, the cluster
Cluster implementation of distributed database solution
1. Node: Cluster Meet <ip><port>
Node nodes send the above command to the specified Ip:port node, and they handshake, and after success, the specified Ip:port node is added to node's cluster.
2. Start the node
A node is a Redis server running in cluster mode, depending on the cluster-enable to determine whether to turn on cluster mode.
3, configuration slot: Through the partitioning method to save the database of key-value pairs, the entire database of the cluster is divided into 0~16384 slots
Cluster Addslots < One or more slots >
Synchronize their slots with each other
The key to be processed by the database is which slot-----No, no, moved, next---until found
Keyway Position algorithm: Cluster Keyslot <key>
4. Re-sharding
Moved the responsibility of the slot has one node transferred to another node
Interim measures used in the Ask slot migration process
5. Replication failover
The cluster is divided into primary nodes and slave nodes, and the primary node is used for processing slots, and child nodes are used to replicate the master node
When the primary node goes offline, the command request is processed in place of the master node.
Cluster replicate <node-id> Node-id is the primary node and the node receiving this command is a child node
Design and implementation of Redis (v)--implementation of multi-machine database