Best Practices and Development Guidelines for Using Alibaba Cloud Redis
This article presents comprehensive Alibaba Cloud Redis development standards covering key and value design, lifecycle management, command usage, client integration, tooling, and efficient big‑key deletion techniques to help developers avoid common pitfalls and improve performance.
1. Key Design
Use readable, manageable keys by prefixing with the business or database name and separating parts with colons (e.g., business:table:id ). Keep keys concise to reduce memory usage and avoid special characters such as spaces, line breaks, or quotes.
Examples:
ugc:video:1Shortened form for complex keys:
user:{uid}:friends:messages:{mid} → u:{uid}:fr:m:{mid}2. Value Design
Avoid big keys: limit STRING values to 10 KB and keep the number of elements in HASH , LIST , SET , and ZSET below 5 000. Use SCAN ‑based incremental deletion for non‑string big keys.
Incorrect usage (multiple SET commands):
set user:1:name tom set user:1:age 19 set user:1:favor footballCorrect usage with a single HMSET :
hmset user:1 name tom age 19 favor football3. Key Lifecycle Management
Set expiration times with EXPIRE (prefer staggered expirations to avoid spikes) and monitor idle time for keys that should not expire.
4. Command Usage
Prefer O(N) commands where the size N is known; replace full scans with HSCAN , SSCAN , or ZSCAN . Disable risky commands such as KEYS , FLUSHALL , and FLUSHDB via the Redis rename mechanism.
Avoid using SELECT for multiple databases in production because it adds overhead and can cause interference.
Batch operations improve efficiency; native commands (e.g., MGET , MSET ) are atomic, while pipelines are non‑atomic but allow mixed commands.
mget key1 key2 pipeline ...5. Client Usage
Isolate applications by using separate Redis instances. Employ connection pools (e.g., JedisPool ) to control connections and improve throughput. Handle exceptions properly and close resources after use.
Jedis jedis = new Jedis(host, port);In high‑concurrency scenarios, add circuit‑breaker mechanisms (e.g., Netflix Hystrix) and enable SSL if needed.
6. Related Tools
Data synchronization can be performed with redis‑port . For big‑key detection, use specialized tools; hotspot key detection can leverage Facebook's redis‑faina , which uses MONITOR for short‑term analysis.
7. Deleting Big Keys
Provide Java utilities that iteratively scan and delete large structures before removing the key itself. Sample methods include:
public void delBigHash(String host, int port, String password, String bigHashKey) { /* scan with HSCAN and hdel */ } public void delBigList(String host, int port, String password, String bigListKey) { /* use LLEN, LTRIM, then del */ } public void delBigSet(String host, int port, String password, String bigSetKey) { /* SSCAN + SREM */ } public void delBigZset(String host, int port, String password, String bigZsetKey) { /* ZSCAN + ZREM */ }After incremental deletion, remove the key with jedis.del(bigKey) .
Following these guidelines helps reduce Redis‑related issues, improves performance, and ensures stable operation in production environments.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
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.