Best Practices for Using Alibaba Cloud Redis: Key Design, Command Usage, Client Configuration, and BigKey Management
This article presents comprehensive guidelines for Alibaba Cloud Redis, covering readable and concise key naming, value design to avoid big keys, safe command usage, client connection pooling and circuit‑breaker patterns, appropriate eviction policies, and practical code examples for deleting large hashes, lists, sets, and sorted sets.
This article introduces development specifications for using Alibaba Cloud Redis, focusing on key design, command usage, client usage, related tools, and big‑key deletion techniques.
1. Key Design
1.1 Key Naming
Readability and Manageability
Use business name (or database name) as a prefix to avoid collisions, separated by colons, e.g., ugc:video:1 .
Simplicity
Keep keys short while preserving semantics; long keys increase memory usage. Example of a concise key:
u:{uid}:fr:{mid}Avoid Special Characters
Do not include spaces, line breaks, quotes, or escape characters.
1.2 Value Design
Avoid Big Keys
Limit string values to 10 KB and keep hash, list, set, zset element counts under 5 000 to prevent network traffic spikes and slow queries.
Select Appropriate Data Types
Choose data structures that balance memory encoding and performance, e.g., using hmset for multiple fields instead of separate set commands.
Control Key Lifecycle
Use expire to set TTLs (preferably staggered) and monitor idletime for non‑expiring keys.
2. Command Usage
2.1 O(N) Commands
Commands like hgetall , lrange , smembers , zrange are acceptable when the size (N) is known; otherwise prefer hscan , sscan , zscan .
2.2 Disable Dangerous Commands
Prohibit keys , flushall , flushdb in production, using Redis rename or scan‑based alternatives.
2.3 Use of SELECT
Avoid relying on multiple logical databases; they share a single thread and many clients have limited support.
2.4 Batch Operations
Use native batch commands ( mget , mset ) or pipelines for higher throughput, limiting batch size to around 500 elements.
2.5 Transactions
Redis transactions lack rollback and require all keys in a slot for cluster mode; use with caution.
2.6 Lua Scripts in Cluster
All keys must be passed via the KEYS array and reside in the same slot; otherwise the script fails.
2.7 MONITOR Command
Use monitor only when necessary and avoid long‑running sessions.
3. Client Usage
3.1 Isolation of Applications
Separate unrelated business workloads; share only common data via services.
3.2 Connection Pooling
Employ a connection pool to control connections and improve efficiency. Example (Java/Jedis):
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
// execute commands
} catch (Exception e) {
logger.error("op key {} error: " + e.getMessage(), key, e);
} finally {
if (jedis != null) {
jedis.close(); // returns to pool
}
}3.3 Circuit Breaker
Integrate a circuit‑breaker (e.g., Netflix Hystrix) for high‑concurrency scenarios.
3.4 Secure Access
Set strong passwords and enable SSL encryption if needed.
3.5 Eviction Policies
Choose an appropriate maxmemory-policy (e.g., volatile-lru , allkeys-lru , noeviction ) and configure TTLs based on business needs.
4. Related Tools
4.1 Data Synchronization
Use redis-port for cross‑instance data sync.
4.2 Hot Key Detection
Leverage monitor or third‑party tools like redis-faina to identify hot keys.
5. Deleting Big Keys
5.1 Hash Deletion (hscan + hdel)
public void delBigHash(String host, int port, String password, String bigHashKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult
> scanResult = jedis.hscan(bigHashKey, cursor, scanParams);
for (Entry
entry : scanResult.getResult()) {
jedis.hdel(bigHashKey, entry.getKey());
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
jedis.del(bigHashKey);
}5.2 List Deletion (ltrim)
public void delBigList(String host, int port, String password, String bigListKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
long llen = jedis.llen(bigListKey);
int counter = 0;
int batch = 100;
while (counter < llen) {
jedis.ltrim(bigListKey, batch, llen);
counter += batch;
}
jedis.del(bigListKey);
}5.3 Set Deletion (sscan + srem)
public void delBigSet(String host, int port, String password, String bigSetKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult
scanResult = jedis.sscan(bigSetKey, cursor, scanParams);
for (String member : scanResult.getResult()) {
jedis.srem(bigSetKey, member);
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
jedis.del(bigSetKey);
}5.4 Sorted Set Deletion (zscan + zrem)
public void delBigZset(String host, int port, String password, String bigZsetKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult
scanResult = jedis.zscan(bigZsetKey, cursor, scanParams);
for (Tuple tuple : scanResult.getResult()) {
jedis.zrem(bigZsetKey, tuple.getElement());
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
jedis.del(bigZsetKey);
}By following these guidelines, developers can reduce common Redis pitfalls, improve performance, and maintain a stable production environment.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.