A preface

This article is a preliminary understanding of redis String data structure SDS (Simple Dynamic String), which means Simple Dynamic String, literally meaning is smiple generation refers to Simple, Simple operation, users can quickly understand the use, do not need to care about redis internal implementation; Dynamic refers to the Dynamic expansion, the table is able to automatically dynamically allocate memory space; String is a String, it’s easy to understand;

Public account: Knowledge seeker

Inheriting the spirit of open Source, Spreading technology knowledge;

2 SDS structure

2.1 Redis SDS data structure

The data structure prior to redis3.2 is as follows;

struct sdshdr {
    unsigned int len;   
    unsigned int free;  
    char buf[];         
};
Copy the code
  • Len represents the length of space already used in buF (buffer);
  • Free represents the unused length in buF;
  • Buf [] stands for buffer array, storing characters;

2.12 Redis buffer structure

The actual size of the buF image is 11 (len + free + 1), where len =5 for used space and free=5 for unused space; Reserved space character \0 occupies one bit; When we store the string ZXZXZ in redis, we have allocated the memory space, and the memory space we can use afterwards; In C, the length of a ZXZ character is calculated by iterating through the array and ending with \0 (c uses \0 to distinguish strings in memory space). However, Redis only needs an SDSLen (non-C readers don’t have to worry about such apis) to calculate the length of the string; From the perspective of algorithm, the length of a string acquired by Redis is O(1), and the c language is O(N), so Redis is much faster.

2.2 Redis space allocation strategy

Secondly, it can be found from the figure above that a string ZXZXZ is stored, which occupies a length of 5, a space of 5, and \0 occupies 1. The reason is that when the redis string storage size is less than 1MB, the free size of any string stored is always the same as its own size. If the size of a string is greater than 1MB, free is allocated to a fixed size of 1MB. This is called the space preallocation policy. In THE case of C, 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 space. Therefore, the speed of Redis is quite fast, compared with C to operate memory space;

C language in the operation of the memory space to constantly calculate the size, allocate the space when the string is allocated, if not allocated, then the string may overwrite the string has been stored in the memory space; For example, memory space for storage. If you append a GGG to ZZZ string, then the original data will become ZZZGGG \ 0K \0 without calculating the allocated space, which is very easy to find that the memory overflow. The first string overwrites part of the second string;

Therefore, redis operation content space is to prevent memory overflow, and can store images, videos and other binary data, if c language operation storage, a \0 binary file may lead to memory leakage, buffer overflow, etc., so C language generally only operate text files;

Three related links

If you want to dig into redis SDS source code, please refer to the following link;

Blog.csdn.net/yangbodong2…

Juejin. Cn/post / 684490…

Blog.csdn.net/qq193423571…

Lynnapan. Making. IO / 2017/07/14 /…

Focus on knowledge seekers: