Principle | Description |
Conciseness | Shorten the Key length appropriately while ensuring semantics. When there are many keys, the memory space occupied by keys cannot be ignored. |
Naming Rules | Start with an English letter. Only uppercase letters, lowercase letters, digits, vertical bars, underscores, English periods ( .), and English half-width colons (:) can appear in the name. |
Semantic Segmentation | Separate different business logic meanings with an English half-width colon ( :). Separate words within the same business logic meaning segment with an English half-width period (.). This is used to represent a complete semantic unit. |
Readability | End the Key name with the Value type it represents to improve readability. For example: user:basic.info:userid:string. |
Length Limit | An excessively long Key name also occupies a certain amount of memory space. It is recommended to keep it within 128 bytes. |
Special Characters Prohibited. | Do not use \\, *, ?, {}, [], (), spaces, single or double quotation marks, escape characters, and so on. Otherwise, the Key may become unsearchable or fail to be retrieved. |
Example | Description |
cx:cxdb:user:000110011 | Concise and semantically clear, using colons for hierarchical structuring. |
user:basic.info:userid:string | Separate words within the same semantic segment with periods, ending with the Value type. |
Example | Issue |
cx:cxdb:cxdb_user_info:000110011 | The table name redundantly contains the database name "cxdb", which is redundant and increases the Key length. |
set user 000110011 | The Key contains spaces, which are special characters prohibited for use. |
# Write a Key> set cx:cxdb:user:000110011 xiaoming# Set the Key to expire in one hour> expire cx:cxdb:user:000110011 3600
> object idletime cx:cxdb:user:000110011(integer) 150039 # This indicates that the Key has not been accessed for 150,039 seconds.
Data Structure Type | Big Key Identification Criteria | Issue Description |
String | The Value exceeds 10 MB. | Excessively large data value |
Set | The number of members exceeds 10,000. | Excessive number of members |
List | The number of members exceeds 10,000. | Excessive number of members |
Hash | The number of members exceeds 1,000, and the total size of member Values is 1,000 MB. | Excessively large total volume of members |
Guideline | Description |
Control Value Size | Keep the String type within 10KB. For Hash, List, Set, and Zset, keep the number of elements under 5000. |
Avoid Directly Deleting Big Keys. | Do not use del to delete big keys unless necessary. |
Progressive Deletion | For non-string big keys, it is recommended to use hscan, sscan, and zscan for progressive deletion. |
Monitor Automatic Deletion Upon Expiration | Prevent issues caused by the automatic deletion of big keys upon expiration. For example, setting a 1-hour expiration for a Zset with 2 million elements triggers a del operation, causing blocking. |
Data Structure Type | Application scenarios | Description |
String | Simple string data, such as configuration information, counters, and so on | If you need to store binary data, you can use the binary-safe string type. |
Hash | Data with multiple fields and values, such as user information, product information, and so on. | It can save memory space and facilitates batch operations. |
List | Ordered collection of elements, such as message queues, task lists, and so on | It supports insertion and deletion operations at both ends. |
Set | Unordered collection of elements, such as tag lists, friend lists, and so on | Supports union, intersection, difference, and other operations. |
Sorted Set | Ordered collection of elements, such as leaderboards, voting lists, and so on. | It is sorted by score and supports range queries. |
Scenario | Recommendation |
Small data volume with frequent traversal needs | Use ziplist for better memory efficiency. |
Large data volume with frequent insertion, deletion, or lookup needs | Avoid using ziplist. Consider using other data structures instead. |
set user:1:name tomset user:1:age 19set user:1:favor football
Operating System: Ubuntu 24.04.3 LTS / x86_64
Runtime Version: GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)
hmset user:1 name tom age 19 favor football
피드백