Master Redis String Internals: Common Interview Questions & Answers
This article reviews essential Redis string concepts, covering typical interview questions, encoding types, the EMBSTR threshold, SDS structure, and memory allocation details to help readers solidify their understanding of Redis string implementation.
Interview Focus: Redis String
Redis strings are a fundamental data type that frequently appear in technical interviews. Candidates should master common operations, underlying implementations, the three encoding schemes (INT, EMBSTR, RAW), and the specifics of the SDS structure.
Typical Interview Questions
1. What happens when you SET an existing key?
The existing value is overwritten.
2. How is a floating‑point number stored in a Redis string?
The number must first be converted to its textual representation; the string then stores that representation.
3. What is the maximum size of a Redis string?
A Redis string can be up to 512 MB, as documented by the official specifications.
4. How does Redis implement strings?
Redis strings use a robj object with three possible encodings:
INT : stores integer values directly as a long.
EMBSTR : used when the string length is less than or equal to a threshold (44 bytes in Redis 5.0.5).
RAW : used for larger strings.
5. Why is the EMBSTR threshold 44?
Redis uses jemalloc, which allocates memory in 64‑byte chunks. The redisObject occupies 16 bytes, and the SDS header (sdshdr) uses 3 bytes plus a terminating null byte, leaving 44 bytes for actual data.
#define LRU_BITS 24
typedef struct redisObject {
unsigned type:4;
unsigned encoding:4;
unsigned lru:LRU_BITS; /* LRU time or LFU data */
int refcount;
void *ptr;
} robj;The sdshdr8 structure (used for EMBSTR) is defined as:
struct __attribute__((__packed__)) sdshdr8 {
uint8_t len; /* used */
uint8_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 LSB of type, 5 unused bits */
char buf[];
};6. Why was the previous EMBSTR threshold 39?
Before Redis 3.2, the SDS header occupied 8 bytes. The introduction of multiple SDS header sizes (sdshdr8, sdshdr16, etc.) reduced the overhead by 6 bytes, but an extra flags byte added back 1 byte, resulting in a net saving of 5 bytes and a threshold of 39 bytes.
7. What is the purpose of SDS?
SDS (Simple Dynamic String) wraps C strings to provide:
O(1) length retrieval via a stored length field.
Pre‑allocated spare capacity to avoid frequent reallocations.
Binary‑safe storage without relying on a terminating null byte.
Understanding these details helps interviewees demonstrate deep knowledge of Redis’s memory model and string handling.
NiuNiu MaTe
Joined Tencent (nicknamed "Goose Factory") through campus recruitment at a second‑tier university. Career path: Tencent → foreign firm → ByteDance → Tencent. Started as an interviewer at the foreign firm and hopes to help others.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
