Redis stand-alone, master-slave, sentry, cluster four modes

insert image description here

Redis stand-alone mode

insert image description here

Redis stand-alone mode refers to the mode in which the Redis database runs as an independent, single process on a single server. In this mode, Redis does not involve data sharding or cluster configuration, and all data and operations are performed in one instance. The following is a detailed introduction to Redis stand-alone mode:

  1. Single instance: In Redis stand-alone mode, only one instance of Redis runs on a server. This instance is responsible for handling all data storage and manipulation requests.

  2. Memory database: Redis stand-alone mode is still a memory-based database, all data is stored in memory, so the read operation is very fast. This makes Redis stand-alone mode suitable for application scenarios that require high-speed reading, such as caching systems.

  3. Single-threaded model: Redis stand-alone mode uses a single-threaded model to process client requests. This is because most Redis operations are non-blocking, and the performance bottleneck of Redis usually lies in CPU rather than thread concurrency. This single-threaded model simplifies internal data structures and operations, improving performance and stability.

  4. Persistence: Redis stand-alone mode supports data persistence, that is, saving data to disk for recovery after restart. There are two main persistence methods: RDB (Redis Database Dump) and AOF (Append-Only File). RDB saves the snapshot of the database to the disk, and AOF appends the write operation to the file for data recovery.

  5. Data type: Redis stand-alone mode supports a variety of data types, such as strings, hash tables, lists, sets, ordered sets, etc. These data types enable Redis to be used for a variety of purposes such as caching, counters, leaderboards, real-time analytics, and more.

  6. Publish-subscribe: Redis stand-alone mode supports publish-subscribe mode, where one client can publish messages, while other clients can subscribe to interested message channels. This is very useful when building real-time messaging systems.

  7. Transaction: Redis stand-alone mode supports transactions, allowing multiple commands to be executed in one atomic operation, either all submitted or all rolled back.

  8. Application scenarios: Redis stand-alone mode is suitable for small-scale applications, or scenarios that do not require particularly high data availability. It can be used as cache, counter, real-time analysis, leaderboard, task queue, etc.

Although Redis stand-alone mode has advantages in simplicity and ease of use, performance and availability bottlenecks may occur when faced with high concurrency and large-scale data.

Redis master-slave mode

insert image description here

The Redis master-slave mode is a distributed architecture used to improve the availability, performance and data backup capabilities of the Redis database. In master-slave mode, there is a master node (master) and one or more slave nodes (slaves). The master node is responsible for processing write operations and some read operations, while the slave nodes are responsible for replicating the data of the master node and processing some read operations.

The following are the key features and working principles of the Redis master-slave mode:

  1. Data replication: The master node is responsible for write operations and some read operations, while the slave node realizes data backup and read sharing by replicating the data of the master node. The slave node will periodically obtain data snapshots (RDB) or incremental logs (AOF) from the master node to maintain data consistency.

  2. Read-write separation: the master node is responsible for write operations, while the slave nodes are responsible for read operations, thereby sharing the load of the master node. This improves the overall performance and load carrying capacity of the system.

  3. Data backup: By copying data to slave nodes, the master-slave mode realizes redundant backup of data. When the master node fails, one of the slave nodes can be upgraded to a new master node, thus ensuring the availability of the system.

  4. Failure recovery: When the master node fails, you can achieve fast failure recovery by upgrading a slave node to be the new master node. This process is called failover (failover).

  5. Data sharding: By using multiple slave nodes, the master-slave mode can also achieve data sharding, thereby improving the scalability and performance of the system.

  6. Configuration settings: In the master-slave mode, the slave node needs to configure the address and port of the master node for data replication. The master node does not automatically sense the presence of slave nodes.

  7. Data delay: Since the slave node needs to copy data from the master node, there may be a slight delay in the data of the slave node relative to the master node.

The master-slave mode is suitable for scenarios that need to improve the availability and performance of the Redis database, especially in applications that read more and write less. However, the master-slave model does not provide an absolute guarantee of high availability, because when the master node fails, the failover may take some time to complete. For higher-level high-availability and distributed requirements, you can consider using Redis cluster, sentinel mode or other distributed database solutions, which are the modes we will talk about below!

