Series of articles:
What does redis do with simple dynamic string SDS
A List of what redis does at the bottom
What Hash is redis doing underneath
This machine uses redis related commands
redis-server ./etc/redis.conf
Copy the code
Start Redis from this configuration file
ps -ef | grep redis
Copy the code
Check the background Redis process
Redis-cli -v or redis-server -vCopy the code
Check the version number of Redis
redis-cli -h host -p port -a password
Copy the code
Connect to remote Redis through redis client
Simple dynamic string SDS
What is the SDS
Redis is written in C language. It is the traditional string structure of C language. However, Redis uses an abstract type named Simple Dynamic String (SDS) as the default string structure of Redis.
C String structure
C uses an array of characters of length N+1 to represent a string of length N, and the last element of the array is always a null character ‘\0’. C uses the null character to determine whether a string is the end.
The structure of the SDS
SDSHDR is the name of the structure
Free is the amount of memory that SDS allocates, but does not use
Len is the length of the array of strings held by the SDS. Note that this length does not include the last null character ‘/0’.
Buf is a char array that contains the details of SDS, and the last byte holds the null character ‘/0’
The blank character ‘/0’ at the end of SDS does not count in len length because it is added to SDS as a function to reuse part of the C string. Simultaneously allocating an extra 1 byte of space to the SDS null characters and adding the null characters to the character array is done automatically by the SDS function. Most importantly,SDS does not distinguish between strings by this null character, so the null character is of no concern to the consumer.
SDS strings are designed to benefit from this
1. Constant complexity Gets the length of the string
C string gets the length of the string by iterating through the string, counting every character encountered until it encounters the null character ‘/0’, which represents the cutoff. SDS gets the length of the string directly from the len attribute, which SDS automatically sets when it creates or updates Therefore, SDS is used to reduce the complexity of obtaining strings from O(N) to O(1). Redis keys must be strings, using SDS. So we execute STRLEN on a long string key without any impact on system performance, ensuring that fetching string length does not become a performance bottleneck for Redis.
2. Prevent buffer overflow
C string itself does not record its length. If the required memory is not allocated in advance and the string is pieced together directly, the problem of buffer overflow may occur. When SDS modifies a string, it first checks whether the SDS space meets the modification requirements (according to len and free attributes), and then performs the actual modification, eliminating the possibility of buffer overflow.
3. Reduced the number of memory reallocations caused by string changes
C string Memory needs to be reallocated N times after changing the string SDS uses the len and free attributes to reallocate up to N times
1. Pre-allocate space
Optimizes the SDS string growth operation. When the SDS string length is increased and memory must be expanded, there are two cases:
If the length modified by SDS is less than 1Mb, the free attribute of SDS will be assigned a value the same as the current len value, indicating that half of the memory space obtained by SDS is used and the other half is vacant and waiting to be used. If the SDS is modified again at this time, if the space of free+ Len is enough to meet the modified result, it will not be The space needs to be allocated again, reducing the number of times the space needs to be allocated once.
If the SDS modified length is greater than or equal to 1Mb, 1Mb of unused space is assigned to the FREE property of the SDS. Using a space preallocation strategy, Redis can reduce the number of memory allocations required to continuously perform string growth, up to N times if the string is modified N times.
2. Lazy space release
It is used to optimize shortening of SDS strings. When shortening SDS strings is needed, memory does not immediately reclaim the extra space, but uses the free property to record the extra space and wait for future use.
For example, len of an SDS is 13 and free is 0, len of a shortened SDS is 7 and free is 6, and there is no memory reallocation in this process.
SDS provides an API to truly free up SDS free space without worrying about memory waste.
4. Binary storage
The C string ends with the null character ‘/0’, which distinguishes different strings. As a result, the C string can only store text data, but cannot store binary data such as pictures, audio, videos, and compressed files.
For example this binary data Redis cluster,C string can only recognize Redis SDS to distinguish different strings by len attribute, so can save binary data in any format.