Redis Development Guidelines: Key Design, Command Usage, and Best Practices
This article outlines comprehensive Redis development standards, covering key naming conventions, value design, lifecycle management, command usage guidelines, prohibited commands, batch operations, transaction considerations, and Lua scripting constraints, aiming to reduce common pitfalls and improve performance and maintainability.
Key Design
Key Name Design
Use a business name or database name as a prefix to avoid key collisions, separating parts with colons, e.g., ugc:video:1 or business:table:id .
Readability and Manageability
Choose readable prefixes and keep keys concise; long keys increase memory usage. Example of shortening:
user:{uid}:friends:messages:{mid} can be simplified to u:{uid} .
Simplicity
Avoid special characters such as spaces, newlines, quotes, or escape characters.
Value Design
Avoid Big Keys
Keep string values under 10KB; limit hash, list, set, and sorted‑set elements to no more than 5,000. Large collections (e.g., a list with two million items) should be avoided.
For non‑string big keys, use incremental deletion with hscan , sscan , or zscan instead of DEL , and be aware of expiration‑triggered deletions.
Select Appropriate Data Types
Use the most suitable Redis data structure and configure memory encoding (e.g., ziplist) while balancing memory savings and performance.
Bad example (using separate strings for each field):
set user:1:name tom
set user:1:age 19
set user:1:favor footballGood example (using a hash):
hmset user:1 name tom age 19 favor footballControl Key Lifecycle
Set expiration times with EXPIRE (consider staggering expirations to avoid spikes). For non‑expiring data, monitor IDLETIME closely.
Command Usage
Beware O(N) Commands
Commands like HGETALL , LRANGE , SMEMBERS , ZRANGE , and SINTER traverse N elements; use their scan counterparts ( HSCAN , SSCAN , ZSCAN ) when N can be large.
Prohibited Commands
Avoid using KEYS , FLUSHALL , FLUSHDB in production; disable them via Redis rename mechanism or replace with safe alternatives like SCAN .
Selective Use of SELECT
Redis databases are weakly isolated; many clients have limited support, and using multiple databases can cause interference in a single‑threaded environment.
Batch Operations for Efficiency
Native batch commands such as MGET and MSET .
Use pipelines for non‑native batch operations, but keep batch size reasonable (e.g., ≤500 items, depending on payload size).
Note that native batch commands are atomic, while pipelines are not; pipelines can combine different commands but require client and server support.
Limit Use of Transactions
Redis transactions lack rollback and, in clustered mode, all keys in a transaction must reside in the same slot (use hashtags to co‑locate keys if needed).
Lua Scripting in Cluster Mode
All keys used in a Lua script must be passed via the KEYS array, and they must belong to the same slot; otherwise the script fails with an error.
MONITOR Command
Use MONITOR only when necessary and avoid long‑running sessions.
By following these guidelines, developers can minimize Redis‑related issues, improve performance, and maintain a clean, manageable data store.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
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.