search
HomeDatabaseRedisSummary and sharing of three cluster modes of Redis

Recommended learning: Redis video tutorial

Three cluster modes

  • redis has three clusters mode, of which master-slave is the most common mode.
  • Sentinel Sentinel mode was evolved to make up for the complexity of master-slave switchover after the host in the master-slave replication cluster goes down. As the name suggests, Sentinel is used for monitoring. Its main function is to monitor the master-slave cluster, automatically switch between master and slave, and complete cluster failover.
  • cluster mode is the cluster mode officially provided by redis. It uses Sharding technology to not only achieve high availability, read and write separation, but also achieve true distributed storage.

1. Master-slave replication

redis master-slave replication

1. reids master-slave mode

2. Redis replication principle

Redis replication is divided into two parts: operation synchronization (SYNC) and command propagate (command propagate)

  • Synchronization (SYNC) is used to update the status of the slave server to be consistent with the master server. The vernacular explanation is to actively obtain the data of the main server from the server. Keep your data consistent. The specific implementation is that after receiving the SYNC command, the master server generates an RDB snapshot file and then sends it to the slave server.
  • Command propagate (command propagate) is used for command propagation in order to keep the slave server consistent with the master server state after the master server data is modified and the master slave is inconsistent. The vernacular explanation is that after the master server receives the client's data modification command, the database data changes, and the command is cached, and then the cached command is sent to the slave server. The slave server achieves master-slave data consistency by loading the cached command. This is called command propagation.
  • Why there are two replication operations of synchronization and command propagation: When there is only synchronization operation, when the slave server sends the SYNC command to the master server, the master server will still receive it when generating the RDB snapshot file. The client's command modifies the data status. If this part of the data cannot be communicated to the slave server, then the master-slave data will be inconsistent. At this time, command propagation occurs. After the master server receives the SYNC command from the slave server, it generates an RDB snapshot file and caches the commands received during this period, and then uses the command propagation operation to send them to the slave server. To achieve master-slave data consistency.

3. Principle of redis master-slave replication

The two operations of redis replication are introduced above, and redis master-slave replication is officially based on synchronization and Command propagation is achieved. The following two pictures show the redis replication process:

##4. Advantages and disadvantages of redis master-slave replication

Advantages:

1. Realizes read-write separation, improves availability, and solves single-machine failures. 2. During master-slave replication, both master and slave are non-blocking and are still available.

Disadvantages:

1. During the master downtime, you need to manually switch the host. At the same time, some data cannot be synchronized with the slave server in time, resulting in data inconsistency (manual intervention is required)

2. After the slave goes down and multiple slaves are restored, a large number of SYNC synchronizations will cause the master IO pressure to double (the startup time can be manually avoided)

3. Online expansion is complicated.

Summary:

The advantages of redis master-slave replication are mainly improved availability Disadvantages

2. Sentinel Sentinel Mode

Sentinel Sentinel Sentinel Introduction

Sentinel Sentinel is essentially a Redis instance running in a special mode, but the initialization process and work are different from ordinary Redis, and it is essentially a separate process.

Sentinel is a high-availability solution for Redis: a Sentinel system (system) composed of one or more Sentinel instances (instances) can monitor any number of master servers and all slave servers under these master servers. , and can automatically switch from the slave server to the master server when the master server goes offline.

1. Sentinel system

The following figure is a simple Sentinel system architecture diagram. A Sentinel system monitors a master-slave cluster, where server1 is the Redis master server and server2 /3/4 is the Redis slave server. The above master-slave replication is used between master and slave to achieve master-slave consistency. The Sentinel system monitors the entire master-slave cluster.

2. Sentinel failover

When the Sentinel system detects that the main server of Server1 is offline, it will terminate server2/3 /4 copy.

At the same time, Sentinel upgrades server2 to the main server, and server3/4 replicates from the new main server. At the same time, wait for server1 to come online again.

#The Sentinel system can also actively downgrade the main service to a slave server and upgrade the slave server to the master server.

2.1. Sentinel monitoring process

