ApsaraDB for Redis provides the Sentinel-compatible mode to increase compatibility and reduce code modifications. After you enable this mode, clients can connect to an ApsaraDB for Redis instance in the same manner as they would connect to the native Redis Sentinel.

ApsaraDB for Redis uses the high-availability (HA) component developed by Alibaba Cloud instead of Redis Sentinel. For more information, see Features.

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 used in many business scenarios that involve 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.

CommandDescription
SENTINEL sentinelsQueries Sentinel instances for a specified master node and the status of these Sentinel instances. Command syntax:
SENTINEL sentinels <Master node name>
SENTINEL get-master-addr-by-nameQueries 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 Commands supported by ApsaraDB for Redis Community Edition.

Prerequisites

  • The ApsaraDB for Redis instance runs Redis 4.0, 5.0, or 6.0.
  • 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 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.

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 its 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.
    • 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 or a cluster instance in direct connection mode,

      set the no_loose_sentinel-enabled parameter to yes.

    For more information, see Modify the values of parameters for an 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 ApsaraDB for Redis instance:

If password-free access is enabled for the ApsaraDB for 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 an ApsaraDB for Redis instance in Sentinel-compatible mode without using passwords
    Note For more information about how to enable password-free access, see Enable password-free access.

    The following sample code shows 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. Example: 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 sample code shows how to connect to an ApsaraDB for Redis instance in Sentinel-compatible mode without using passwords:

        @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 by using passwords
    A Java client of the earliest version is used in this example. 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, visit What's New in Maven.

    The following sample code shows how to connect to an ApsaraDB for Redis instance by using passwords:

            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. Example: 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 as the database password.

    The following sample code shows how to connect to an ApsaraDB for Redis instance in Sentinel-compatible mode by using passwords:

            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 by using passwords section of this topic.