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

Prerequisites

  • The engine version of your ApsaraDB for Redis instance is Redis 4.0or 5.0.
  • The ApsaraDB for Redis instance runs in a virtual private cloud (VPC).
    Note If the instance is deployed in the classic network, you can change the network type to VPC. For more information, see Change the network type from classic network to VPC.
  • The private IP address of your Elastic Compute Service (ECS) instance or the public IP address of your on-premises host is added to a whitelist of the ApsaraDB for Redis instance. For more information, see Configure whitelists.

Overview of Redis Sentinel

Redis Sentinel provides Redis with services such as master and replica monitoring, fault alerting, and automatic failover. Redis Sentinel is used in 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 component developed by Alibaba Cloud, which does not require Redis Sentinel.

After you enable the Sentinel-compatible mode, you can use the commands that are described in the following table.

Command Description
SENTINEL sentinels Queries Sentinel instances for a specified master and the status of these Sentinel instances. Syntax:
SENTINEL sentinels <master name>
SENTINEL get-master-addr-by-name Queries the IP address and port number of a specified master. Syntax:
SENTINEL get-master-addr-by-name <master name>

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 where the instance is deployed.
  3. On the Instances page, click the ID of the instance.
  4. On the Instance Information page, click System Parameters in the left-side navigation pane.
  5. On the System Parameters page, find the cluster_compat_enable parameter and click Modify in the Actions column.
  6. In the dialog box that appears, set the parameter to 1 and click OK.
    Note A value of 0 indicates that the Sentinel-compatible mode is disabled. This is the default value. A value of 1 indicates that the Sentinel-compatible mode is enabled. For more information about parameters, see Supported parameters.

Example: Create a password-free connection in Sentinel-compatible mode

Note To use the Sentinel-compatible mode to connect to an ApsaraDB for Redis instance without a password, you must set the network type to VPC and enable password-free access. For more information, see Enable password-free access for a VPC.

In this example, spring-data-redis is used, as shown in the following open source code:

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

Parameters:

  • master-name: the name of the master. You can specify a custom name.
  • sentinel-host: the endpoint in the VPC for the ApsaraDB for Redis instance.
  • sentinel-port: the port number of the ApsaraDB for Redis instance. The default port number is 6379.

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

Example: Create a connection that requires a password in Sentinel-compatible mode

In this example, Jedis is used, as shown in the following open source code:

        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);

Parameters:

  • masterName: the name of the master. You can specify a custom name.
  • sentinels.add: the endpoint and port number in the VPC for the ApsaraDB for Redis instance, in the format of r-********.redis.rds.aliyuncs.com:6379.
  • dbPassword/sentinelPassword: the password of the ApsaraDB for Redis instance. The password format varies based on the selected account. If you forget your password, you can reset it. For more information, see Change or reset the password.
    Note
    • If you are using the default account, which is named after the instance ID, enter only the password.
    • If you are using a custom account, enter a password in the format of <user>:<password>. For example, if the username of a 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 ApsaraDB for Redis 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);