Introduction to Redis Object Types
Redis is a key-value database where both keys and values are represented as objects. For example, when you run the following command:
[plain]
Redis> SET message "hello redis"
The key 'message' is an object containing the string "message", and the value is an object that contains "hello redis".
Redis supports five main object types:
- REDIS_STRING – String object
- REDIS_LIST – List object
- REDIS_HASH – Hash object
- REDIS_SET – Set object
- REDIS_ZSET – Sorted set object
Each Redis object has a specific structure, which is defined as follows in C:
[cpp]
/*
* Redis object
*/
typedef struct redisObject {
// Type of the object
unsigned type: 4;
// Not used (alignment bit)
unsigned notused: 2;
// Encoding
unsigned encoding: 4;
// LRU time (relative to server.lruclock)
unsigned lru: 22;
// Reference count
int refcount;
// Pointer to the value
void *ptr;
} robj;
The 'type' field defines the object type, such as string, list, hash, etc. However, for efficiency, each object may use different underlying data structures depending on its content. The 'encoding' field specifies how the object is stored internally.
Redis Object Underlying Data Structures
Redis uses eight different underlying data structures, each corresponding to a specific encoding. These include:
- REDIS_ENCODING_INT – Long integer
- REDIS_ENCODING_EMBSTR – Embedded string (introduced in Redis 3.0)
- REDIS_ENCODING_RAW – Raw string
- REDIS_ENCODING_HT – Dictionary
- REDIS_ENCODING_LINKEDLIST – Doubly linked list
- REDIS_ENCODING_ZIPLIST – Compressed list
- REDIS_ENCODING_INTSET – Integer set
- REDIS_ENCODING_SKIPLIST – Skip list combined with a dictionary
String objects can be encoded as integers, raw strings, or embstr. If the string's content can be converted into a long integer, it will be stored as an integer. Otherwise, it will be stored as either an embstr or a raw string. Embstr is used for small strings (less than 39 bytes), while raw is used for longer ones. This optimization helps reduce memory usage and improve performance.
The code below shows how Redis decides between embstr and raw encoding:
[cpp]
#define REDIS_ENCODING_EMBSTR_SIZE_LIMIT 39
robj *createStringObject(char *ptr, size_t len) {
if (len <= REDIS_ENCODING_EMBSTR_SIZE_LIMIT)
return createEmbeddedStringObject(ptr, len);
else
return createRawStringObject(ptr, len);
}
Understanding these internal structures helps developers optimize their Redis usage and better manage memory and performance in real-world applications.
eifbar hqdvape randm 9000 igerbar cbd thc vape
YIWU JUHE TRADING COMPANY , https://www.nx-vapes.com