Best Practices for Using Alibaba Cloud Redis: Key Design, Command Usage, Client Configuration, and Tools
This article outlines Alibaba Cloud Redis development guidelines, covering key naming conventions, value design, command usage, client configuration, lifecycle management, and recommended tools, providing practical recommendations to avoid common pitfalls, improve performance, and ensure reliable operation of Redis in production environments.
Introduction
This guide presents development standards for using Alibaba Cloud Redis, explaining key design, command usage, client usage, and related tools to reduce issues during Redis usage.
1. Key Design
Key Naming
Recommendation: readability and manageability – use business or database name as a prefix and separate parts with colons, e.g., business:table:id .
Example: ugc:video:1
Recommendation: keep keys concise; avoid special characters, spaces, line breaks, or quotes.
Example of shortened key: user:{uid}:friends:messages:{mid} → u:{uid}:fr:m:{mid}
Value Design
Enforce: reject big keys to prevent network traffic spikes and slow queries.
String values should be ≤10KB; hash, list, set, and sorted‑set elements should not exceed 5,000. Example of a bad key: a list with 2 million elements.
For non‑string big keys, avoid DEL ; use HSCAN , SSCAN , or ZSCAN for incremental deletion and be aware of expiration side‑effects.
Recommendation: choose appropriate Redis data types and configure memory encoding (e.g., ziplist) while balancing memory savings and performance.
Example commands:
set user:1:name tom
set user:1:age 19
set user:1:favor football
Correct bulk insertion:
hmset user:1 name tom age 19 favor football
Key Lifecycle
Set expiration with EXPIRE ; if possible, stagger expirations to avoid spikes. Monitor idle time for keys that do not expire.
2. Command Usage
O(N) Commands
Avoid commands that scan large N without limits; use HSCAN , SSCAN , ZSCAN instead of HGETALL , LRANGE , etc., when N is unknown.
Disable Dangerous Commands
Prohibit KEYS , FLUSHALL , FLUSHDB in production; rename or block them, or replace with progressive SCAN processing.
Select Usage
Redis multiple databases are weakly supported; avoid relying on them for isolation.
Batch Operations
Use pipelines for bulk operations (e.g., MGET , MSET ) but limit batch size (≈500 commands) and be aware of atomicity differences.
Differences:
Native commands are atomic; pipelines are not.
Pipelines can bundle different commands.
Both client and server must support pipelines.
Transactions
Redis transactions are limited (no rollback) and, in cluster mode, all keys in a transaction must reside in the same slot (use hashtags to co‑locate keys).
Lua Scripts in Cluster
All keys must be passed via the KEYS array; otherwise the script fails.
All keys must belong to the same slot.
Monitor Command
Use MONITOR sparingly; avoid long‑running sessions.
3. Client Usage
Do not share a single Redis instance across unrelated applications; separate business domains and expose shared data via services.
Use connection‑pooled clients to control connections and improve efficiency.
In high‑concurrency scenarios, add circuit‑breaker mechanisms (e.g., Netflix Hystrix).
Secure access with strong passwords and enable SSL when needed (Alibaba Cloud Redis supports SSL).
Configure appropriate maxmemory‑policy based on workload; default is volatile‑lru . Other policies include:
allkeys‑lru
allkeys‑random
volatile‑random
volatile‑ttl
noeviction
4. Related Tools
Data synchronization: redis‑port
Big‑key search tools
Hot‑key detection (uses MONITOR , use briefly)
5. Appendix – Deleting Big Keys
Use pipelines to accelerate deletion.
Hash: HSCAN + HDEL
List: LTRIM
Set: SSCAN + SREM
Sorted Set: ZSCAN + ZREM
These methods allow gradual removal of large data structures without blocking the Redis server.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.