All Products
Search
Document Center

ApsaraDB for Redis:Use the Sentinel-compatible mode to connect to an ApsaraDB for Redis instance

Last Updated:Apr 08, 2024

ApsaraDB for Redis uses the high-availability (HA) component developed by Alibaba Cloud instead of Redis Sentinel. To improve the compatibility of ApsaraDB for Redis with open source Redis and minimize code modifications, ApsaraDB for Redis provides the Sentinel-compatible mode. After you enable this mode, clients can connect to a Tair instance in the same manner as they connect to Redis Sentinel.

Overview of Redis Sentinel

Redis Sentinel provides open source Redis with features such as master and replica monitoring, fault alerting, and automatic failover. Redis Sentinel is suitable for many business scenarios that use self-managed Redis databases and require high reliability. To facilitate the migration of Redis databases to the cloud in such scenarios, Alibaba Cloud provides the Sentinel-compatible mode. After you enable the Sentinel-compatible mode, you can run the Sentinel commands described in the following table.

Command

Description

SENTINEL sentinels

Queries Sentinel instances of a master and the status of the Sentinel instances. Command syntax:

SENTINEL sentinels <Name of a master>

SENTINEL get-master-addr-by-name

Queries the IP address and port number of a master. Command syntax:

SENTINEL get-master-addr-by-name <Name of a master>

For more information about Sentinel commands that are supported by different engine versions, see Commands supported by ApsaraDB for Redis Community Edition.

Prerequisites

  • The engine version of the ApsaraDB for Redis instance is Redis 4.0 or later.

  • The ApsaraDB for Redis instance is deployed in a virtual private cloud (VPC).

    Note

    If the ApsaraDB for Redis instance runs in the classic network, switch the network type to VPC. For more information, see Switch the network type from classic network to VPC.

  • The internal IP address of your Elastic Compute Service (ECS) instance or the public IP address of your on-premises host is added to an IP address whitelist of the ApsaraDB for Redis instance. For more information, see Configure whitelists.

Procedure

  1. Log on to the ApsaraDB for Redis console and go to the Instances page. In the top navigation bar, select the region in which the instance that you want to manage resides. Then, find the instance and click the instance ID.

  2. In the left-side navigation pane of the Instance Information page, click System Parameters.

  3. Enable the Sentinel-compatible mode for the instance by modifying the corresponding parameter based on the instance architecture. For more information, see Configure instance parameters.

    • If the instance is a read/write splitting instance or a cluster instance in proxy mode, set the sentinel_compat_enable parameter to 1.

    • If the instance is a standard instance, set the #no_loose_sentinel-enabled parameter to yes.

    Note
    • You can view the instance architecture on the instance details page.

    • Cluster instances in direct connection mode use open source Redis Cluster for load balancing, eliminating the need for the Sentinel component. Additionally, Sentinel parameters cannot be configured in this mode.

    After you enable the Sentinel-compatible mode for the instance, you can connect to the instance and run the SENTINEL sentinels test command. If the command is successful, the Redis Sentinel-compatible mode is enabled for the instance. The Sentinel-compatible mode does not provide additional endpoints. You can use the original endpoint, such as r-********.redis.rds.aliyuncs.com:6379 to connect to the instance.

Connect to an ApsaraDB for Redis instance in Sentinel-compatible mode

