Updated on 2024-11-29 GMT+08:00

Redis Usage Specifications

Key and Value Design

  • Key design
    • [Mandatory] A key must not contain the following special characters: spaces, brackets (excluding braces ({})), and newline characters (\n), etc.
    • [Suggestion] Control the key length while ensuring semantics. When there are a large number of keys in Redis, the keys use a large amount of memory.
    • [Recommendation] You are advised to configure an expiration time for each Redis key-value pair to control the lifecycle of Redis. You are advised to add a random number to each expiration time so that all Redis key-value pairs do not expire at the same time.
  • Value design
    • [Mandatory] Do not add large values. String values must be at most 64 KB, and hash, list, set, and ZSET values must not exceed 3000.

Command Usage

  • [Mandatory] Do not run the keys command to perform fuzzy search in the production environment. If there is a large amount of data, the active instance in the Redis cluster becomes faulty.
  • [Mandatory] Use the Redis cluster mode instead of the single-node mode.
  • [Mandatory] Do not use time-consuming commands or sleep commands in Lua scripts.
  • [Recommendation] Pay attention to the commands whose complexity is O (N) and use them with caution in the production environment.
  • [Recommendation] If fuzzy search is required, run the scan, hcan, zscan, or sscan command.
  • [Recommendation] You are advised to use a pipeline when you need to read and write data for multiple times. It is recommended that a maximum of 500 elements be read and written at a time.
  • [Recommendation] Unless otherwise specified, you are not advised to use Redis as a message queue in pub/sub scenarios.
  • [Recommendation] You are advised to run the unlink command instead of the del command to delete large values.
  • [Suggestion] Redis is not recommended because its transaction capability is weak.
  • [Suggestion] You are not advised to run the monitor command for a long time unless necessary.