Redis sentinel mode

insert image description here

Redis Sentinel is a solution for monitoring and managing Redis clusters, designed to provide high availability and automatic failover. In Redis sentinel mode, a group of Redis sentinel processes are responsible for monitoring the status of Redis master nodes and slave nodes, and automatically failover when the master node fails.

The following are the key features and working principles of Redis sentinel mode:

  1. Monitor the master and slave nodes: The Redis sentinel process regularly monitors the status of the master and slave nodes. They determine the health of the node by sending heartbeats and checking the Redis server's response.

  2. Automatic failover: Sentinel automatically detects when a Redis master node fails and coordinates the promotion of an available slave node as the new master node for fast failover. This reduces system downtime.

  3. Configuration management: Redis sentry can monitor multiple Redis master-slave nodes. When the status of the node changes, the sentinel can automatically update the configuration to ensure that the client connects to the correct node.

  4. Provide service discovery: clients can connect to one or more Redis sentinel processes without directly connecting to specific Redis nodes. Sentry can provide clients with information about the currently available master and slave nodes.

  5. Multi-Sentinel Mode: Multiple Redis Sentinels can be configured for redundancy and high availability. These Sentinels cooperate with each other to jointly manage the Redis cluster.

  6. Voting decision: During the failover process, if multiple sentinels believe that a slave node should be promoted to the new master node, they will vote and reach a consensus to make a decision.

Redis sentinel mode is suitable for Redis deployments that require high availability, especially in terms of read-write separation and failover. It can effectively monitor and manage Redis clusters, thereby reducing system downtime and improving application stability.

However, it should be noted that Redis sentinel mode does not provide absolute high availability, because there may be brief service interruptions during failover. For higher-level high-availability and performance requirements, consider using the Redis cluster that will be covered below.

Redis cluster mode

insert image description here

Redis cluster mode is a distributed architecture for combining multiple Redis nodes into a logical cluster to provide high availability, scalability, and data distribution capabilities. In a Redis cluster, data will be distributed to multiple nodes, each node is responsible for managing part of the data, and can also automatically perform failover and data migration.

The following are the key features and working principles of Redis cluster mode:

  1. Data sharding: Redis cluster will divide data into multiple data shards, and each data shard will be stored on different nodes. This can effectively distribute data to multiple nodes, improving the scalability and performance of the system.

  2. High availability: Redis cluster is designed with high availability, each data shard has multiple copies, ensuring that even if a node fails, the data is still available. The cluster supports automatic failover, which can upgrade a slave node to a new master node, thus ensuring the availability of the system.

  3. Distributed fault detection: Redis cluster uses multiple nodes for fault detection. When a node fails, other nodes can detect it and coordinate the failover.

  4. Inter-node communication: Nodes in the Redis cluster communicate through a binary protocol to achieve data synchronization, fault detection, and node status maintenance.

  5. Automatic data migration: When adding or deleting nodes, the Redis cluster can automatically perform data migration to ensure that data is evenly distributed among different nodes.

  6. Data consistency: Redis cluster distributes data by using hash slots to ensure that data with the same key will be stored on the same node, thereby ensuring data consistency.

  7. Read-write separation: Redis cluster supports client-side read-write separation, that is, read operations can read data from any node among multiple nodes, while write operations will be sent to the master node.

  8. Node expansion: When it is necessary to expand the capacity of the Redis cluster, it can be achieved by adding new nodes. Redis cluster will automatically migrate part of the data to new nodes to achieve capacity expansion.

Redis cluster mode is suitable for scenarios that require high availability, high performance, and distributed capabilities, especially when faced with large-scale data and high concurrent requests. It can effectively manage and distribute data, providing a higher level of availability and scalability, but also requires some additional configuration and management work, especially cluster configuration, which is a laborious place, how to allocate resources, Even expansion and contraction, involving elastic architecture, is a relatively advanced technology. Of course, there is also a docker deployment method, you can try it, it is very fun and interesting! ! !

The difference between the four modes

