In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what is the data structure of redis SDS". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
A preface
Redis's string data structure SDS (Simple Dynamic String), which means a simple dynamic string, literally means that smiple refers to simple, easy to operate, and users can quickly understand without worrying about the internal implementation of redis; Dynamic refers to dynamic expansion, and tables can automatically dynamically allocate memory space; String represents strings, which is not difficult to understand.
Two SDS structure 2.1redis SDS data structure
The data structure before redis3.2 is as follows
Struct sdshdr {unsigned int len; unsigned int free; char buf [];}
Len represents the length of space already used in the buf (buffer)
Free represents an unused length in buf
Buf [] represents an array of buffers, storing characters
2.12 redis buffer structure
A more vivid storage image is as follows: the actual size in buf is 11 (len + free + 1), where the space len = 5 has been used, and the unused space free=5; reserves a bit of empty character\ 0; when we store a string zxzxz in redis, we have already allocated the memory space and the memory space that can be used later. If it is a c language, then to get a zxzxz character length, you need to traverse the entire character array and end with\ 0 (C language distinguishes the string in memory space by\ 0) before calculating the length of a string. However, redis only needs a sdslen (non-c language readers do not have to struggle with this kind of API) to calculate the string length. From an algorithm point of view, the string length of redis is O (1), and the c language is O (N), so redis is much faster.
2.2 redis space allocation strategy
Secondly, through the above figure, we can find that the length of a string zxzxz is 5, and the usage space is 5,\ 0 accounts for 1. The reason is that when the storage size of redis string is smaller than 1MB, any string will be stored, and its free size will always be the same as its own size. When the string size is larger than 1MB, the free size will be fixed to 1MB, which is called space pre-allocation strategy. If it is a c language, you need to calculate the length of the current string in buf, then calculate the length of the string to be appended, and then allocate the size of the space. Therefore, the speed of redis is quite fast, compared with the memory space of c operation.
C language constantly calculates the size when operating the memory space, allocates the size of the space when appending a string, and if it is not allocated, then the appended string may overwrite the string that has been stored in the memory space. For example, the memory space is stored in zzz\ 0kkk\ 0; the storage of zzz takes 3 bits, plus 1 bit of unallocated space. If a ggg is appended to the zzz string, the original data will become zzzggg\ 0k\ 0 without calculating the allocated space. It is intuitive to find the memory overflow, and the first string covers part of the second string.
Therefore, the operation content space of redis is to put an end to memory overflow, and it can store binary data such as pictures and videos. If it is stored in c language, a\ 0 in binary files may cause memory leaks, buffer overflows, etc., so c language generally only operates text files.
This is the end of the content of "what is the data structure of redis SDS". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.