Mastering Alibaba Cloud Redis: Key Design, Commands, and Client Best Practices
This guide outlines Alibaba Cloud Redis development standards, covering key naming conventions, value design, command usage, client configuration, eviction policies, and tools for data synchronization, big-key management, and monitoring, helping developers avoid common pitfalls and optimize performance.
Key Design
Key naming
Readability and manageability
Use business name (or database name) as prefix to avoid key collisions, separated by colon, e.g., business:table:id
ugc:video:1Simplicity
Keep key length short while preserving semantics; long keys increase memory usage.
user:{uid}:friends:messages:{mid} simplified to u:{uid}Avoid special characters
Do not include spaces, line breaks, quotes, or other escape characters.
Value design
Avoid bigkey (prevent network traffic, slow queries)
String values should be under 10KB; hash, list, set, zset elements should not exceed 5000.
Bad example: a list with 2 million elements.
For non‑string bigkeys, avoid DEL; use HSCAN, SSCAN, ZSCAN for incremental deletion and be aware of expiration issues.
Select appropriate data type
Choose data structures wisely, balancing memory encoding optimizations (e.g., ziplist) with performance.
set user:1:name tom
set user:1:age 19
set user:1:favor footballCorrect usage:
hmset user:1 name tom age 19 favor footballControl key lifecycle
Use EXPIRE to set TTL, optionally stagger expirations; monitor idle time for non‑expiring keys.
Command Usage
O(N) commands attention to N
Commands like HGETALL, LRANGE, SMEMBERS, ZRANGE, SINTER are usable but N must be known; prefer HSCAN, SSCAN, ZSCAN for iteration.
Disable commands
Avoid using KEYS, FLUSHALL, FLUSHDB in production; disable via Redis rename or use SCAN for incremental processing.
Reasonable use of SELECT
Redis multiple databases are weak; many clients have poor support; using multiple DBs can cause interference.
Batch operations for efficiency
Native commands: MGET, MSET.
Non‑native: use PIPELINE.
Limit batch size (e.g., ≤500 elements) depending on element size.
Native commands are atomic; pipeline is non‑atomic.
Pipeline can bundle different commands.
Pipeline requires client and server support.
Avoid excessive Redis transactions
Transactions are weak (no rollback) and in cluster mode all keys in a transaction must be in the same slot (use hashtag to solve).
Lua script constraints in Redis Cluster
All keys must be passed via KEYS array; commands must reference KEYS positions; all keys must reside in the same slot.
MONITOR command
Use MONITOR only when necessary and avoid long‑running sessions.
Client Usage
Avoid sharing a Redis instance across multiple applications
Separate unrelated business logic; expose shared data via services.
Use connection pool
Control connections and improve efficiency. Example:
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
}Circuit breaker
In high concurrency, add circuit‑breaker (e.g., Netflix Hystrix).
Proper encryption
Set strong passwords; use SSL if needed (supported by Alibaba Cloud Redis).
Eviction policy
Choose appropriate maxmemory‑policy based on business; default is volatile‑lru, which evicts expired keys using LRU, but may cause OOM.
allkeys‑lru: evicts any key by LRU.
allkeys‑random: evicts random keys.
volatile‑random: evicts random expired keys.
volatile‑ttl: evicts keys nearest to expiration.
noeviction: rejects writes when memory limit reached.
Related Tools
Data synchronization
Use redis‑port for data sync between Redis instances.
Big‑key search
Tool: https://developer.aliyun.com/article/117042
Hot‑key detection
Implementation uses MONITOR; short‑term use of redis‑faina is recommended; Alibaba Cloud Redis handles hot‑key at kernel level.
Delete bigkey
Use PIPELINE to accelerate deletion.
Redis 4.0 supports asynchronous key deletion.
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);
List<Entry<String, String>> entryList = scanResult.getResult();
if (entryList != null && !entryList.isEmpty()) {
for (Entry<String, String> entry : entryList) {
jedis.hdel(bigHashKey, entry.getKey());
}
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
// delete bigkey
jedis.del(bigHashKey);
}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 left = 100;
while (counter < llen) {
// trim 100 elements from left
jedis.ltrim(bigListKey, left, llen);
counter += left;
}
// finally delete key
jedis.del(bigListKey);
}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);
List<String> memberList = scanResult.getResult();
if (memberList != null && !memberList.isEmpty()) {
for (String member : memberList) {
jedis.srem(bigSetKey, member);
}
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
// delete bigkey
jedis.del(bigSetKey);
}SortedSet 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);
List<Tuple> tupleList = scanResult.getResult();
if (tupleList != null && !tupleList.isEmpty()) {
for (Tuple tuple : tupleList) {
jedis.zrem(bigZsetKey, tuple.getElement());
}
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
// delete bigkey
jedis.del(bigZsetKey);
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