We introduced the stand-alone mode, master-slave mode, sentinel mode and cluster mode of Redis respectively. Below I will briefly compare these 4 modes to better understand the differences between them.

  1. Standalone mode:

    • Overview: Redis stand-alone mode is the simplest deployment mode, with only one Redis instance running on a single server.
    • Advantages: Simple and easy to use, suitable for small-scale applications or development environments. Can be used as cache, temporary storage, etc.
    • Disadvantages: It does not have high availability, and if a single point of failure occurs, data will be unavailable. Not suitable for high concurrency and large-scale applications.
  2. Master-slave mode:

    • Overview: Redis master-slave mode consists of master nodes and slave nodes. The slave nodes replicate the data of the master node to improve availability and read-write separation.
    • Advantages: Provides a certain degree of high availability and read-write separation. It can cope with the failure of the master node and realize fast failover.
    • Disadvantage: When the master node fails, it is necessary to manually upgrade the slave node to the new master node, and there is a certain amount of downtime. Not suitable for large-scale writing and data distribution scenarios.
  3. Sentinel mode:

    • Overview: Redis sentinel mode monitors the status of master and slave nodes through a group of sentinel processes, and automatically performs failover.
    • Advantages: Provides automated high availability with automatic detection of master node failure and fast failover. Suitable for scenarios that require high availability.
    • Disadvantage: Failover may cause brief service interruption. Configuration and management are relatively complex.
  4. Cluster mode:

    • Overview: Redis cluster mode achieves data sharding and high availability by clustering multiple nodes.
    • Advantages: Provides high availability, scalability, and data distribution capabilities. Automatic data sharding and failover, suitable for large-scale and high-concurrency scenarios.
    • Disadvantages: The configuration and management are relatively complicated, and multiple nodes need to be maintained.

In general, different Redis deployment modes are suitable for different needs and scenarios. If you need a simple data storage or development and testing environment, you can consider the stand-alone mode. If you need certain high availability and read-write separation, you can choose the master-slave mode. Sentinel mode can be used if a higher level of automated high availability and failover is required. And if you need high availability, scalability, and data distribution capabilities, you can choose cluster mode. When choosing a mode, make trade-offs and make decisions based on your application needs, availability requirements, and performance requirements.

And in the previous article, because it involves docker cluster deployment, I have published a detailed article about deploying Redis cluster. You can read it if you are interested!
Docker deploys Redis cluster nodes.
According to my short project experience, I haven’t encountered a large-scale system yet, so I haven’t used the cluster mode. It’s a stand-alone version, which implements token authentication or user personal data storage. There is no such advanced requirement. , However, it doesn't mean that you don't need it in the future. Everyone hopes to learn more and practice more. Opportunities are always reserved for those who are prepared!

respective application scenarios

Different Redis deployment modes are suitable for different application scenarios. Here are suggestions for different scenarios:

  1. Standalone mode:
    • Applicable scenarios: development and test environments, small-scale applications, temporary data storage, cached data, temporary counters, etc.
  2. Master-slave mode:
    • Applicable scenarios: applications with more reads and fewer writes, improved read performance, and redundant backup of some data.
  3. Sentinel mode:
    • Applicable scenarios: Scenarios that require automated high availability and failover, and require high data availability.
  4. Cluster mode:
    • Applicable scenarios: large-scale applications that require high availability, high performance, and data distribution, and large-scale write and read scenarios.

Taken together, choosing the right Redis deployment model depends on your application needs and priorities. If you need simple caching or temporary storage, you can choose stand-alone mode. If you need read-write separation and a certain degree of high availability, you can choose the master-slave mode. If you have high requirements for high availability and do not want to manually manage failover, you can choose Sentinel mode. And if you need high availability, scalability, and data distribution, you can choose cluster mode. No matter which model you choose, you need to make trade-offs and decisions based on actual needs.

It's the beginning of autumn, and the summer of 2023 has become yesterday, bless us, bless us each other, the sky is cool and autumn, everything will be successful! come on! ! !

Guess you like

Origin blog.csdn.net/weixin_53742691/article/details/132174609