Databases 8 min read

Understanding Redis String Implementation and Its Performance Advantages

This article explains Redis's custom Simple Dynamic String (SDS) implementation, its dynamic expansion mechanism, and the performance benefits such as O(1) length retrieval, buffer overflow avoidance, and efficient memory allocation strategies for high‑throughput applications.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Redis String Implementation and Its Performance Advantages

The author shares a personal interview experience where a lack of deep Redis knowledge led to a failed interview question about Redis strings, and then provides a detailed technical overview of Redis's string implementation.

The article covers two main topics:

Redis string implementation (Simple Dynamic String, SDS)

Performance advantages of Redis strings

Redis String Implementation

Although Redis is written in C, it does not use the native C string type. Instead, it defines its own string structure called Simple Dynamic String (SDS) to achieve higher speed and performance.

SDS is defined as:

struct sdshdr{
    // length of used part
    int len;
    // length of free (unused) part
    int free;
    // character buffer
    char[] buf;
};

SDS stores the used length explicitly, allowing O(1) retrieval of the string length, and follows the C‑string convention of terminating with a null byte that is not counted in len .

Dynamic Expansion Characteristics

SDS can dynamically grow. When additional data exceeds the current free space, Redis performs three steps:

Calculate whether the current allocation is sufficient.

Allocate enough space to accommodate the new size.

Allocate a free buffer: if len < 1M , allocate len bytes of free space; otherwise allocate a fixed 1 MB free buffer.

This mechanism is similar to how Java's ArrayList expands.

Performance Advantages of Redis Strings

Fast length retrieval (O(1) thanks to stored len ).

Prevention of buffer overflow.

Reduced number of memory allocations, improving memory‑use efficiency.

1. Fast Length Retrieval

Because len is stored in the SDS header, getting the string length is a simple field access with O(1) complexity, unlike C strings where strlen scans until the terminating null byte (O(n)).

2. Buffer‑Overflow Prevention

When concatenating C strings with strcat , the programmer must ensure enough space, or a buffer overflow occurs. Redis checks and expands the buffer before each append, eliminating this risk.

For example, appending "world" to a string that only has five free slots would trigger the expansion steps described above.

3. Efficient Memory Management

Redis employs two complementary strategies:

Space pre‑allocation : after an append, Redis not only allocates the exact needed size but also reserves additional free space ( free ) based on the new length, reducing future allocations.

Lazy space reclamation : when a string shrinks (e.g., via sdstrim ), Redis does not immediately free the reduced part; the space remains available for subsequent operations, and can be reclaimed manually via provided APIs.

These techniques lower allocation overhead and improve throughput for high‑performance workloads.

With this understanding, readers can confidently discuss Redis string internals in interviews.

Author: 小小木 (source: cnblogs.com/wyc1994666/p/10669212.html)

PerformanceDatabaseRedisString()SDS
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.