After you obtain a private endpoint, you can bypass proxy servers and use the private endpoint to connect to cluster instances of ApsaraDB for Redis. This shortens the response time of ApsaraDB for Redis. This topic describes the precautions and method of using a private endpoint to connect to cluster instances of ApsaraDB for Redis. The Jedis and PhpRedis clients are used in the examples.

Prerequisites

  • Direction connection to the ApsaraDB for Redis cluster instance is enabled. For more information, see Enable a private endpoint.
  • The client IP address is added to the whitelist of the ApsaraDB for Redis cluster instance. For more information, see Set IP address whitelists.
  • A client that supports Redis Cluster is used, such as Jedis and PhpRedis.
    Note
    • If you use a client that does not support Redis Cluster, you may fail to obtain data because the client cannot redirect your request to the correct shard.
    • Jedis uses the JedisCluster class to support Redis Cluster. For more information, see Jedis documentation.
    • You can obtain a list of clients that support Redis Cluster in the client list on the Redis official website.
  • The Elastic Compute Service (ECS) instance on which the Redis client is deployed and the target ApsaraDB for Redis instance are connected to the same virtual private cloud (VPC).

Background information

When you enable direct connection, ApsaraDB for Redis allocates a virtual IP (VIP) address to the master node of each data shard in the ApsaraDB for Redis cluster. Before a client sends the first request to a private endpoint, the client uses a domain name server (DNS) to resolve the private endpoint. The resolution result is the VIP address of a random data shard in the cluster. The client can use this VIP address to manage the data of the ApsaraDB for Redis cluster over the Redis Cluster protocol. The following figure shows the service architecture of an ApsaraDB for Redis cluster instance in the direct connection mode.
Figure 1. Service architecture of an ApsaraDB for Redis cluster instance in the direct connection mode
Service architecture of an ApsaraDB for Redis cluster instance in the direct connection mode

Precautions

  • Private endpoints can be accessed through the Alibaba Cloud internal network only.
  • In the direct connection mode, you can use the SELECT command to switch databases. However, some Redis Cluster clients, such as stackExchange.redis, do not support the SELECT command. If you are using these clients, you can only use database 0.
  • If you use multi-key commands, transactions, or Lua scripts to modify multiple keys, ensure that these keys are stored in the same hash slot.
    Note Multi-key commands include DEL, SORT, MGET, MSET, BITOP, EXISTS, MSETNX, RENAME, RENAMENX, BLPOP, BRPOP, RPOPLPUSH, BRPOPLPUSH, SMOVE, SUNION, SINTER, SDIFF, SUNIONSTORE, SINTERSTORE, SDIFFSTORE, ZUNIONSTORE, ZINTERSTORE, PFMERGE, and PFCOUNT.
  • The direct connection mode supports password-free access in a VPC and username and password authentication.

Sample code for connecting Jedis to the instance

Note For more information about how to use Jedis, see the Jedis documentation.
  • Connections based on the default connection pool
    import redis.clients.jedis.HostAndPort;
    import redis.clients.jedis.JedisCluster;
    import redis.clients.jedis.JedisPoolConfig;
    
    import java.util.HashSet;
    import java.util.Set;
    
    public class DirectTest  {
        private static final int DEFAULT_TIMEOUT = 2000;
        private static final int DEFAULT_REDIRECTIONS = 5;
        private static final JedisPoolConfig DEFAULT_CONFIG = new JedisPoolConfig();
    
        public static void main(String args[]){
    
            // The private endpoint that is assigned to you when you Enable a private endpoint.
            String host = "r-bp1xxxxxxxxxxxx.redis.rds.aliyuncs.com";
            int port = 6379;
            String password = "xxxx";
    
            Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
            jedisClusterNode.add(new HostAndPort(host, port));
    
            JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT,
                    DEFAULT_REDIRECTIONS,password, "clientName", DEFAULT_CONFIG);
    
            jc.set("key","value");
            jc.get("key");
    
            jc.close();
        }
    }
  • Connections based on a custom connection pool
    import redis.clients.jedis.*;
    
    import java.util.HashSet;
    import java.util.Set;
    public class main {
        private static final int DEFAULT_TIMEOUT = 2000;
        private static final int DEFAULT_REDIRECTIONS = 5;
        private static final JedisPoolConfig DEFAULT_CONFIG = new JedisPoolConfig();
    
        public static void main(String args[]){
            JedisPoolConfig config = new JedisPoolConfig();
            // The maximum number of idle connections. Set this parameter as required. The value cannot exceed the maximum number of connections supported by the specific instance type.
            config.setMaxIdle(200);
            // The maximum number of connections. Set this parameter as required. The value cannot exceed the maximum number of connections supported by the specific instance type.
            config.setMaxTotal(300);
            config.setTestOnBorrow(false);
            config.setTestOnReturn(false);
    
            // The private endpoint that is assigned to you when you Enable a private endpoint.
            String host = "r-bp1xxxxxxxxxxxx.redis.rds.aliyuncs.com"; 
            int port = 6379;
            // The password of the instance.
            String password = "xxxxx";
    
            Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
            jedisClusterNode.add(new HostAndPort(host, port));
            JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT,
                    DEFAULT_REDIRECTIONS,password, "clientName", config);
    
            try {
                jc.set("foo", "bar");
                String foobar = jc.get("foo");
                jc.zadd("sose", 0, "car");
                jc.zadd("sose", 0, "bike");
                Set<String> sose = jc.zrange("sose", 0, -1);
            } finally {
                if (jc ! = null) {
                    jc.close();
                }
            }
        }
    }

Sample code for connecting PhpRedis to the instance

Note For more information about how to use PhpRedis, see the PhpRedis documentation.
<? php
// The private endpoint and the connection port.
$array = ['r-bp1xxxxxxxxxxxx.redis.rds.aliyuncs.com:6379'];
// The password for the connection.
$pwd = "xxxx";

// Use the password to connect to the cluster instance.
$obj_cluster = new RedisCluster(NULL, $array, 1.5, 1.5, true, $pwd);

// The result of the connection.
var_dump($obj_cluster);

if ($obj_cluster->set("foo", "bar") == false) {
    die($obj_cluster->getLastError());
}
$value = $obj_cluster->get("foo");
echo $value;
? >