Back to Knowledge Hub

    What is Redis SDS and Why is it Better Than C Strings?

    Redis
    Data Structures
    SDS
    String Optimization

    What is SDS?

    SDS (Simple Dynamic String) is Redis's custom string implementation, designed specifically for high-performance scenarios. As Redis's most fundamental data structure, it underpins all key-value storage and list elements. Its design philosophy can be summarized in three keywords: safety, efficiency, and flexibility.

    SDS Internal Structure

    SDS Structure Diagram

    Each SDS contains three key metadata elements:

    1. Length Field (len): Real-time tracking of current string length (8 bytes)
    2. Free Space (free): Pre-allocated unused buffer size (8 bytes)
    3. Character Array (buf): Flexible array for actual content storage

    This design creates a three-part memory layout: header metadata + data area + reserved space. Here's an example storing "Hello":

    len=5          ← Current string length
    free=3         ← Available free space
    buf=['H','e','l','l','o','\0','',''] ← Actual storage (includes null terminator and reserved space)
    

    Core Advantages

    1. Fast Length Access

    While traditional C strings require traversing the entire array to get the length (O(n) time complexity), SDS directly reads the len attribute (O(1)). When accessing LIST and other data structures, this optimization can improve performance by over 100x.

    2. Buffer Overflow Prevention

    SDS's space pre-allocation strategy completely eliminates C string overflow risks:

    • For strings < 1MB: Doubles capacity during expansion
    • For strings ≥ 1MB: Adds 1MB extra space per expansion

    This mechanism reduces memory reallocation frequency by over 50% when handling APPEND operations.

    3. Binary Safety

    SDS can store any binary data (including content with '\0' characters), enabling Redis to safely store:

    • Binary image data
    • Serialized Protobuf messages
    • PDF file segments

    In contrast, C strings truncate at '\0', causing data corruption.

    4. C String Compatibility

    While SDS functionally surpasses C strings, it maintains '\0' termination, allowing it to:

    • Directly use C string library functions
    • Seamlessly integrate with existing system APIs
    • Reduce developer learning curve

    5. Memory Optimization

    Through lazy space reclamation, SDS doesn't immediately free memory when shortening strings. Instead, it tracks excess space in the free field. This space can be reused during future expansions, reducing memory allocations by 30-50%.

    6. Type Classification

    Redis implements 5 SDS types (sdshdr5~sdshdr64) for different string lengths, minimizing memory overhead through differentiated metadata headers:

    TypeLength RangeHeader Size
    sdshdr50-31 bytes1 byte
    sdshdr832-255 bytes3 bytes
    sdshdr16256-65535 bytes5 bytes
    sdshdr3265536-4294967295 bytes9 bytes
    sdshdr64Extra long strings17 bytes

    SDS Applications in Redis

    1. Key-Value Storage: All Redis keys are SDS type
    2. List Elements: LIST structure elements rely on SDS
    3. Persistence Buffer: AOF/RDB file writing uses SDS as buffer
    4. Network Protocol: RESP protocol parsing is implemented with SDS

    Related Resources: