Tair (Redis OSS-compatible) uses the high-availability (HA) component developed by Alibaba Cloud instead of Redis Sentinel. For more information, see Functions and features. To improve instance compatibility and reduce code changes, Tair (Redis OSS-compatible) provides the Sentinel-compatible mode. After you enable the mode, you can connect to a Tair or Redis Open-Source Edition instance in the same way as you 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 business scenarios in which self-managed Redis databases are used and high reliability is required. 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 the Sentinel commands that are supported by different engine versions, see Commands supported by Redis Open-Source Edition.
Prerequisites
The engine version of the instance is Redis 4.0 or later.
The instance is deployed in a virtual private cloud (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 instance. For more information, see Configure whitelists.
Procedure
Log on to the 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.
In the left-side navigation pane of the Instance Information page, click Parameter Settings.
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 execution 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 instance in Sentinel-compatible mode
After you enable the Sentinel-compatible mode, you can use one of the following methods to connect to the instance: If you enable password-free access for the 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 instance in Sentinel-compatible mode without using a password
The following sample code shows how to connect to an instance in Sentinel-compatible mode without using a password.
Spring Data Redis
In this example, Spring Data Redis 2.4.2 is used.
@Bean
public JedisConnectionFactory connectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("testmaster")
.sentinel("r-bp10noxlhcoim2****.redis.rds.aliyuncs.com", 6379);
JedisPoolConfig poolConfig = new JedisPoolConfig();
...
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
return connectionFactory;
}
Parameters:
master: the name of the master. You can use the default value, such as testmaster
.
sentinel: the VPC endpoint and port number of the instance. Separate the endpoint and port number with a comma (,). Example: "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com", 6379
.
redis-py
In this example, Python 3.9 and redis-py 4.3.6 are used.
from redis.sentinel import Sentinel
SENTINEL_HOST = "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com"
SENTINEL_PORT = 6379
SENTINEL_MASTER_NAME = "testmaster"
sentinel = Sentinel([(SENTINEL_HOST, SENTINEL_PORT)])
r = sentinel.master_for(SENTINEL_MASTER_NAME, db=0)
r.set('foo', 'bar')
print(r.get('foo'))
Parameters:
SENTINEL_HOST and SENTINEL_PORT: the VPC endpoint and port number of the instance.
SENTINEL_MASTER_NAME: the name of the master. You can use the default value, such as testmaster
.
Connect to an instance in Sentinel-compatible mode by using a password
The following sample code shows how to connect to an instance in Sentinel-compatible mode by using a password.
Java
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 update the clients to the latest stable versions. For more information about the latest versions, see What's New in Maven.
String masterName = "any-name";
Set<String> sentinels = new HashSet<>();
sentinels.add("r-bp10noxlhcoim2****.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);
Parameters:
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 instance. Example: r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379
.
dbPassword and sentinelPassword: the account password of the instance. The password format varies based on the account that you select: If you forget your password, you can 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 a custom account is testaccount
and the password is Rp829dlwa
, enter testaccount:Rp829dlwa
as the password.
redis-py
In this example, Python 3.9 and redis-py 4.3.6 are used.
from redis.sentinel import Sentinel
SENTINEL_HOST = "r-bp10noxlhcoim2****.rds.aliyuncs.com"
SENTINEL_PORT = 6379
SENTINEL_MASTER_NAME = "redis_master" # Note: This name cannot be changed.
SENTINEL_REDIS_PWD = "testaccount:Rp829dlwa"
conf = {
'password': SENTINEL_REDIS_PWD,
}
sentinel = Sentinel([(SENTINEL_HOST, SENTINEL_PORT)], sentinel_kwargs=conf)
r = sentinel.master_for(SENTINEL_MASTER_NAME, db=0, **conf)
r.set('foo', 'bar')
print(r.get('foo'))
Parameters:
SENTINEL_HOST and SENTINEL_PORT: the VPC endpoint and port number of the instance.
SENTINEL_MASTER_NAME: the name of the master in the Redis Sentinel configuration. The name is set to redis_master
and cannot be changed.
SENTINEL_REDIS_PWD: the account password of the instance.
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?
You can set the #no_loose_sentinel-password-free-access parameter to yes to connect to the instance in Sentinel-compatible mode over a VPC without enabling password-free access.
If the engine version of the instance is earlier than Redis 6.0, update your client, modify the code to add a password for Sentinel authentication, and then try again. For more information, see Connect to an instance in Sentinel-compatible mode by using a password.