Databases 7 min read

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.

Programmer DD
Programmer DD
Programmer DD
Redis Best Practices: Naming, Size Limits, Expiration, and Command Pitfalls

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.

Redis Desktop Manager
Redis Desktop Manager

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).

Redis Data Size
Redis Data Size

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.

Disaster caused by KEYS
Disaster caused by KEYS

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.

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.

redisbest practicesKey Naming
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.