Sentinel monitoring cluster process:

  • Command Sentinel to return the redis server to running status by sending commands. Publish and subscribe When the status of the master server changes, Sentinel notifies other slave servers through the
  • publish and subscribe mode.

2.2. Sentinel failover

Sentinel failover:

  • #1. Sentinel in Sentinel system The instance sends a PING command to the cluster every 1s
  • 2. If the reply time of an instance in the cluster to the Sentinel instance exceeds down-after-milliseconds, then this instance will send the PING command. The Sentinel instance is subjectively down Line
  • 3. So when will it be objectively offline? Other instances in the Sentinel system need to confirm that the instance supervisor in the cluster is offline.
  • If the master server is marked as subjectively offline, the Sentinel process monitoring the master in the Sentinel system needs to confirm whether the master has entered the supervisor offline state once per second.
  • 4. If there are enough Sentinel instances (depending on the configuration) to confirm that the Master has entered the supervisory offline state, the Master will be marked as objectively offline.

3. Sentinel advantages and disadvantages

Advantages:

1. Sentinel mode is based on master-slave replication, so Sentinel has all the advantages of master-slave replication. 2. Sentinel has master-slave switching and failover, so the cluster has higher availability

Disadvantages:

1. Redis is difficult to support online expansion, and online expansion is more complicated.

Summary:
sentinel is mainly used to monitor the redis master-slave cluster and improve the availability of the redis master-slave cluster.

3. Cluster mode

redis cluster

1. reids cluster

Redis Cluster is a kind of server Sharding Technology, redis version 3.0 is officially available.
Sentinel has basically achieved high availability, but each machine stores the same content, which wastes memory, so Redis Cluster implements distributed storage. Different content is stored on each machine node.

#2. Redis Cluster data sharding principle

redis data sharding uses hash slot, redis cluster There are 16384 hash slots. Each Key is checked modulo 16384 after passing the CRC16 check to determine which slot to place.
When accessing the redis key, redis will obtain a result based on the CRC16 algorithm, and then calculate the remainder of the result and 16384, and use this value to obtain the data from the corresponding node.
At this time, the application client actually only needs to connect to any one of the nodes, and then each node in the Redis Cluster saves the slot information of other nodes. In this way, after the access key calculates the slot, the node information is obtained from the configuration by saving the slot information, and then the corresponding node is retrieved to obtain the data.

3. Redis Cluster replication principle

The redis-cluster cluster introduces the master-slave replication model. One master node corresponds to one or more slave nodes. When the master node When a node goes down, the slave node will be enabled. When other master nodes ping a master node A, if more than half of the master nodes communicate with A times out, then the master node A is considered to be down. If both master node A and its slave node A1 are down, then the cluster can no longer provide services

Recommended learning:Redis video tutorial

The above is the detailed content of Summary and sharing of three cluster modes of Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:脚本之家. If there is any infringement, please contact admin@php.cn delete
Redis: Beyond SQL - The NoSQL PerspectiveRedis: Beyond SQL - The NoSQL PerspectiveMay 08, 2025 am 12:25 AM

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis: A Comparison to Traditional Database ServersRedis: A Comparison to Traditional Database ServersMay 07, 2025 am 12:09 AM

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redis: Introduction to a Powerful In-Memory Data StoreRedis: Introduction to a Powerful In-Memory Data StoreMay 06, 2025 am 12:08 AM

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Is Redis Primarily a Database?Is Redis Primarily a Database?May 05, 2025 am 12:07 AM

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redis: Database, Server, or Something Else?Redis: Database, Server, or Something Else?May 04, 2025 am 12:08 AM

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redis: Unveiling Its Purpose and Key ApplicationsRedis: Unveiling Its Purpose and Key ApplicationsMay 03, 2025 am 12:11 AM

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

Redis: A Guide to Key-Value Data StoresRedis: A Guide to Key-Value Data StoresMay 02, 2025 am 12:10 AM

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis: Caching, Session Management, and MoreRedis: Caching, Session Management, and MoreMay 01, 2025 am 12:03 AM

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),