Tair (Redis OSS-compatible) uses a proprietary high availability (HA) component and does not rely on Sentinel. However, to improve compatibility and reduce the need for code changes, Tair (Redis OSS-compatible) provides a Sentinel-compatible mode. After you enable this feature, you can connect to Tair and Redis Open-Source Edition instances the same way you connect to a self-managed Redis Sentinel deployment.
Redis Sentinel
Redis Sentinel provides features for open-source Redis, such as master and replica node monitoring, failure notifications, and automatic failover. Many high-reliability applications that use a self-managed Redis database rely on Sentinel. To simplify migrating these databases to the cloud, Alibaba Cloud developed the Sentinel-compatible mode. After you enable this mode, you can use the following Sentinel commands. In these commands, the master name must be set to redis_master.
Command | Description |
SENTINEL sentinels | Queries the list of Sentinel instances for the specified master and their status. Usage: SENTINEL sentinels redis_master
|
SENTINEL get-master-addr-by-name | Queries the IP address and port number of the specified master. Usage: SENTINEL get-master-addr-by-name redis_master
|
For more information about Sentinel command support in different versions, see Redis Open-Source Edition command support.
Prerequisites
The Sentinel-compatible mode is enabled.
The IP address of your client, such as the internal IP address of an ECS instance or the public IP address of an on-premises host, is in the instance's IP whitelist.
Connection examples
After you enable the Sentinel-compatible mode, you can connect to the instance with or without a password. A password is not required if password-free access is enabled for the instance within a VPC. Otherwise, you must provide authentication credentials.
Password-free connection
The following code shows how to connect to an instance by using the Sentinel-compatible mode without a password.
Spring Data Redis
This example uses Spring Data Redis 2.4.2.
@Bean
public JedisConnectionFactory connectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("redis_master")
.sentinel("r-bp10noxlhcoim2****.redis.rds.aliyuncs.com", 6379);
JedisPoolConfig poolConfig = new JedisPoolConfig();
...
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
return connectionFactory;
}
Parameters:
master: The master name. This must be set to redis_master.
sentinel: The VPC endpoint and port number of the instance, separated by a comma (,), such as "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com", 6379.
redis-py
This example uses Python 3.9 and redis-py 4.3.6.
from redis.sentinel import Sentinel
SENTINEL_HOST = "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com"
SENTINEL_PORT = 6379
SENTINEL_MASTER_NAME = "redis_master"
sentinel = Sentinel([(SENTINEL_HOST, SENTINEL_PORT)])
r = sentinel.master_for(SENTINEL_MASTER_NAME, db=0)
r.set('foo', 'bar')
print(r.get('foo'))
Parameters:
Password-based connection
The following code shows how to connect to an instance by using the Sentinel-compatible mode with a password.
Java
This example uses the minimum required client versions. Your clients must meet the following version requirements:
Note We strongly recommend that you upgrade your client to the latest stable version. To find the latest version, search the MVN Repository.
Jedis
String masterName = "redis_master";
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);
Lettuce
String masterName = "redis_master";
String sentinelNodes = "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379";
String sentinelPassword = "testaccount:Rp829dlwa";
RedisURI redisURI = RedisURI.create("redis-sentinel://" + sentinelPassword + "@" + sentinelNodes + "#" + masterName);
redisURI.getSentinels().forEach(it -> it.setPassword(sentinelPassword));
RedisClient client = RedisClient.create(redisURI);
RedisCommands<String, String> sync = client.connect().sync();
System.out.println(sync.set("key", "value"));
System.out.println(sync.get("key"));
client.close();
Parameters:
masterName: The master name. This must be set to redis_master.
sentinels.add/sentinelNodes: The VPC endpoint and port number of the instance, in the format of r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379.
dbPassword and sentinelPassword: The password for the instance account. If you forget the password, you can reset it. For more information, see Change or reset the password.
Note For a default account, where the username is the same as the instance ID, enter only the password.
For a newly created account, use the <user>:<password> format. This format is also supported for the default account. For example, if the username is testaccount and the password is Rp829dlwa, enter testaccount:Rp829dlwa.
redis-py
This example uses Python 3.9 and redis-py 4.3.6.
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 master name. This must be set to redis_master.
SENTINEL_REDIS_PWD: The password for the instance account.
FAQ
Q: What do I do if I receive a NOAUTH Authentication required error after I switch from a self-managed Redis Sentinel deployment to the Sentinel-compatible mode?
A: You can enable the #no_loose_sentinel-password-free-access parameter by setting it to yes. This allows password-free Sentinel connections over a VPC endpoint without enabling the global VPC password-free access feature.
If your instance runs a Redis version earlier than 6.0, upgrade your client and modify the code to include the Sentinel authentication password, then try again. For more information, see Password-based connection in this topic.
Q: Why do I receive an Unknown sentinel subcommand 'MASTERS' error?
A: This error occurs because your instance's minor version does not support the SENTINEL MASTERS command. Update the minor version and try again.
Q: Do Redis Open-Source Edition instances support the Sentinel-compatible mode?