Databases 11 min read

Mastering Alibaba Cloud Redis: Key Design, Commands, and Client Best Practices

This guide details Alibaba Cloud Redis development standards, including readable key naming, value size limits, safe command usage, client connection pooling, eviction policies, useful tooling, and efficient big‑key deletion methods, helping engineers build high‑performance, reliable Redis services.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Mastering Alibaba Cloud Redis: Key Design, Commands, and Client Best Practices

Introduction

This article introduces development guidelines for using Alibaba Cloud Redis, covering key‑value design, command usage, client configuration, related tools, and big‑key removal techniques.

1. Key‑Value Design

1.1 Key Naming

Use readable, manageable keys with a business or database prefix separated by colons, e.g., gc:video:1. Keep keys concise to reduce memory usage, e.g., u:{uid}:fr:m:{mid}. Avoid special characters such as spaces, line breaks, or quotes.

1.2 Value Design

Avoid big keys: keep string values under 10 KB and limit hash, list, set, zset elements to 5 000. For non‑string big keys, use incremental deletion with hscan, sscan, or zscan instead of DEL. Choose appropriate data structures and control key lifecycles with EXPIRE or staggered expirations.

2. Command Usage

2.1 O(N) Commands

Commands like HGETALL, LRANGE, SMEMBERS, ZRANGE should be used only when the size N is known; otherwise prefer HSCAN, SSCAN, ZSCAN.

2.2 Disabled Commands

Avoid using KEYS, FLUSHALL, FLUSHDB in production; rename or block them.

2.3 SELECT Usage

Redis multiple databases are weakly supported; using separate instances is recommended.

2.4 Batch Operations

Use native batch commands like MGET, MSET or pipelines for higher throughput, limiting batch size (e.g., ≤ 500 elements) to avoid memory spikes.

2.5 Transactions

Redis transactions lack rollback and require all keys in a transaction to reside in the same slot when using clusters.

2.6 Lua Scripts in Cluster

All keys must be passed via the KEYS array, and they must belong to the same slot; otherwise the script fails.

3. Client Usage

3.1 Instance Isolation

Separate unrelated business workloads into different Redis instances.

3.2 Connection Pool

Use a connection pool to control connections and improve efficiency. Example (Jedis):

Jedis jedis = null;
try {
    jedis = jedisPool.getResource();
    // execute commands
    jedis.executeCommand();
} 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

In high‑concurrency scenarios, add a circuit‑breaker such as Netflix Hystrix.

3.4 Encryption

Set strong passwords and enable SSL if needed (supported by Alibaba Cloud Redis).

3.5 Eviction Policies

Choose an appropriate maxmemory‑policy (e.g., volatile‑lru, allkeys‑lru, noeviction) and configure expiration times to avoid OOM.

4. Related Tools

Data synchronization: redis‑port Big‑key search tools

Hot‑key detection (e.g., facebook’s redis‑faina)

5. Deleting Big Keys

Use pipelines to accelerate deletion. Redis 4.0 supports asynchronous key deletion.

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<Entry<String, String>> scanResult = jedis.hscan(bigHashKey, cursor, scanParams);
        for (Entry<String, String> 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<String> 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<Tuple> 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);
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

redisAlibaba CloudKey DesignCommand OptimizationBigKey ManagementClient Pooling
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.