Databases 7 min read

Understanding Redis String Implementation and Its Performance Advantages

This article explains how Redis implements its own string type using Simple Dynamic Strings (SDS), details the internal structure and dynamic expansion mechanism, and highlights the performance benefits such as O(1) length retrieval, buffer‑overflow protection, and reduced memory allocation overhead.

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

During a recent interview the author realized a gap in understanding Redis internals, particularly how Redis implements its string data type, and decided to share the findings.

The article covers two main topics:

Redis string implementation

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 Simple Dynamic String (SDS) to achieve higher speed and performance.

SDS is defined by the following structure:

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

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

Dynamic Expansion of SDS

When additional data is appended, Redis checks whether the current free space is sufficient. If not, it allocates a larger buffer according to the following rules:

Calculate the required size.

Allocate enough memory to satisfy the request.

If the new length < 1 MiB, allocate free space equal to the new length; otherwise allocate a fixed 1 MiB free space.

This strategy resembles Java's List growth algorithm.

Performance Advantages of Redis Strings

Fast length retrieval (O(1) via stored len).

Prevention of buffer overflow.

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

Fast Length Retrieval

Because SDS keeps the used length in the struct, obtaining the string length simply returns len , which is O(1). In contrast, a traditional C string requires scanning until the terminating '\0', which is O(n).

Buffer‑Overflow Protection

Before concatenating strings, Redis always checks and expands the buffer if necessary, eliminating the risk of overflow that can occur with naïve strcat usage.

Reduced Memory Allocation Overhead

Redis employs two optimizations:

Space pre‑allocation : After an append, Redis not only allocates the exact needed space but also reserves additional free space ("free") based on the new length (len). For len < 1 MiB, free = len; for len ≥ 1 MiB, free = 1 MiB.

Lazy space reclamation : When a string shrinks, Redis does not immediately free the reduced memory; instead it keeps the space for future use, while providing an API for manual reclamation.

These techniques lower the frequency of system memory allocations, which are costly operations.

With this knowledge, the author feels prepared to discuss Redis string internals confidently in future interviews.

Source: juejin.im/post/5ca9d8ae6fb9a05e5c05c4e8
Performancememory managementRediscString()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.