How does Redis store a value's type under the hood?
A great question, this is exactly what makes Redis more than just "key-value", a high-performance in-memory object store. Under the hood, Redis stores every piece of data as an object, each carrying a type, an encoding, and auxiliary metadata.
Let's break it down:
1. Every value is a RedisObject
Internally, Redis doesn't just store a "value", it builds a structure like:
typedef struct redisObject {
unsigned type:4; // type (string, list, hash, set, zset, etc.)
unsigned encoding:4; // storage method (raw, int, ziplist, etc.)
void *ptr; // a pointer to the actual data
int refcount; // a reference counter
...
} robj;The takeaway: Redis knows exactly what data type this is and exactly how it's stored (which memory structure).
2. type, the logical data type
The type field defines what kind of structure this is at the level of Redis's API:
REDIS_STRINGREDIS_LISTREDIS_SETREDIS_ZSETREDIS_HASH
Redis's commands (GET, HGETALL, ZADD, etc.) use this field to know which operations are valid.
3. encoding, the physical representation
This is how exactly the data is laid out in memory. Redis dynamically picks the optimal encoding based on the data's size and structure.
Examples:
| Data type | Possible encodings | Description |
|---|---|---|
| String | int, embstr, raw | A number, a short string (in the object), a long string (in allocated memory) |
| List | quicklist | A compressed list of nodes |
| Set | intset, hashtable | A set of numbers, or a full hash table |
| Hash | ziplist (before Redis 6) → listpack | Compact storage of "key-value" pairs |
| Sorted Set | ziplist / skiplist | A compact structure, or a balanced list |
| Stream | listpack | A sequence of records with IDs and fields |
Redis automatically switches between encodings:
for instance, a small Hash is stored as a compact listpack,
and converts to a full hashtable as it grows.
4. Memory optimization
Redis applies several techniques to cut overhead:
- Shared integers: small numbers (0-9999) are stored as shared objects.
- SDS (Simple Dynamic String): strings are implemented with metadata (length, buffer, capacity).
- Compact structures:
listpack,ziplist, andintsetstore elements back to back in a single block of memory.
The result: less overhead, faster memory allocation and release.
5. A value's type is known instantly
Redis doesn't run checks "on the fly", the type and encoding are known immediately from the object itself.
So commands like GET or HGETALL run in O(1), with no extra conversions.
6. An example in action
When you run:
SET age 30Redis:
- creates the
agekey (String), - sees the value is a number,
- stores it as an object with
type = REDIS_STRING,encoding = int,ptr = 30.
If you then run:
SET description "long text about user ..."→ type = REDIS_STRING, but encoding = raw,
since this is now a string, not a number.
Summary
Under the hood, Redis stores every value as an object (RedisObject),
carrying:
- the data type (string, hash, list …),
- the storage encoding (int, ziplist, hashtable …),
- a pointer to the content.
That's what lets Redis automatically pick the optimal representation and work with memory as efficiently as possible, fast, compact, and predictable.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.