Suggest an editImprove this articleRefine the answer for “How does Redis store a value's type under the hood?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)Redis stores every value as an object (`RedisObject` / `robj`) that carries a `type` field (the logical type: string, list, set, zset, hash) and an `encoding` field (the physical representation - int, embstr, raw, ziplist/listpack, hashtable, skiplist, and so on). **Key point:** Redis dynamically switches encodings depending on data size (e.g. a small Hash is stored as a compact listpack, and converts to a full hashtable as it grows), so commands run in O(1) with no extra on-the-fly checks.Shown above the full answer for quick recall.Answer (EN)ImageA 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 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`, 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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.