Home  >  Article  >  Database  >  What are the Redis specifications?

What are the Redis specifications?

王林
王林forward
2023-06-03 14:19:491234browse

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.

Usage specifications

Hot and cold data distinction

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.

Business data separation

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.

Message size limit

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.

Limit on the number of connections

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.

Cache Key Set expiration time

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 cannot have intermediate states

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.

The preferred extension method is client-side hash

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.

Operation restrictions

The use of Keys is strictly prohibited

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.

It is strictly prohibited to use Flush

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.

It is strictly prohibited to use it as a message queue

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.

Batch operations without setting a range are strictly prohibited

redis So fast and slow queries, except for network delays, belong to these batch operation functions. Most online problems are caused by these functions.

1. [zset] Unranged operations on zset are strictly prohibited

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

2. [hash] It is strictly prohibited to use HGETALL

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

3, [key] mget operation of Redis Cluster cluster

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

4 , [Others] It is strictly prohibited to use some aggregation operations such as sunion, sinter, sdiff, etc.
Disable the select function

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.

Disable transactions

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.

Disable lua script extension

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.

Prohibit long-term monitor

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.

Key specification

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:

  1. Can clean data based on a certain type of key

  2. Can update data based on a certain type of key

  3. Be able to understand the ownership and application scenarios of certain types of keys

  4. 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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete