ApsaraDB for Redis provides the Sentinel-compatible mode. After you enable this mode, clients can connect to ApsaraDB for Redis instances in the same manner as they would connect to the native Redis Sentinel.

Prerequisites

  • The engine version of your ApsaraDB for Redis instance is 4.0 or 5.0.
  • The ApsaraDB for Redis instance is deployed in a virtual private cloud (VPC).
    Note If the instance is deployed in the classic network, switch the network type to VPC. For more information about how to switch the network type, see Change 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.

Overview of Redis Sentinel

Redis Sentinel provides Redis with features such as master and replica monitoring, fault alerting, and automatic failover. Redis Sentinel is suitable for many business scenarios that use on-premises 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.

Note ApsaraDB for Redis uses the high-availability (HA) component developed by Alibaba Cloud without the need to use Redis Sentinel. For more information, see Feature

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 for a specified master node and their statuses. Command syntax:
SENTINEL sentinels <Master node name>
SENTINEL get-master-addr-by-name Queries the IP address and port number of the specified master node. Command syntax:
SENTINEL get-master-addr-by-name <Master node name>
Note For more information about Sentinel commands that are supported by different engine versions, see Sentinel command group.

Enable the Sentinel-compatible mode

  1. Log on to the ApsaraDB for Redis console.
  2. In the top navigation bar of the page, select the region in which the instance is deployed.
  3. On the Instances page, click the ID of the instance.
  4. In the left-side navigation pane of the instance details page, click System Parameters.
  5. On the System Parameters page, find the #no_loose_sentinel-enabled parameter and click Modify in the Actions column.
  6. In the dialog box that appears, set the parameter to yes, and click OK.
    Note A value of no indicates that the Sentinel-compatible mode is disabled. This is the default value. A value of yes indicates that the Sentinel-compatible mode is enabled. For more information about the supported parameters, see Supported parameters.

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 ApsaraDB for Redis instance:

If password-free access is enabled for the ApsaraDB for Redis instance, you can connect to the instance by using the Sentinel-compatible mode. Otherwise, you must configure authentication information when you connect to the instance.

  • Connect to an ApsaraDB for Redis instance in Sentinel-compatible mode without a password
    Note For more information about how to enable password-free access, see Enable password-free access.

    The following code provides an example on how to connect to an ApsaraDB for Redis instance by using Spring Data Redis:

        @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;
        }

    The following section describes the parameters:

    • master-name: the name of the master node. You can specify a custom name, such as testmaster.
    • sentinel-host: the VPC endpoint that is used to connect to the ApsaraDB for Redis instance.
    • sentinel-port: the port number that is used to connect to the ApsaraDB for Redis instance. The default port number is 6379.

    The following code provides an example on how to connect to an ApsaraDB for Redis instance in Sentinel-compatible mode without a password:

        @Bean
        public JedisConnectionFactory connectionFactory() {
            RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
                    .master("any-name")
                    .sentinel("r-********.redis.rds.aliyuncs.com", 6379);
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            ...
            JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
            return connectionFactory;
        }
  • Connect to an ApsaraDB for Redis instance in Sentinel-compatible mode with a password
    A Java client of the earliest version is used in this example. The following requirements must be met for clients:
    • Jedis 3.6.0 or later must be used.
    • Lettuce 5.3.0.RELEASE or later must be used.
    • Spring Data Redis 2.5.1 or later must be used. The spring.redis.sentinel.password parameter must be 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 code provides an example on how to connect to an ApsaraDB for Redis instance 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);

    The following section describes the parameters:

    • masterName: the name of the master node. You can specify a custom name, such as testmaster.
    • sentinels.add: the VPC endpoint and port number that are used to connect to the ApsaraDB for Redis instance. Format: r-********.redis.rds.aliyuncs.com:6379.
    • dbPassword/sentinelPassword: the password of the account that is used to connect to the ApsaraDB for Redis instance. The password format varies based on the account that you select. If you forget your password, you can reset it. For more information about how to reset a password, see Change or reset the password.
      Note
      • If you use the default account whose username is the same as the instance ID, you can enter only the password.
      • If you use a custom account, the format of the password must be <user>:<password>. For example, if the username of the custom account is testaccount and the password is Rp829dlwa, you must enter testaccount:Rp829dlwa.

    The following code provides an example on how to connect to an ApsaraDB for 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);

FAQ

  • Q: 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 ApsaraDB for Redis?

    A: You can upgrade your client, modify some code to add a password for Sentinel authentication, and then try again. For more information, see the Connect to an ApsaraDB for Redis instance in Sentinel-compatible mode with a password section of this topic.