Redis key and value practice summary

This article mainly summarizes some good practical experience in using Redis over the years of work, hoping to give you a little inspiration or help.

One key usage specification

1.1 How to name the key?

Consider from the perspective of simplicity and readability, such as: service name + business module + business data.

1.2 How long is the key suitable?

On the premise of a complete description of the business, the shorter the key, the better. Why? Does the key take up memory if it grows longer?

1.3 What is a hot key?

The so-called hot key is a sudden large number of requests to access a particular key on redis, and this particular key is the hot key. Remember when XX stars got married and XX stars got divorced.

1.4 What are the effects of hot keys?

Due to a large number of requests for access, it is easy to cause the traffic to be too concentrated and reach the upper limit of the physical network card, which causes the redis server to go down. Because there is no redis anti-stress, the mysql server will be down.

1.5 How to avoid hot keys?

The key naming rules should be as random as possible; increase the monitoring of the key; increase the second-level cache, the so-called second-level cache is the JVM cache.

Two value usage specifications

2.1 Avoid big keys

The so-called big key means that the value stored by the key is very large.

Why do we need to avoid big keys?

When the application server fetches the value from Redis, if this block is a big key, the network card traffic of the Redis block will be very large, affecting performance.

Does big key affect Redis performance?

The bottom layer of Redis is a single-threaded model. Assuming that there is no big key, it can process 100,000 tasks per second; with a big key, it may process 10,000 tasks per second, or even lower. Do you think it affects performance?

More serious may be blocked, which is by no means alarmist.

How big is the big key?

The specific size is determined from the business level. Generally speaking:

  • The maximum string type does not exceed 512M, and the size in actual development does not exceed 10k.
  • The size of hash, list, set, and zset should not exceed 100k.

2.2 Set expiration time to value.

Why do I need to set an expiration time for value?

If you don't set an expiration time for value, value will stay in memory. The memory really didn't come from a strong wind.

How long is the expiration time appropriate?

Generally speaking, it depends on your own business and can be set from the hit rate of the key. Different keys set different expiration times.

Why do different keys need to set different expiration times ?

If a large number of keys have the same expiration time, a large number of keys will expire in a certain period of time, which is prone to an avalanche.

How to avoid an avalanche that causes Redis to be unavailable?

You can set different expiration times for different keys. The same key can also generate a random number as the expiration time.

2.3 Value data cold and hot separation

What is cold and hot separation?

The so-called hot and cold is from the point of view of data access, that is, the more accessed data is called hot data, and the less accessed data is called cold data.

Why do we need to separate hot and cold?

Cold data is rarely accessed, and it takes up memory in Redis, which is a bit wasteful, and the memory is indeed not windy.

How to practice cold and heat separation?

This part involves: Is it to cache all attributes or some attributes?

Suppose we retrieve 30 fields from the database, of which about 10 are commonly used in business, and the remaining 20 are not commonly used. I don't know when we will use them.

How to flexibly and quickly support business changes while ensuring the separation of hot and cold data?

In order to solve this problem and realize the separation of cold and heat, we provide two sets of interfaces:

A set of interfaces caches the entire amount of data in order to avoid various development problems that require adding this field and adding that field in the business, and has a certain versatility and flexibility.

A set of interfaces is to cache common business data, which not only meets business needs, but also reduces memory space usage.

Three Redis data isolation

What is data environment isolation?

It means that the pre-released key and value and the online key and value each have a set, and they do not affect each other.

Why do you need data environment isolation?

I have encountered a case where a problem occurred on the user line due to a misoperation in the pre-release. So in terms of user experience and data security,

Pre-release and online data isolation is in line with business requirements. Even if there will be two pieces of data, I think it is ok.

Four Redis capacity evaluation

How to apply for Redis capacity?

From the current business perspective, for example, how many active users are there in total, how many active users are there on average every day, and how much memory each key and value occupies. The multiplication of the two is roughly the memory that needs to be applied for.

Just apply for memory, is it enough?

It may not be enough, because the business is changing, we should consider the problem with a development perspective, we can reserve 50% or double the capacity to support the rapid development of the business, so there is no need to expand the capacity frequently.

What should we do when expanding the capacity?

If the memory needs to be expanded, you can communicate with the DBA about the business scenario and evaluate the capacity. Jointly promote the perfect implementation of expansion. DBAs are professional, and it doesn't hurt to listen to their suggestions.

Guess you like

Origin blog.csdn.net/jack1liu/article/details/112388895