Redis Best Practices: Naming, Size Limits, Expiration, and Command Pitfalls
This guide outlines practical Redis best practices, covering readable key naming with colons, keeping string values under 10 KB, setting expirations, avoiding O(n) commands, disabling risky commands, leveraging batch operations, and responsibly using the monitor command to maintain performance and stability.
Key Naming Design: Readability, Manageability, Brevity
It is recommended to use a colon (:) to separate parts of a key because many Redis clients group keys by colon. For example: apps:app:1, apps:app:2, apps:app:3. Tools like Redis Desktop Manager automatically categorize them under an apps folder.
Value Design: Avoid Big Keys
String values should be kept within 10 KB. Larger values cause a performance breakpoint, as shown in Redis benchmarks. For instance, a 10 KB value with a QPS of 100 000 would consume the full bandwidth of a 1 Gbps network (10 KB × 100 000 = 1 000 MB).
Control Key Lifecycle: Set Expiration
Set an expiration time for every key whenever possible. Without expirations, stale data can accumulate over months, leading to hundreds of gigabytes of unused cache that may eventually cause performance degradation. Expirations enable automatic eviction of obsolete data.
Beware O(n) Commands
Commands like LINDEX and LREM on lists have O(n) complexity, meaning their execution time grows with the number of elements. Since Redis is single‑threaded, a slow command can delay subsequent commands, severely impacting performance.
Disable Dangerous Commands: KEYS, FLUSHDB, FLUSHALL
These commands should be disabled in the configuration (using rename‑command). FLUSHDB and FLUSHALL wipe data entirely. The KEYS command can cause massive performance loss, especially under high concurrency, as illustrated by real‑world incidents.
Prefer Batch Operations for Efficiency
Batch commands fall into two categories:
Native batch commands such as MGET, MSET, HMGET, HMSET, LPUSH with multiple values.
Non‑native batch commands like Pipeline.
Using these commands can dramatically improve performance, especially on single‑node Redis or Sentinel setups. In cluster mode, batch operations are limited by slot boundaries. Keep batch sizes reasonable (generally ≤ 1000) depending on data size.
monitor Command: Limit Usage Time
The monitor command streams all commands executed by the server in real time. Prolonged use can fill the output buffer and increase memory consumption, especially under high concurrency.
afeiMacBook-Pro:redis-3.2.11 afei$ src/redis-cli monitor
OK
1573915193.053188 [0 127.0.0.1:55357] "COMMAND"
1573915197.087383 [0 127.0.0.1:55357] "set" "name" "afei"
1573915217.938838 [0 127.0.0.1:55357] "set" "公众号" "阿飞的博客"Conclusion
Every technology has trade‑offs; there is no silver bullet. Understanding Redis’s strengths and weaknesses allows you to design a reasonable architecture, write optimal code, and avoid costly mistakes—such as using KEYS in production and incurring financial loss.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
