Author: Enjoy learning classroom old gu
Wechat official account: Enjoy Learning class online
One, foreword
Sometimes we need to know the use of online Redis, especially need to know some prefix key value, let us how to check? And usually the data in Redis is massive, so we access the massive data in Redis? How to avoid accidents! Today to share a small knowledge point, I hope you spray.
Ii. Occurrence of accidents
Because our user token cache uses [user_Token: userID] key format to store the user’s token value. In order to help developers, we have to check how many users are logged in online.
Keys user_token* is used directly to query, and this is where the accident occurred. Cause Redis to be unusable and fake death.
Third, analyze the reasons
There are millions of users logged in online, so there is a lot of data. Keys algorithm is traversal algorithm, complexity is O(n), that is, the more data, the higher the time.
If the data volume reaches millions, keys will cause the Redis service to stall. Because Redis is a single-threaded program, all instructions are executed sequentially, and other instructions must wait until the current keys command is finished.
Iv. Solutions
So how do we traverse big data? This is also a common interview question. We can use the other Redis command scan. Let’s look at the features of SCAN:
-
The complexity is also O(n), but it is done in steps by cursor and does not block the thread
-
Provide count, not the number of results, but the number of dictionary slots Redis traversed in a single pass (approximately equal to)
-
Like Keys, it provides pattern matching;
-
The server does not need to store state for the cursor. The only state of the cursor is the cursor integer returned by SCAN to the client.
-
It is important that the results returned may be duplicated and need to be repeated by the client;
-
A single return that is empty does not mean the traversal is over, but that the returned cursor value is zero
4.1 Format of scan command
4.2 Command interpretation
Scan cursor MATCH < returns elements that MATCH the given pattern > count Number of elements returned in each iteration
-
The SCAN command is an incremental loop, returning only a small number of elements per call. So Redis won’t fake his death;
-
The SCAN command returns a cursor that starts at 0 and ends at 0.
4.3, for example,
Start at 0, return cursor 6, return data, continue scan, start at 6
Five, the summary
This is often asked in interviews, and is often used by our partners in the process of work. Generally, when the amount of data is not large, there will be no problem, but when the amount of data is large, your operation mode is not correct, your performance will be deducted.
You can quit your job! , from the point of view of regex matching introduced a wrong use of Redis, you can also have a look!
Original address:
www.toutiao.com/i6697540366528152077
Popular content:
1. Why are some big companies too weak?
55 Must-know Java Performance tuning details! Catch them all!
3. Interview question: How many rows can a B+ tree store in InnoDB?
Interviewer: what do you know about SpringAOP? Please add these content, absolute extra points!
5. How to optimize cache architecture when 200,000 users access a hot Key at the same time?
6, multithreading, exactly how many threads should be set?
7. How to deploy Tomcat in a production environment?
God level code annotation appreciation, like to take to use!
Java backend technology (ID: JavaITWork), learn Java with 200,000 people! Reply:1024, you can get it for free! Includes SSM, Spring family bucket, microservices, MySQL, MyCat, cluster, distributed, middleware, Linux, network, multi-threading, Jenkins, Nexus, Docker, ELK and so on free learning video, continue to update!