Suggestions on Using Redis
This section provides suggestions on using Redis, including setting key names and values, and using commands.
Key
Suggestions on setting key names:
- Keep the format consistent.
Normally, use the service name or database name as the prefix, followed by colons (:). For example: service name:table name:ID.
- Minimize the key length.
Minimize the key length without compromising clarity of the meaning. Abbreviate common words. For example, user can be abbreviated to u, and messages can be abbreviated to msg.
- Do not include special characters.
Value
Suggestions on setting key values:
- Avoid using big keys.
Big keys can result in traffic storms and slow queries. Keep the size of Strings within 10 KB and the number of Hashes, Lists, Sets, or Zsets within 5000.
- Use appropriate data types.
For example, to store multiple attributes of a user, you can use multiple keys, such as set u:1:name "X" and set u:1:age 20. To save memory usage, you can also use the HMSET command to set multiple fields to their respective values in the hash stored at one key.
- Set appropriate timeout.
Redis Commands
Suggestions on using Redis commands:
- Pay attention to the value of N for commands whose time complexity is O(N).
For example, the HGETALL, LRANGE, SMEMBERS, ZRANGE, and SINTER commands may compromise performance if they involve a large number of elements. Alternatively, you can use SCAN sister commands, such as HSCAN, SSCAN, and ZSCAN commands.
- Some commands are not supported in DCS. For details, see Redis Command Compatibility and Web CLI Command Compatibility.
- Exercise caution when using the SELECT command.
Redis does not have a strong support for the shared database functionality. Redis is single-threaded, so shared databases interferes with each other. You are advised to use multiple Redis instances instead of using shared databases for one instance. Cluster DCS instances on HUAWEI CLOUD do not support shared databases.
- For batch operations, use the MGET command, MSET command, or pipelining to improve the efficiency, but do not include a large number of elements in one batch operation.
MGET command, MSET command, and pipelining differ in the following ways:
- MGET and MSET are atomic operations, while pipelining is not.
- Pipelining can be used to send multiple commands at once, while MGET and MSET cannot.
- Pipelining must be supported by both the server and the client.
- The timeout of Lua scripts is 5s, so avoid using long scripts, such as including time-consuming sleep statements or long loops.
- When invoking the Lua script, do not use random functions to specify keys. Otherwise, the execution results will be inconsistent between the master and standby nodes, causing data inconsistency.
- The restrictions on using Lua scripts for cluster instances are as follows:
- When the EVAL or EVALSHA command is run, the command parameter must contain at least one key. Otherwise, the client displays the error message " ERR eval/evalsha numkeys must be bigger than zero in redis cluster mode".
- When the EVAL or EVALSHA command is run, a cluster DCS Redis instance uses the first key to compute slots. Ensure that the keys to be operated are in the same slot.
Others
- Use connection pools and persistent connections ("pconnect" in Redis terminology) to control connections and improve the efficiency.
- Set maxmemory-policy (the eviction policy applied when the maxmemory limit is reached) to one of the following policies:
- noeviction: When the memory limit is reached, DCS instances return errors to clients and no longer process write requests and other requests that could result in more memory to be used. However, DEL and a few more exception requests can continue to be processed.
- allkeys-lru: DCS instances try to evict the least recently used keys first, in order to make space for new data.
- volatile-lru: DCS instances try to evict the least recently used keys with an expire set first, in order to make space for new data.
- allkeys-random: DCS instances recycle random keys so that new data can be stored.
- volatile-random: DCS instances evict random keys with an expire set, in order to make space for new data.
- volatile-ttl: DCS instances evict keys with an expire set, and try to evict keys with a shorter time to live (TTL) first, in order to make space for new data.
- allkeys-lfu: DCS instances evict the least frequently used keys from all keys.
- volatile-lfu: DCS instances evict the least frequently used keys with an expire field from all keys.
- When deleting big keys, do not always use the DEL command.
- To delete big Hashes, use HSCAN + HDEL commands.
- To delete big Lists, use the LTRIM command.
- To delete big Sets, use SSCAN + SREM commands.
- To delete big Sorted Sets, use ZSCAN + ZREM commands.
- Do not include a large number of commands in a pipeline. A maximum of 1024 commands is recommended for a pipeline of a cluster instance.
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.