After the Sentinel-compatible mode is enabled, you can use one of the following methods to connect to the Redis instance: If password-free access is enabled for the Redis instance, you can connect to the instance in Sentinel-compatible mode. Otherwise, you must configure authentication information when you connect to the instance.

  • Connect to a Tair instance in Sentinel-compatible mode without using a password

    Note

    For more information about how to enable password-free access, see Enable password-free access.

    The following sample code shows how to use Spring Data Redis to connect to a Redis instance in Sentinel-compatible mode:

        @Bean
        public JedisConnectionFactory connectionFactory() {
            RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
                    .master("testmaster")
                    .sentinel("r-********.redis.rds.aliyuncs.com", 6379);
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            ...
            JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
            return connectionFactory;
        }

    Parameter description:

    • master: the name of the master. You can use the default value, such as testmaster.

    • sentinel: the virtual private cloud (VPC) endpoint and port number of the Redis instance. Separate the endpoint and port number with a comma (,). Example: "r-********.redis.rds.aliyuncs.com", 6379.

    The following sample code shows how to use Spring Data Redis to connect to a Redis data store monitored by Redis Sentinel:

        @Bean
        public JedisConnectionFactory connectionFactory() {
            RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
                    .master("original-master-name")
                    .sentinel(original-sentinel-1-host, original-sentinel-1-port)
                    .sentinel(original-sentinel-2-host, original-sentinel-2-port);
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            ...
            JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
            return connectionFactory;
        }
  • Connect to a Tair instance in Sentinel-compatible mode by using a password

    In this example, a Java client of the earliest version is used. The client must meet the following requirements:

    • Jedis 3.6.0 or later is used.

    • Lettuce 5.3.0.RELEASE or later is used.

    • Spring Data Redis 2.5.1 or later is used. The spring.redis.sentinel.password parameter is specified for the Spring Data Redis client.

    Note

    We recommend that you upgrade the clients to the latest stable versions. For more information about the latest versions, see What's New in Maven.

    The following sample code shows how to connect to a Redis instance in Sentinel-compatible mode by using a password:

            String masterName = "any-name";
            Set<String> sentinels = new HashSet<>();
            sentinels.add("r-********.redis.rds.aliyuncs.com:6379");
            GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
            String dbPassword = "testaccount:Rp829dlwa";
            String sentinelPassword = "testaccount:Rp829dlwa";
    
            JedisSentinelPool jedisSentinelPool =
                    new JedisSentinelPool(masterName, sentinels, poolConfig,
                            2000, 2000, dbPassword,
                            0, null, 2000, 2000,
                            sentinelPassword, null);

    Parameter description:

    • masterName: the name of the master. You can use the default value, such as testmaster.

    • sentinels.add: the VPC endpoint and port number of the Redis instance. Example: r-********.redis.rds.aliyuncs.com:6379.

    • dbPassword and sentinelPassword: the account password of the Redis instance. The password format varies based on the account that you select. If you forget your password, reset it. For more information, see Change or reset the password.

      Note
      • If you use the default account whose username is the same as the instance ID, directly enter the password.

      • If you use a custom account, enter the password in the <user>:<password> format. A password in this format can also be used for default account logon. For example, if the username of the custom account is testaccount and the password is Rp829dlwa, enter testaccount:Rp829dlwa as the password.

    The following sample code shows how to connect to a Redis data store monitored by Redis Sentinel by using a password:

            String masterName = "original-master-name";
            Set<String> sentinels = new HashSet<>();
            sentinels.add("original-sentinel-1-host:original-sentinel-1-port");
            sentinels.add("original-sentinel-2-host:original-sentinel-2-port");
            GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
            String dbPassword = "original-db-password";
            String sentinelPassword = "original-sentinel-password";
    
            JedisSentinelPool jedisSentinelPool =
                    new JedisSentinelPool(masterName, sentinels, poolConfig,
                            2000, 2000, dbPassword,
                            0, null, 2000, 2000,
                            sentinelPassword, null);

FAQ

  • What do I do if the NOAUTH Authentication required error message appears when I switch from the native Redis Sentinel mode to the Sentinel-compatible mode provided by Tair?

    You can upgrade your client, modify the code to add a password for Sentinel authentication, and then try again. For more information, see the "Connect to a Tair instance in Sentinel-compatible mode by using a password" section of this topic.

  • Do Redis cost-efficient instances support the Sentinel-compatible mode?

    Yes, Redis cost-efficient instances support the Sentinel-compatible mode.