In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
When we need to use distributed cache Redis in the development process, we first need to understand what caching is used in the system.
A small amount of data storage, high-speed read and write access. To ensure high-speed access through all the data in-momery, while providing the function of data landing, in fact, this is the most important application scenario of Redis.
Massive data storage, distributed system support, data consistency guarantee, convenient cluster nodes to add / delete. Since then, Redis3.0 has started to support clusters and implemented semi-automatic data sharding, but it needs the support of smart-client.
Huawei Cloud distributed cache Redis has entered the public trial phase of Redis5.0. Registration in the public trial phase can be used free of charge, so what problems do we need to understand when we need to use Redisde in the development process?
Next, the editor will tell you one by one.
First, why use Redis?
I think using Redis in a project is mainly considered from two perspectives: performance and concurrency.
Of course, Redis also has other functions such as distributed locks, but if it is only for distributed locks and other functions, there are other middleware, such as ZooKpeer, instead of Redis. Therefore, this question is mainly answered from the perspectives of performance and concurrency.
Performance:
As shown in the following figure, when we encounter a SQL that takes a long time to execute and the results do not change frequently, it is particularly suitable to cache the run results. In this way, the subsequent request is read in the cache so that the request can respond quickly.
Digression: I suddenly want to talk about this standard of rapid response. There is no fixed standard for this response time depending on the effect of the interaction.
But someone once told me: "ideally, our page jump needs to be solved in an instant, and the operation on the page needs to be solved in an instant."
In addition, the time-consuming operation of more than one finger should have a progress prompt, and can be suspended or cancelled at any time, so as to give the user the best experience. "
So how much time is the moment, the moment, the flick of a finger?
According to the Maha Monk Law:
An instant is a thought, twenty thoughts are an instant, twenty moments are a flick of a finger, twenty fingers are a stroke, twenty strokes are a moment, and there are thirty moments in a day and a night.
So, after careful calculation, it is 0.36s in a moment, 0.018 seconds in a moment, and 7.2s in a finger.
Concurrency:
As shown in the following figure, in the case of large concurrency, all requests directly access the database, and the database will have a connection exception.
At this point, you need to use Redis to do a buffering operation so that the request accesses the Redis first instead of directly accessing the database.
What are the disadvantages of using Redis?
You must understand this problem when you use Redis for so long. Basically, you will encounter some problems when using Redis, and there are only a few common ones.
The answers are mainly four questions:
Double write consistency between ◆ cache and database
◆ cache avalanche problem
◆ cache breakdown problem
Concurrency contention of ◆ caches
I personally think that these four problems are often encountered in the project, and the specific solutions are given later.
Third, why is single-threaded Redis so fast?
This problem is an investigation of the internal mechanism of Redis. According to my interview experience, many people don't know that Redis is a single-threaded work model. Therefore, this question should be reviewed.
The answer is mainly the following three points:
◆ pure memory operation
◆ single-thread operation avoids frequent context switching
◆ adopts non-blocking Istroke O multiplexing mechanism.
Digression: now we have to talk in detail about the Istroke O multiplexing mechanism, because this statement is so popular that most people don't understand what it means.
Let's take an example: Xiaoqu opened a delivery store in S City, which is responsible for the same city express service. Xiaoqu hired a group of couriers because of financial constraints, and then Xiaoqu found that there was not enough money to buy a car for express delivery.
Mode of operation 1:
Every time a customer sends a delivery, Xiaoqu lets a courier keep an eye on it, and then the courier drives to deliver the delivery.
Slowly Xiaoqu found the following problems with this mode of operation:
Dozens of couriers in ◆ basically spend their time robbing cars, and most couriers are idle. Whoever gets the car will be able to deliver it.
◆ with the increase of express delivery, there are more and more couriers, Xiaoqu found that the express store is becoming more and more crowded, there is no way to hire new couriers.
Coordination between ◆ couriers takes a lot of time.
Combining the above shortcomings, Xiaoqu learned from the bitter experience and put forward the following mode of operation.
Mode of operation 2:
Xiaoqu only employs one courier. Then, for the express delivery sent by the customer, Xiaoqu is marked according to the place of delivery, and then placed in one place in turn.
Finally, the courier went to pick up the express delivery in turn, one at a time, then drove to deliver the express delivery, and then came back to get the next delivery.
Comparing the above two modes of operation, do you obviously feel that the second mode of operation is more efficient and better?
In the above analogy:
◆ every courier → every thread
◆ every courier → every Socket (Izod O stream)
Different states of ◆ express delivery place → Socket
◆ customer express delivery request → request from the client
The mode of operation of ◆ Xiaoqu the code running on the → server
◆ the audit of a car → CPU
So we have the following conclusion:
The first mode of operation is the traditional concurrency model, in which each Imax O stream (express) has a new thread (courier) management.
The second mode of operation is Ihammer O multiplexing. There is only a single thread (a courier) that manages multiple Ipicot O streams by tracking the status of each Ipicuro stream (the place of delivery of each delivery).
The following analogy to the real Redis threading model is shown in the figure:
To put it simply, our redis-client will produce Socket with different event types when operating.
On the server side, there is an Imax O multiplexer program that places it in the queue. Then, the file event dispatcher takes it from the queue in turn and forwards it to different event handlers.
It should be noted that the Redis O multiplexing mechanism, Redis also provides select, epoll, evport, kqueue and other multiplexing function libraries, you can understand.
Of course, the above three points are only superficial, but they are the most basic. If you want to know more detailed operating rules, you can go to Huawei Cloud official Forum to exchange experiences with all kinds of big players. Big names will certainly learn a lot.
The content of the article is reprinted to the official account "Heart of procedure".
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.