Table of Contents
1. Preface
1. Goals and gains
2. Port planning
2. Single-machine simulation
(1) Service planning
1. Redis instance
2. Sentinel Service
(2) Service configuration
2. Sentinel service
(3) Service management
3. Client integration
(1) Basic integration
1. Global configuration file
2. Integrated configuration
(2) Read-write separation
Home Database Redis Example analysis of high availability in Redis sentry mode

Example analysis of high availability in Redis sentry mode

Jun 02, 2023 pm 10:38 PM
redis

    1. Preface

    Redis high availability has two modes: Sentinel mode and Cluster mode, this article builds one master, two slaves and three sentinelsRedis high availability service based on the sentinel mode.

    1. Goals and gains

    One master, two slaves and three sentinelsRedis service can basically meet the high availability requirements of small and medium-sized projects. Use Supervisor to monitor and manage Redis instances. . Through this article, the following goals will be achieved:

    • Sentinel mode service planning and construction

    Sentinel mode service is compared to a single machine The version service is more reliable and suitable for scenarios where reading and writing are separated, the amount of data is not large, and reliability and stability are required.

    • Client integration and read-write separation

    Connect to the sentinel mode through the Spring framework to complete the production environment Common operations.

    2. Port planning

    Port planning is the first step to complete this solution.

    Example analysis of high availability in Redis sentry mode

    2. Single-machine simulation

    Single-machine simulation is to simulate operations on a physical machine or virtual machine to restore the intermediate process of the original solution as much as possible. Usually Used during the learning or development phase.

    In order to simplify the operation, the Redis service makes the following conventions: data is not persisted to disk; service instances run as foreground processes; node configuration files use the default configuration file as a template; there is no password verification.

    (1) Service planning

    1. Redis instance

    When the service is started for the first time, it clearly knows which node is the master node. When the service is running for a long time and When a master-slave switch occurs, it is not possible to display which node is the master node and needs to be queried indirectly through the command line.

    Node Host Port Role Additional configuration
    node01 127.0.0.1 6380 As the master service when started for the first time
    node02 127.0.0.1 6381 As a slave service when started for the first time replicaof 127.0.0.1 6380
    node03 127.0.0.1 6382 As a slave service when started for the first time replicaof 127.0. 0.1 6380

    Additional configuration refers to the new configuration in the node configuration file when the Redis service instance is started for the first time.

    2. Sentinel Service

    There is no master-slave distinction between Sentinel service nodes, and all nodes are on an equal footing. When an exception occurs in the main service, the sentinel service will trigger the voting strategy and select candidates from the slave nodes of the Redis instance as the new main service.

    Node Host Port Additional configuration
    node01 127.0.0.1 26380 sentinel monitor mymaster 127.0.0.1 6380 2
    node02 127.0.0.1 26381 sentinel monitor mymaster 127.0.0.1 6380 2
    node03 127.0.0.1 26382 sentinel monitor mymaster 127.0.0.1 6380 2

    (2) Service configuration

    1. Redis instance

    The initial configuration file of the node uses the default configuration file as a template.

    After node01 and node02 initialize the configuration files, the master-slave relationship between nodes is displayed and the following configuration is added:

    replicaof 127.0.0.1 6380
    Copy after login
    2. Sentinel service

    The initial configuration file of the node is as follows: The default configuration file is a template.

    After node01, node02, and node03 initialize the configuration files, add the following configuration:

    sentinel monitor mymaster 127.0.0.1 6381 2
    Copy after login

    (3) Service management

    When testing or learning, it is recommended to use the foreground process to manage services. It is convenient to simulate a single point of failure, view logs and observe master-slave switching.

    Under production conditions, it is recommended to use Supervisor to manage the service, which is not only easy to manage but also can automatically restart the service after abnormal termination. Three physical machines are used in high availability scenarios.

    1. Redis instance
    /usr/local/redis/bin/redis-server /usr/local/redis/conf/ms/redis80.conf --port 6380 --save '' --daemonize no 
    /usr/local/redis/bin/redis-server /usr/local/redis/conf/ms/redis81.conf --port 6381 --save '' --daemonize no
    /usr/local/redis/bin/redis-server /usr/local/redis/conf/ms/redis82.conf --port 6382 --save '' --daemonize no
    Copy after login
    2. Sentinel service
    /usr/local/redis/bin/redis-sentinel /usr/local/redis/conf/ms/sentinel280.conf --port 26380 --daemonize no
    /usr/local/redis/bin/redis-sentinel /usr/local/redis/conf/ms/sentinel281.conf --port 26381 --daemonize no
    /usr/local/redis/bin/redis-sentinel /usr/local/redis/conf/ms/sentinel282.conf --port 26382 --daemonize no
    Copy after login

    3. Client integration

    Client implementation refers to the integration based on SpringBoot. Two-step implementation: first, complete the integration as the basis; second, add new features based on production needs.

    (1) Basic integration

    The content of basic integration is to use Java client to connect to the high-availability sentinel mode Redis service to achieve the requirements for normal operation of single-node failure services.

    1. Global configuration file

    The configuration information added to the global configuration file is: masterThe parameter is the sentinel service name, here is the default value;nodesThe parameter is the sentinel service list (not the Redis instance service list); databaseThe parameter is the database.

    spring:
      redis:
        database: 0
        sentinel:
          nodes: 192.168.181.171:26380,192.168.181.171:26381,192.168.181.171:26382
          master: mymaster
    Copy after login
    2. Integrated configuration

    is integrated into the SpringBoot system. The core is to create a LettuceConnectionFactory connection factory. Through the Redis connection factory, it can be smoothly inherited into other parts of the Spring system. frame.

    @Configuration
    public class RedisSentinelConfig {
        @Autowired
        private RedisProperties redisProperties;
        
        @Bean
        public RedisConnectionFactory lettuceConnectionFactory() {
            RedisProperties.Sentinel sentinel = redisProperties.getSentinel();
            HashSet<String> nodes = new HashSet<>(sentinel.getNodes());
            String master = sentinel.getMaster();
            RedisSentinelConfiguration config = new RedisSentinelConfiguration(master, nodes);
            config.setDatabase(redisProperties.getDatabase());
            return new LettuceConnectionFactory(config);
        }
    }
    Copy after login

    (2) Read-write separation

    Basic integration only implements the process of high-availability Redis service. In the production environment, other configurations still need to be added: modify the custom connection database serial number; authorize the connection ;Connection pool configuration; read-write separation.

    Under the premise of high availability, the feature of separation of reading and writing is derived. The main library completes the write request; the slave library completes the read request (the slave library does not allow writing).

    @Bean
    public LettuceClientConfigurationBuilderCustomizer lettuceClientCustomizer() {
        // 配置读写分离
        return builder -> builder.readFrom(ReadFrom.REPLICA);
    }
    Copy after login

    The above is the detailed content of Example analysis of high availability in Redis sentry mode. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    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

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

    Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

    How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

    How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

    How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

    Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

    How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

    To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

    How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

    Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

    How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

    The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

    How to make message middleware for redis How to make message middleware for redis Apr 10, 2025 pm 07:51 PM

    Redis, as a message middleware, supports production-consumption models, can persist messages and ensure reliable delivery. Using Redis as the message middleware enables low latency, reliable and scalable messaging.

    How to start the server with redis How to start the server with redis Apr 10, 2025 pm 08:12 PM

    The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

    See all articles