Implementation of Redis Multi-machine database

Source: Internet
Author: User
Tags redis cluster redis server

First, copy

In Redis, a user can make a server replicate another server by executing the slaveof command or setting the slaveof option, which we call the replicated server as the primary server (master), while the server replicating the primary server is referred to as the slave server (slave).


Suppose you now have two Redis servers with addresses 127.0.0.1:6379 and 127.0.0.1:12345, if we send the following command to the server 127.0.0.1:12345:

127.0.0.1:12345>slaveof127.0.0.1 6379

Then server 127.0.0.1:12345 will become the slave server for 127.0.0.1 6379, and server 127.0.0.1 6379 will become the primary server for 127.0.0.1:12345.

The database for both the master and slave servers in the replication will hold the same data, which is conceptually referred to as "database state consistent". The set can be executed on the primary server, and the value can be obtained from the server.

Partial resynchronization is achieved by copying the offset, copying the backlog buffer, and the server running ID three parts.

At the beginning of the copy operation, the slave server becomes the client of the primary server, and the replication steps are performed by sending a command request to the primary server, which becomes the client for each other later in the copy operation.

The primary server updates the state from the server by propagating commands to the slave server, keeping the master-slave server consistent, while the slave server sends commands to the primary server for heartbeat detection and command loss detection.

Second, Sentinel

Sentinel (Sentinel, Sentry) is a high availability solution for Redis: A sentinel system consisting of one or more Sentinel instances can monitor any number of primary servers, and all the slave servers under those primary servers, and when the monitored primary server is offline, Automatically upgrade one of the slave servers under the offline primary server to the new primary server, and then continue processing the command request by the new primary server instead of the offline primary server.

Sentinel is just a Redis server running in a special mode that uses a different command table than normal mode, so the commands that Sentinel mode can use are different from the commands that ordinary Redis servers can use.

Third, the cluster

The Redis cluster is a distributed database scheme provided by Redis, where the cluster is sharing data through shards and provides replication and failover capabilities.

For example, a cluster consists of 7000, 7001, and 70,023 nodes, and the diagram shows the entire process of the handshake for the three nodes.


16,384 slots in a cluster can be assigned to individual nodes in the cluster, each of which records which slots are assigned to them. When a node receives a command request, it checks to see if it is responsible for the slot in which the key is being processed, and if not, the node returns a moved error to the client, and the moved error carries information that directs the client to the node that is responsible for the relevant slot.

The re-sharding of Redis clusters is performed by Redis-trib, and the key to re-sharding is to transfer all key-value pairs belonging to a slot from one node to another.

If Node A is migrating slot I to Node B, when node A fails to find the database key specified by the command in its own database, Node A returns an Ask error to the client, directing the client to Node B to continue looking for the specified database key.

The moved error indicates that the responsibility for the slot has been transferred from one node to another, and the Ask error is only a temporary measure used by the two nodes in the process of migrating the slots.

Implementation of Redis Multi-machine database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.