A brief discussion on the difference between Redis SDS and C strings
This article will introduce to you the difference between Redis SDS dynamic strings and C strings. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
#The bottom layer of redis does not use "C string" to represent it, but uses its own built "SDS abstract type" for default string representation. [Related recommendations: Redis video tutorial]
C string
The data stored in C string will have a null character at the end. For example, the actual form of "redis" is 'R' 'E' 'D' 'I' 'S' '\0'
SDS (dynamic string)
SDS is an abstract type built by redis, mainly used to store the default string representation of redis, the AOF buffer in the AOF module, and the client status input buffer.
SDS abstract type content includes:
int len, used to record the length of the string
int free, used To record the number of unused bytes in the buf array
char buf[], the byte array is used to save the string
SDS The structure is as shown in the figure below
Difference
1. When finding the length
C string needs to be traversed to know the length of the string, the complexity is O(n).
SDS only needs to access the internal len attribute, and the time complexity is O(1).
2. Buffer overflow problem
The C string sets an S1 as "redis", but there is an S2 adjacent to it as "abc" on the bottom layer, and then here If S1 is spliced to S3 "ccc" through the function strcat, the final result should be "redisccc". However, if the memory set for S1 is not enough, this will cause the S2 adjacent to it to be modified.
SDS will first determine whether the space is enough. If it is not enough, the space will be expanded to a sufficient location, and more free unused space of len length will be added. For example, the length of the redis string is 5, and then it will be added. The space is pre-allocated to the same length of 5, and the final actual space length is free len 1 to 10.
3. String memory allocation
c string, when data is added or reduced to a certain string, the memory will be re-applied. But if the Too many applications will inevitably lead to performance degradation, and if it is changed N times, it will be allocated N times.
SDS uses two mechanisms internally: lazy space release and space pre-allocation
Space pre-allocation:
Space pre-allocation: refers to when we perform a space allocation , we will allocate more space of len length on the original basis
Here, when the SDS length is less than 1M, it is free = len. For example, if the SDS length is 6byte, the actual space is 6byte 6byte 1byte
When it is larger than 1M, only 1M more will be allocated. free = 1M, for example, if the SDS length is 60M, the actual space is 60M 1M 1byte
Lazy space release
When we reduce a certain string At this time, the program does not immediately use memory reallocation to recycle the shortened bytes, but records them through free for subsequent use. SDS also provides corresponding APIs to prevent lazy space from causing memory waste.
4. Binary security
c The string is finally terminated by a null character, but if some special data requires a null character, the data cannot be saved. Will cause the data before the first null character to be recognized in advance. SDS Because SDS identifies strings based on length, it can ensure the accuracy of the data.
5. Compatible with some C language functions Because SDS also follows C's null character ending, it can use some functions in C
Summary
Record notes while studying, deepen Remember, keep it up. If you have any questions, please point them out. Thanks.
For more programming related knowledge, please visit: Programming Video! !
The above is the detailed content of A brief discussion on the difference between Redis SDS and C strings. For more information, please follow other related articles on the PHP Chinese website!

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

Redis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,

Key features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.

The core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool