Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The Redis specification, which is probably the most pertinent


Jun 01, 2021 Article blog


Table of contents


This article was reproduced from the public number: Little Sister Taste

Redis is powerful, data-rich, and fast systems can't stand crazy abuse. By disabling some of the high-risk features and attaching the shackles of development, the business is better able to think about issues with a concise, generic mindset than to be tied to an implementation.

Redis have different persistence and eviction policies for different purposes, so before you use and apply for a Redis cluster, make it clear whether it is used for caching or storage. R edis clusters have both master and cluster modes, each with advantages and disadvantages. The following specifications do not distinguish between cluster patterns, which we explain in terms of usage scenarios and operational limitations, respectively.

Use the specification

Hot and cold data distinction

Although redis supports persistence, storing all data in redis can be expensive. I t is recommended that data from hot data, such as QPS exceeding 5k, be loaded into redis. Low-frequency data can be stored in Mysql ElasticSearch中

Separation of business data

Don't put unrelated data services in one Redis On the one hand, avoid business interaction, on the other hand, avoid single-instance expansion, and can reduce the impact surface in the event of failure, rapid recovery.

Message size limit

Because Redis is a single-threaded service, messages are blocked over the general assembly and other operations are slowed down. I t's a good habit to keep message content below 1KB. N o more than 50KB of a single record is strictly prohibited. Too large a message can 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 wastes a lot of system resources, and extreme situations can cause hosting to crash. Make sure that the correct Redis client connection pool configuration is used.

Cache Key sets the expiration time

Key which is used as a cache, must set an expiration time. T he longer the failure time, the better, depending on the nature of the business. Note that the failure time of the unit is seconds, some milliseconds, this many students do not pay attention to easy to make mistakes.

The cache cannot have a middle state

The cache should only be used for caching purposes, and the business logic should not change after removal and must not be cut into the business. First, the high availability of caches can affect the business;

The extension method prefers client hash

Small apps are gone

If a single redis cluster doesn't serve your data, don't rush to expand your redis cluster (including M/S and Cluster), the larger the cluster, the worse the performance in terms of state synchronization and persistence. P riority is given to cluster splitting using client hash For example, depending on the user id of 10 clusters, the user's tail number of 0 falls on the first cluster.

Operational restrictions

Keys is strictly prohibited

Keys are extremely inefficient, are O(N) operations, block other normal commands, and can be catastrophic on cluster It is strictly prohibited to use, DBA should rename this command, disabled from the root cause.

Flush is strictly prohibited

flush command emptys all data and is a high-risk operation. It is strictly prohibited to use, DBA should rename this command, disabled from root cause, only DBA can operate.

It is strictly prohibited to use as a message queue

Redis is strictly prohibited as a message queue if there are no very specific needs. Redis is used as a message queue and has a variety of capacity, network, efficiency, and functionality issues. If you need a message queue, you can use a high-throughput Kafka or a highly reliable RocketMQ

Bulk operations that do not set the range are strictly prohibited

redis is so fast that slow queries belong to these bulk operation functions, except for network latency. Most online problems are caused by these functions.

1, "zset" is strictly prohibited to zset's non-range operation

ZRANGE ZRANGEBYSCORE AND OTHER FUNCTIONS THAT OPERATE ZSET ARE STRICTLY PROHIBITED FROM USING SUCH OPERATIONS AS ZRANGE myzset 0 -1 WHICH DO NOT SET THE RANGE. S pecify a range, such as ZRANGE myzset 0 100 If the length is not known, you can use ZCARD to determine the length

2, the use of HGETALL for the big data volume Key is strictly prohibited

HGETALL takes out all data related HASH and, if the number of data bars is too large, can also cause congestion, making sure the business is under control. If you are not sure of the length, you can use HLEN to determine the length first

3, the mget operation of the Redis Cluster cluster

Redis Cluster MGET operations take data aggregations to each shard, and performance is much lower than traditional M/S architectures

4, the use of sunion, sinter, sdiff and other aggregation operations is strictly prohibited

Disable the select function

select function is used to switch database which is a problem-prone place for the user, and cluster mode does not support multiple database and does not have any benefit, disabled.

Disable transactions

redis itself is already fast, and if there is no need to do so, it is recommended to catch exceptions for rollback, not using transaction functions, which few people do.

Disable lua script extensions

lua scripts can do a lot of things that look cool but they're like a stored procedure for SQL introducing performance and some hard-to-maintain issues to disable.

Do not monitor for long periods of time

monitor function can quickly see the traffic that is currently being performed by redis but beware that blocking the monitor command for long monitor of time during peak periods can seriously affect the performance of redis This command is not prohibited, but it is important to pay special attention to use it.

Key specification

Redis Key must be regulated so that it can be easily positioned when problems arise. Redis belongs to scheme KV database, so we rely on conventions to establish its scheme semantics. Benefits:

  1. Ability to clean up data based on certain types of keys
  2. The ability to update data based on certain types of keys
  3. Be able to understand the attribution and scenario of a certain type of key
  4. Prepare for unification and platforming, and reduce technology changes

In general, a key needs to have the following dimensions: business, key purpose, variables, and so on, each dimension is separated by:

User:sex user 10002232 gender

msg:achi 201712 user number of speakers leaderboard

End

Proper constraints are necessary for the maturity of the architecture, and it is the highest level of collective development that can be achieved through agreement. Redis use more, but also to use stability, give some restraint, set some rules, life will become better. Direct blocking through secondary encapsulation of redis client works better.

These are W3Cschool编程狮 about the Redis specification, which is probably the most pertinent related introduction, I hope to help you.