Checking N
in the O(N)
command
We recommend that you not excessively use these commands, including HGETALL
, IRANGE
, SMEMBERS
, ZRANGE
, and sinter
. When using them, you need to specify the value of N
.
The HSCAN
, SSCAN
, and ZSCAN
commands in Redis can be used to traverse hash tables, sets, and sorted sets. These commands allow you to scan elements in the data set by using an iterator, without returning all elements at once like HGETALL
, SMEMBERS
, and ZRANGE
. When using these commands, we recommend that you specify an appropriate COUNT parameter to avoid Redis performance degradation caused by excessive returned elements at once. Returning 1000 elements per traversal is preferred, but the specific quantity depends on the actual Redis situation. You can adjust it as needed.
Configuring disabled commands
Commands like KEYS
, FLUSHALL
, FLUSHDB
are not allowed as the single-threaded CRedis takes a long time to execute them, which may cause command execution blocking. To address the potential blocking, we recommend that you use the scan
command in a progressive manner or configure the disabled commands by disable-command-list
parameter.
FLUSHDB
and FLUSHALL
: We recommend that you not use these two commands in the production environment, as they will clear all data in Redis.
KEYS
: It returns all keys matched with specified mode. We recommend that you not use this command in the production environment, as it will block Redis server.
RANDOMKEY
: It returns one key randomly. We recommend that you not use this command in the production environment, as it will block Redis server.
INFO
: It returns various statistics and configuration items of the Redis server. We recommend that you not use this command in the production environment, as it will block Redis server.
CONFIG
: It allows you to modify the configuration items of the Redis server. But use it with caution as it may cause server to crash.
SHUTDOWN
: It allows you to disable the Redis server. We recommend that you not use this command in the production environment, as it will cause data loss.
BGREWRITEAOF
and BGSAVE
: It allows you to asynchronously rewrite the AOF and RDB files with a large amount of system resources being consumed. So use it with caution.
Using SELECT properly
Redis supports naming multiple databases with incrementing numbers. During the naming, you can change database at any time by using the SELECT
command. The index number of each database is specified by a digit that starts from 0.
Redis supports multi-database operations. In standard edition, you can sort data using multiple databases; Even so, the business requests of Redis will be affected by operations on other databases as Redis is single-threaded. In cluster edition, we recommend that you prioritize using DB 0, as non-0 DBs do not support scaling. Additionally, when handling customer requests, it is not necessary to execute "SELECT 0" to reduce unnecessary interactions.
Using batch operations properly
RTT consumes much time when accessing Redis . If your application needs to perform a large number of GET
or SET
operations, you can use mget
and mset
for batch data operations to reduce the network RTT. The number of elements in MGET
and MGET
operations should not exceed 500. The greater this number is, the greater the impact will be in the case of jitter or scaling on the backend.
Native command: MEGET
, MESETmset
.
Non-native command: PIPELINE
, which improves efficiency.
Note:
We recommend that you maintain the number of elements in each batch operation within 500 and check whether there is big key
in the elements of batch operation.
The native command is an atom operation, while PIPELINE
is not.
PIPELINE
allows you to pack multiple commands,while the native command not.
PIPELINE
must be supported on client and server.
Using no transactions
Redis has a limited transaction feature and does not support rollback. In addition, in the cluster edition of Redis, keys involved in a transaction operation must reside in the same slot.
Prerequisites for cluster edition using Lua
All keys should be passed using the KEYS array. When calling Redis commands in redis.call
/pcall
, the key must be in the KEYS array. Otherwise, the following error message will be returned: error,"-ERR bad lua script for redis cluster,all the keys that the script uses should be passed using the KEYS array"
.
For a Lua bootstrap action, the keys involved must be on the same Redis node. Otherwise, the following error message will be returned: error, "-ERR eval/evalsha command keys must in same slotrn"
.
MONITOR
command
The MONITOR
command has some impacts on the performance of Redis. In daily usage, it is used only for analyzing command execution and not for monitoring. We recommend that you enable it only in the case of troubleshooting or analysis, and disable it in time.
Prohibit setting Redis as message queue
Don't use Redis as a message queue; otherwise, you may encounter problems in capacity, network, efficiency, and feature.
この記事はお役に立ちましたか?