Skip to main content

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:

c
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_STRING
  • REDIS_LIST
  • REDIS_SET
  • REDIS_ZSET
  • REDIS_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 typePossible encodingsDescription
Stringint, embstr, rawA number, a short string (in the object), a long string (in allocated memory)
ListquicklistA compressed list of nodes
Setintset, hashtableA set of numbers, or a full hash table
Hashziplist (before Redis 6) → listpackCompact storage of "key-value" pairs
Sorted Setziplist / skiplistA compact structure, or a balanced list
StreamlistpackA 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, and intset store 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:

bash
SET age 30

Redis:

  • creates the age key (String),
  • sees the value is a number,
  • stores it as an object with type = REDIS_STRING, encoding = int, ptr = 30.

If you then run:

bash
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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.