You need to enable JavaScript to run this app.
Cache for Redis

Cache for Redis

Copy page
Download PDF
Data security
Connecting to Redis from client via SSL encryption
Copy page
Download PDF
Connecting to Redis from client via SSL encryption
Cache for Redis provides SSL (Secure Sockets Layer) encryption service. You can enable SSL encryption in advance, and improve the security of the data link by setting SSL encryption when connecting to the database. This topic describes the specific methods for multilingual clients to access Redis databases via SSL encrypted connections.​
Precautions​
Accessing Redis instances through a private network is relatively secure, and typically requires no data link encryption. Using SSL encryption connection can increase the network response time of Redis services. We recommend that you enable SSL encryption only when necessary (such as when connecting to Redis instances via the internet).​
Preparation​
Before connecting to Redis via SSL encryption at client, you should make the following preparations.​
​
Preparation​
Description​
Setting SSL encryption for Redis​
Enable SSL encryption for the target Redis instance and download the SSL CA certificate. For specific procedures, refer to Setting SSL encryption.​
Setting allowlist for Redis​
Add the IP address of the local server or ECS instance with the client installed to the allowlist of the Redis instance. For specific procedures, refer to Setting allowlist.​
tip​
If your client device and Redis instance are not in the same VPC, you also need to enable public network access for the Redis instance. For specific procedures, refer to Applying for public address.​
Obtain database username information​
Refer to the following steps to obtain the database login username and password for Cache for Redis:​
  • Username​
  • To ensure data security, a default username will be automatically created after the instance is created. You can also create a new username and set the permissions for it according to your service requirements. For more information, refer to Creating and managing account.​
  • Password​
  • Redis supports passwords in the following formats:​
  • Enter the password directly (this method is only available with the default account).​
  • <username> <password>.​
  • <username>:<password>.​
tip​
If you forget your password, you can reset it. For specific procedures, refer to Resetting password.​
Obtain database connection information​
You can follow these steps to obtain the connection address and port number information for Cache for Redis:​
  1. Log in to the Redis console.​
  1. In the upper-left corner of the top menu bar, select the project and region to which the instance belongs.​
  1. On the Instances page, click the target instance.​
  1. On the instance details page, click the Connectivity tab to view the corresponding connection address and port number information in the Private network access or Public network access section.​
tip​
For more information about Redis connection addresses, refer to Address types.​
​
Examples of client SSL connection​
tip​
For more details about the usage instructions of the third-party SDK client program, refer to Instructions for using third-party SDK clients.​
​
Java
Python
PHP
GO
​
tip​
The steps below are illustrated using Jedis client version 3.6.0 as an example. Recommend using the latest version.​
​
import java.io.FileInputStream;​
import java.io.InputStream;​
import java.security.KeyStore;​
import java.security.SecureRandom;​
​
import javax.net.ssl.SSLContext;​
import javax.net.ssl.SSLSocketFactory;​
import javax.net.ssl.TrustManager;​
import javax.net.ssl.TrustManagerFactory;​
​
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;​
import redis.clients.jedis.Jedis;​
import redis.clients.jedis.JedisPool;​
​
public class JedisSSLTest {​
private static SSLSocketFactory createTrustStoreSSLSocketFactory(String jksFile) throws Exception {​
KeyStore trustStore = KeyStore.getInstance("jks");​
InputStream inputStream = null;​
try {​
inputStream = new FileInputStream(jksFile);​
trustStore.load(inputStream, null);​
} finally {​
inputStream.close();​
}​
​
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("PKIX");​
trustManagerFactory.init(trustStore);​
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();​
​
SSLContext sslContext = SSLContext.getInstance("TLS");​
sslContext.init(null, trustManagers, new SecureRandom());​
return sslContext.getSocketFactory();​
}​
​
public static void main(String[] args) throws Exception {​
// redis-shzl8lsda9uaa****_ca_certificate.jks is the certificate file name.​
final SSLSocketFactory sslSocketFactory = createTrustStoreSSLSocketFactory("redis-shzl8lsda9uaa****_ca_certificate.jks");​
// The connection pool settings are the instance's connection address, port number, timeout settings, and password.​
JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), "redis-shzl8lsda9uaa****.redis.ivolces.com",​
6379, 2000, "SslTest:Pwd@1234", 0, true, sslSocketFactory, null, null);​
​
try (Jedis jedis = pool.getResource()) {​
jedis.set("key", "value");​
System.out.println(jedis.get("key"));​
}​
}​
}​
​
​
Last updated: 2026.02.09 14:20:01