SoFunction
Updated on 2025-05-09

A brief discussion on Redis Key naming specification document

In order to ensure the consistency, readability and maintenance of Redis key names during development, this specification is designed to guide development teams to design a reasonable key name format when using Redis.

1. Naming format

Use the layered format of module: submodule: business description: identification to clarify the source and purpose of the data.

  • Clear hierarchy: gradually refine it from the overall situation to the specific.
  • Uniqueness: The identifier ensures that the key name is unique globally.

Format template:

<module>:<submodule>:<description>:<identifier>

Example:

  • User information:user:profile:id:12345
  • Order Status:order:status:order_id:98765
  • Product inventory:product:inventory:item_id:67890

2. Specific specifications

2.1 Lowercase naming

  • Redis key names must use lowercase letters to avoid case confusion.
  • Lowercase naming is consistent with Redis community practices, making it easier to manage and troubleshoot.

2.2 Separate levels with colons

  • Use colons (:) represents a logical hierarchy relationship as a separator.
  • Each part expresses an independent meaning, and the colon separates it for easy reading and automatic analysis.

2.3 Identifier naming

  • Unique identifier part (such asidorder_idetc.) Its significance should be clearly expressed.
  • Use underscore (_) Separate compound words, not camel name (camelCase)。

3. Data type extension naming

For different data structures (e.g.listsethash), you can add type information after the business description part or identifier:

Data Type Format example
String user:profile:id:12345
List chat:messages:list:room_id:56789
Hash user:profile:hash:id:12345
Set (Set) product:categories:set:item_id:67890
Ordered Set (ZSet) leaderboard:score:zset:game_id:11111

4. Time-sensitive key names

For data that has time attributes or needs to expire automatically, you can include time information in the key name:

  • Date information:<description>:date:<YYYYMMDD>Example:order:summary:date:20241122
  • Timestamp information:<description>:ts:<timestamp>Example:session:token:uid:abc123:ts:1692806400

5. Standard summary

5.1 Recommended key name style

  • Use lowercase letters.
  • Colons separate logical levels.
  • Underlines separate words and clearly express the meaning of the data.

5.2 Avoided Problems

  • Avoid excessively long key names: the longer the key names, the more memory usage, and the lower the efficiency.
    • Not recommended:user:profile:personal:information:unique:identifier:12345
    • recommend:user:profile:id:12345
  • Avoid abbreviation: abbreviation may make key names difficult to understand.
    • Not recommended:usr:prf:id:12345
    • recommend:user:profile:id:12345
  • Avoid camel naming: lowercase and underscore-separated styles are more suitable for Redis key names.
    • Not recommended:orderStatusOrderId98765
    • recommend:order:status:order_id:98765

6. Practical application examples

6.1 Redis Key Tool Class in Java

public class RedisKeyUtil {

    // User profile key name    public static String userProfileKey(String userId) {
        return ("user:profile:id:%s", userId);
    }

    // Order status key name    public static String orderStatusKey(String orderId) {
        return ("order:status:order_id:%s", orderId);
    }

    // Chat history list key name    public static String chatMessagesKey(String roomId) {
        return ("chat:messages:list:room_id:%s", roomId);
    }
}

6.2 Common Redis key names

use Key name
User profile user:profile:id:12345
User login status user:session:uid:abc123
Product inventory product:inventory:item_id:67890
Order Status order:status:order_id:98765
Chat message queue chat:messages:list:room_id:12345
Ranking list leaderboard:score:zset:game_id:1

7. FAQ

Why are lowercase and underscore separation recommended?

  • Lowercase naming avoids case confusion.
  • The underlined style is more readable, especially for compound words.

How to control the length of the key name?

  • Avoid more than 128 characters.
  • While ensuring that the key name has a clear meaning, try to be concise.

Is it necessary to have a unified team style?

  • yes. Formulate and follow unified naming specifications to facilitate teamwork and post-maintenance.

Through the above specifications, the team can improve the readability and maintainability of Redis data while avoiding common naming problems. It is recommended to make appropriate adjustments based on actual business conditions to meet specific needs.

This is the end of this article about the Redis Key naming specification document. For more related contents of Redis Key naming specifications, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!