Modular Electronics,Citizen Electronics,Electronic Theory Blog - poitcomputers.com

Redis basic type and underlying implementation

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.

Rum Bar 9000

334818838 3433473450228893 9072818861638736862 N Jpg334799925 753423263115460 4190401049832547556 N Jpg1114ca2831666a7c22d43b4c8afa08d Jpg5e58dea01b27161dfc5621879a10506 Jpg7c86e57dfca11e8bd24c57bd3baaaf7 Jpg4ff80c5036ce9eeda9ee134f8baafcd Jpg8 Jpg2 Jpg6 Jpg

eifbar hqdvape randm 9000 igerbar cbd thc vape

YIWU JUHE TRADING COMPANY , https://www.nx-vapes.com