preface: at present, the knowledge content related to redis has become a regular customer in the back-end interview. Mastering and understanding redis can greatly increase your interview scores. In addition, it is not advisable to memorize the standard answer to stand out in the interview. In this series, the small blackboard will be interview oriented, connecting the relevant knowledge of redis to help you to firmly grasp the small heart of the interviewer in the interview~
previously on In the last article, we discussed redis warm-up in the interview（No, please click here Redis interview routine dismantling (1)）And at the end of the article, we mentioned some questions about redis data structure, so let’s take a look at the angle from which the interviewer will ask questions, and how can we answer them better?
Don’t say much, let’s get to the point! Let’s simulate the real interview scene step by step. Readers can also try to answer and see what the interviewers are facing“Torture”What kind of answer would you give.
Q: You mentioned that redis provides rich data structures. What data structures do you know?
analysis: in fact, the basic data structure of redis（string、hash、list、set、 zset）We will certainly be familiar with it, but if we only answer the five basic data structures, but do not understand the advanced data structures, then the evaluation of the interviewers here must be very general.
Therefore, we need to add several high-level data structures. It should be noted that the things you mentioned must be well understood. The underlying implementation and principles should be mastered, and they should be able to explain clearlyOnly in this way can the interviewer feel bright~
A: Redis provides a very rich data type, including five basic data types:string、hash、list、set、 zsetIn addition, it also provides some advanced data structures, such as bitmap, hyperloglog and bloom filter
analysisIn fact, the points mentioned above will become the entry points for subsequent interviewers,In the interview, the data structures that interviewers prefer to investigate are string, Zset, bitmap, hyperloglog and blong filter. These are high-frequency test points. Mastering these data structures will become the bonus items in the interviewRedis mainly focuses on the structure of the blackboard~
Q: What about the type of string?
analysis:During the interview,Basically, the questions given by the interviewer are not very specificIt doesn’t specify which point you’re going to answer. It’s up to usexpandThe interviewer also wants to see how much you know about this knowledge point. This is a good opportunity to show off. As long as you master it well, you can deal with it calmly.
When it comes to the issue of string type, the following points need to be clarified:
- The first is stringUnderlying data structure
- The second point is stringStorage mode
- The third point is string typeExpansion mode
The second is the storage method of string typeString can be stored in two ways: embstr and raw
Embstr is used when the string length is short, and raw mode is used when the length is more than 44 bytes
Note: if you haven’t studied this aspect before, you may ask, what is embstr and what is raw? What’s the difference? Why 44 bytes? In fact, this may also be a question that the interviewer will go into. When you talk about the problems you can expand in the interview, you can take the initiative to ask the interviewer if you need to talk about it in depth, which is a good bonus item~
analysisEmbstr and raw are actually two different memory allocation methods. However, before we understand this knowledge, we need to know some preliminary knowledge: redis object header
The so-called redis object header is very similar to the Java object header, which is used to store object information. All redis objects have object headers. The data structure of the object header is as follows:
So a redisobject object header needs 16 bytes of storage space
The main difference between embstr and raw is that memory allocation of embstr is continuous, while raw is not
So why use 44 bytes as the dividing line? We can calculate that the minimum length of an SDS is when the generic t is byte type and the byte array is empty, there are capacity (1 byte), len (1 byte) and flag (1 byte), that is, 3 bytes in total, and the object header needs 16 bytes,So a string needs at least 19 bytes of memory
andThe memory allocator allocates memory to the power of 2For example, 2 / 4 / 8 / 16 / 32 / 64 bytes, so the memory allocator will allocate at least 32 bytes (16 < 19 < 32) for the string, and then it will be stored in embstr mode; when the total number of strings exceeds 64 bytes, redis considers it to be a large string and stores it in raw mode
When the size of the string object is 64 bytes, the maximum length of the actual character is 64-19-1 = 44 bytes. The last one byte is because the string is null terminated and occupies one byte（Null endings are used to facilitate the use of glibc’s string processing functions)
Therefore, when the actual length of the string is less than 44 bytes, embstr is used; when it is greater than 44 bytes, raw mode is used
Finally, the expansion strategy of stringWhen the redundant space of SDS is insufficient to store strings, it needs to be expanded. The expansion strategy is that when the string length is less than 1MB, the expansion strategy is adoptedThe doubling strategy is to double the existing space;When the string length exceeds 1MB,Each expansion will only allocate 1 MB more space
Note: redis specifies theThe maximum length is 512MB
The above content is about the string type in the interview need to pay special attention to, small blackboard hope this article can help you to sort out the ideas, check the missing, you can face the interview calmly~
The following small blackboard will continue to update the data structure series to help you dismantle the interview routine~
If you want to know what will happen, please point your fingers and pay attention to the small blackboard of back-end technology~