British Fleming once said: “do not wait for luck, should strive to master knowledge.”
1 introduction
Hi, I’m Mu! Redis is the most familiar, as the cache world has been far ahead of the curve. Basically the entire Internet no matter big or small companies use Redis accounts for the vast majority, so many people use it, that is just to use it, for its use scenarios and did not pay attention to too much (can be used on the line), this article will talk about the basic data structure of Redis string.
Redis has five basic data structures: string, hash, set, zset, and list. These five data structures cover the whole knowledge point of REDIS, which is the most important but also the most basic knowledge point of REDIS. Of course, you will often encounter the basics of Redis in the interview. Where are its pits? How to solve it? Ah Mu will use the roles of the interviewer and the interviewer to explain questions and answers one by one.
2. State of mind at the beginning of the interview
Interviewer: a young, bespectacled interviewer (26,27 years old) walked slowly up to me, holding my resume in his hand and glancing up at me from time to time.
Interviewee: “Oh, stop looking at me. I’m just a rookie with one year’s experience. My heart is freaking out and my legs are shaking. What would he ask me, and how would I smoothly respond? I was like, really, I’m just a kid. Let the interviewer go and make it simple.”
Interviewer: “Come into the conference room and sit across from me.” Why don’t you tell me a little bit about yourself?
Interviewer: Hello, interviewer. My name is A mu, from Henan province. I graduated from Zhengzhou University. I have been working for half a year in guangzhou XXX Company as a back-end development engineer, mainly responsible for search, shopping cart, product modules and so on. In my spare time, I like XXXX (omitted 200 words)……
Interviewer: Well, after looking at your resume, let’s talk about Redis briefly. Most of your projects are related to Redis. Let’s talk about some basic redis string directives.
The interview began to get down to business
It’s a piece of cake.
-
Description: A string is a simple data structure. It is usually stored in the form of a key and a value. Its internal storage is an array of characters. The maximum length of a Redis string is 512 megabytes, and the storage is dynamic (meaning it can change its value at any time). Each time the length of the Redis string is allocated higher than the actual length of the string, which is used to reduce the frequent allocation of memory by pre-allocating redundant space.
-
Commonly used commands:
Set - Sets the key value; If key exists, override the old value, regardless of type, and return OK on success. If key is not present, return nil(not null, do not make a mistake) mset - same as set, set key/value pairs in batches, reduce network overhead mget - Same as get, obtain key/value pairs in batches, reduce network overhead incr-key value +1, if not present, set then incr, Return integer remember that the value must be identified as a number incrby - same as incR, with an additional increment of the specified number decr - same as INCR, the operation is changed to decrby - Same as incrby, The operation is changed to subtraction strlen - get the length len, return 0 if none is present, type integer setnx - Set the key value, return 1 if none is present, otherwise return 0. Setex - Set the key value and expire time. If the key already exists, replace the old value and overwrite it. (If a set value is required and an expiration time is required, this command is recommended. Setting the key value and expiration time is an atomic operation that either succeeds or fails.) Append - Appends the key value at the end and returns a string lengthCopy the code
Interviewer: nodded slightly, said in the heart: “Ah, homework done quite enough oh, to use and pay attention to the point of understanding so clear, from the interviewer’s affirmation, extra points.”
4 Usage scenarios of String
Interviewer: I knew you were going to ask me about scenarios. Fortunately, I was prepared enough. It’s time to show some real skills. Start the book with a face on your face: counting, caching basic data, limiting the number of requests, sharing sessions in a distributed way, checking in, etc. So I’ll follow these scenarios.
4.1 Caching basic data
For example, the basic cache data of login users can be cached (hash storage is recommended, for example). After login, users can assemble the basic data into JSON strings and set them in the cache. Users can request the cache layer to speed up read and write performance and reduce mysql query pressure. If the user updates the data, the cache is refreshed and synchronized. If the user table has a large number of data fields, the data can be divided into hot and cold data and stored separately to reduce the size of keys and reduce network overhead.
// instantiate redis
$redis = new Redis();
$redis->connect('127.0.0.1'.6379);
//echo "Server is running: " . $redis->ping();
$member_info = [
'member_id'= >1001.'member_nickname'= >'THIS is Mu.'.'member_email'= >'[email protected]'.'member_phone'= >'12345678998'.'member_qq'= >'[email protected]'.'member_level'= >120,];$key = sprintf('member:info:id:%u'.$member_info['member_id']);
$expire = 60;
// Set the cache and 60s expiration
$result = $redis->set($key, json_encode($member_info), $expire);
//$redis->set($key, json_encode($member_info), ['nx', 'ex' => 60]);
if (!$result) {
exit( "Uh, setup failed.");
}
echo "Wow, it worked.";
-- 终端 get
127.0.0.1:6379> get member:info:id:1001
"{\"member_id\":1001,\"member_nickname\":\"\\u6211\\u662f\\u963f\\u6c90\",\"member_email\":\"[email protected]\",\"me mber_phone\":\"12345678998\",\"member_qq\":\"[email protected]\",\"member_level\":120}"
127.0.0.1:6379> get member:info:id:1001(nil) -- Remember that no data returns nilCopy the code
4.2 the counter
The counting function is often used to count the amount of data visited on a page. For example, I used to work in an e-commerce company, and the product would often ask us to do the page views of the activity and the number of product details opened in the activity, and analyze the product audience in the activity through PV data. Of course, it will also count the number of hits on the video on the product details page, the total number of calls to the open API per day, and so on. Write code quickly on paper:
<? $redis = new redis (); $redis - > connect (127.0.0.1, 6379); //echo "Server is running: " . $redis->ping(); $key = sprintf('product:detail:pid:%u', 12345); $result = $redis->incr($key); //redis terminal 127.0.0.1:6379> incr product:detail:pid:12345 (integer) 2 127.0.0.1:6379 > get the product: the detail: pid: 12345 "2"Copy the code
4.3 Limiting the Number of requests
Limit access times, this is generally used to control a malicious person using abnormal manual brush interface or malicious damage to our system services, for some more sensitive, more important interface to do the upper limit flow measures. For example: the website uses the mobile phone number to register, the general company SMS send is to use the third party, is to give money, love dearly! It’s 10 cents per click. The heart is bleeding. Although the client has made the verification restriction, it cannot avoid the packet capture simulation request. At this time, we need to make the traffic limiting request for these users or IP addresses, and use INCR + EXPIRE to handle it. Hand tear pseudo-code is as follows:
// instantiate redis
$redis = new Redis();
$redis->connect('127.0.0.1'.6379);
//echo "Server is running: " . $redis->ping();
$key = sprintf('member:login:phone:count:%u'.12345678998);
$maxCount = 10;
-2 The key is not set. -1 The key does not set the expiration time
$ttl = $redis->ttl($key);
if ($ttl > 0) {
$times = $redis->get($key);
if ($times> =$maxCount) return true;
$result = $redis->incr($key);
if ($result) return true;
} else {
$result = $redis->set($key.1.60);
if ($result) return true;
}
return false;
Copy the code
4.4 Distributed Shared Session
Interviewee: “I was secretly glad that I had an internship in my last company, otherwise I would really die.” Here we go: At the beginning, due to the small number of users, the system was generally supported by a single machine. After logging in, users were stored in session(server file storage). Then, as our business grew larger and larger, the number of users surged to millions, at this time, we changed from single machine to cluster, and ngxin was used for load balancing. The requests of each user will be loaded to each server, which is used to share the pressure of the machine and ensure the stability of service.
At this time, it will be found that the user login request is randomly sent to a certain machine, at this time, the session file is generated, but when the user accesses other pages, it is randomly sent to another server, at this time, the session cannot be obtained, the user login request will be intercepted, resulting in the user may have to log in many times. That’s not going to work. A very unfriendly user experience is going to get soaked in blood.
This is where distributed caching can solve the problem. Using Redis to centrally manage user Session information, each user login information is centrally obtained from Redis, so as to solve this problem perfectly. We can set php.ini:
Save_handler = redis -- Save mode session.save_path =" TCP :// 127.0.0.12:6379 "-- Save pathCopy the code
Interviewer: “This guy is so well-prepared, well-organized. I think I need to spice him up a little bit. What other solutions can be used to manage sessions in load balancing clusters besides redis?
Interviewer: “in the heart secretly sweet: shout, this is with me on the bar, ask of almost not on the line, but also have to go deep to ask; I have more than a year of experience of the people, so hard on me, please forgive the younger brother! But I’m new. I’m new. Well, now that you’ve asked me about this, I’m going to tell you what I’ve learned:
In addition to using Redis to store sessions, we can also consider a tradeoff:
-
Session Session persistence – Means that each request is made by the user on the same machine
-
Session Session replication – Replicates session information from each application server to other server nodes
4.4-1 Session Session retention
-1. Use “ip_hash” or “url_hash” (requires additional install) while using upstream in nginx load balancing. The hash result set of each user request IP address is assigned to a fixed server request so that the user is always logged in. For example:
upstream"Bakend" is the name of upstream and the address of the proxy_pass reverse proxy ip_hash.server 192.168.0.1:80;
server 192.168.0.2:80;
server 192.168.0.3:80;
}
Copy the code
2. There are three ways to use HaProxy to maintain sessions for load balancing:
-
① Ip_hash recognition algorithm similar to Nginx
-
(2) Cookie recognition, HaProxy inserts a cookie in the browser after the user visits for the first time, and the browser will bring the cookie to HaProxy for identification and analysis when the user visits for the next time
-
(3) HaProxy stores the session generated by the backend server and the backend server id in a table in HaProxy, and queries this table for identification when requesting. (If you are interested, you can Google HaProxy by yourself.)
Disadvantages of session persistence:
1. Where load balancing is useless: In the face of heavy traffic and high concurrency, we use Nginx as load balancing to reduce service pressure; Therefore, session persistence obviously abandons this advantage. Each user requires a certain machine, and there may be a waste of multiple machine resources, while some machines are under too much pressure and load, and tend to the single-machine era.
Failure to resolve session issues: If a server suddenly goes down or fails to connect due to timeout, nginx’s high availability will kick out the server and reassign requests to other machines, causing users to log in again.
4.4-1 Session Session replication
Session replication is a service mechanism for replicating data stored in sessions of different instances. In a cluster environment, session file data is synchronized between multiple servers to ensure that sessions on all servers are consistent and transparent. If a user logs in and requests are randomly assigned to any machine and the login information is available through the session session, or if a machine service fails and goes down, the NGINx load balancing scheduler will traverse the available nodes and distribute requests to other machines. However, the session has been synchronized, and the user will not be affected to log in again.
Cause: Session replication of a session may cause additional performance loss. If a large number of objects exist in a session, the session cache will be synchronized and the performance consumption will increase.
The final summary
Interviewer: Hello, interviewer. These are the types of strings I often use in my projects, along with their usage scenarios, analysis of scenarios, problems and solutions.
Interviewer: Well, it can be seen that the basic knowledge of Redis is very solid, and some of the commands often used are well analyzed and very careful. It shows that you are good at accumulating in your daily work, “a positive look from the interviewer”. So let’s move on to the next question (wait for the next chapter)…
From the analysis of the above interview situation, basically some commonly used instructions are covered and the introduction of the use of the scene; This is not necessarily the case, but when we actually use string directives, we need to consider our scenario. We need to decide the type to use according to the number of users or other aspects. We need to consider the amount of data set. So we’ll continue in the next chapter.
Hayao Miyazaki once said, “What you meet is fate, and what you have is luck. It doesn’t matter if you’re not perfect. Everything has cracks, and that’s where the light comes in.” Our study is also so, only the interviewer abuse me thousands of times, I can treat the interviewer as the first love; Imperfection is the opportunity for improvement.
Ok, I am a mu, a worker who does not want to be eliminated at the age of 30 ⛽️ ⛽️ college.