Redis has powerful functions and rich data types. No matter how fast the system is, it cannot withstand crazy abuse. By disabling some high-risk functions and hanging the shackles of development, the business can consider problems with concise and general ideas instead of being bound to a certain implementation.
Redis will have different persistence strategies and eviction strategies based on different uses. Therefore, before using and applying for a Redis cluster, please clarify whether it is used for caching or storage. Redis cluster has two modes: master-slave and Cluster, both of which have their own advantages and disadvantages. The following specifications do not distinguish between cluster modes. We explain them in terms of usage scenarios and operational restrictions.
Although redis supports persistence, storing all data in redis is very expensive. It is recommended to load hot data (such as data with QPS exceeding 5k) into redis. Low-frequency data can be stored in Mysql
and ElasticSearch
.
Do not put all irrelevant data services into one Redis
. On the one hand, it avoids mutual business impact, on the other hand, it avoids the expansion of a single instance, and can reduce the impact and quickly recover in the event of a failure.
Since Redis
is a single-threaded service, excessive messages will block and slow down other operations. It's a good practice to keep message content under 1KB. Single records exceeding 50KB are strictly prohibited. Excessively large messages will also cause high usage of network bandwidth and IO problems when persisting to disk.
Frequent creation and destruction of connections will waste a lot of system resources, and in extreme cases will cause the host to crash. Please make sure you are using the correct Redis
client connection pool configuration.
As a Key
used for cache, the expiration time must be set. The longer the expiration time is, the better. Please set it according to the nature of your business. Note that some units of the failure time are seconds and some are milliseconds. Many students may easily make mistakes if they don't pay attention.
The cache should be used only for caching. The business logic should not change after being removed, and it must not be cut into the business. First, the high availability of the cache will affect the business; second, the deep coupling will have unpredictable effects; third, it will have a skin effect on the maintenance row.
Forget about small applications
Such as singleredis
The cluster cannot be used for your data Services, do not rush to expand your redis
cluster (including M/S and Cluster). The larger the cluster, the worse the performance in state synchronization and persistence. Priority is given to using the client hash
for cluster splitting. For example: 10 clusters are divided according to user ID, and the user whose last number is 0 falls into the first cluster.
Keys
The command efficiency is extremely low, it is an O(N)
operation and will block Other normal commands, on cluster
, would be disastrous operations. Use is strictly prohibited, DBA
should rename
this command and disable it from the source.
flush
The command will clear all data and is a high-risk operation. Use is strictly prohibited, DBA
should rename
this command, disabled from the source, only DBA
is operable.
If there are no very special needs, it is strictly prohibited to use Redis
as a message queue. Redis
When used as a message queue, there will be various problems in terms of capacity, network, efficiency, and functionality. If you need a message queue, you can use the high-throughput Kafka
or the highly reliable RocketMQ
.
redis
So fast and slow queries, except for network delays, belong to these batch operation functions. Most online problems are caused by these functions.
ZRANGE
, ZRANGEBYSCORE
and other multiple operationsZSET
function, it is strictly prohibited to use ZRANGE myzset 0 -1
and other operations that do not set a range. Please specify the range, such as ZRANGE myzset 0 100
. If you are not sure about the length, you can use ZCARD
to determine the length
HGETALL
for large data volume keys and it will be taken out For all data related to HASH
, if the number of data items is too large, it will also cause congestion. Please ensure that the business is controllable. If you are not sure about the length, you can use HLEN
to determine the length first
Redis Cluster
##MGET operation will collect data from each shard and aggregate it. Compared with the traditional
M/S architecture, the performance will drop a lot. Please stress test and evaluate in advance
select function is used to switch
database, for For users, this is where problems can easily occur. The
cluster mode does not support multiple
database and has no benefits, so it is disabled.
redis
It is already very fast. If there is no major need, it is recommended to catch exceptions and roll back. Do not use transaction functions. Few people do this.
lua
Although the script can do many things that look cool
, it is like SQL
's stored procedures will introduce performance and some difficult maintenance issues and should be disabled.
monitor
function can quickly see the data flow currently being executed by redis
, but be careful, it will be blocked for a long time during peak periods On the monitor
command, it will seriously affect the performance of redis
. This command is not prohibited from being used, but special care must be taken when using it.
Redis
's Key
must be standardized so that when problems are encountered, they can be easily located. Redis
belongs to the KV
database without scheme
, so we rely on convention to establish its scheme
semantics. Its benefits:
Can clean data based on a certain type of key
Can update data based on a certain type of key
Be able to understand the ownership and application scenarios of certain types of keys
Prepare for unification and platformization and reduce technical changes
Generally, a key
needs to have the following dimensions: business, key purpose, variables, etc. Each dimension is separated by:. Here are several examples of keys:
user:sex User 10002232’s gender
msg:achi 201712’s user number ranking
The above is the detailed content of What are the Redis specifications?. For more information, please follow other related articles on the PHP Chinese website!