Redis knowledge (4)

Source: Internet
Author: User

1.3 zipmap Structure

If the redisObject type member value is of the REDIS_HASH type, when the entry of the hash is smaller than the configuration value: if the length of the hash-max-zipmap-entries or value string is less than hash-max-zipmap-value, it can be encoded into REDIS_ENCODING_ZIPMAP storage to save memory. otherwise, Dict is used for storage.

Zipmap uses a string array to store keys and values in sequence. Each key-value pair in the column is queried until it is found. Its structure is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/210143C28-0.jpg "style =" border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; text-align: center; background-color: # f3f3f3; padding: 4px 5px 5px; margin: 10px; "alt =" Image011730.jpg "/>

To save memory, we use some tips to save the length of key and value. if the length of the key or value is smaller than ZIPMAP_BIGLEN (254), it is expressed in one byte. If it is greater than ZIPMAP_BIGLEN (254), it is saved in five bytes, the first byte stores ZIPMAP_BIGLEN (254), and the last four bytes stores the length of the key or value.

  1. There are only 2 bytes at initialization, and 1st bytes indicate the number of key-value pairs saved by zipmap (if the number of key-value pairs exceeds 254, it is always expressed as 254, the number of actually saved key-value pairs in zipmap can be calculated using the zipmapLen () function ).

    • After hset (nick, wuzhu,



    • The number of key-value pairs (the number of entries in zipmap) saved in 1st bytes is 1.

    • 2nd bytes save key_len value 4

    • 3rd ~ 6. Save the key "nick"

    • 7th bytes to save the value_len value 5

    • 8th Bytes: the number of null and closed bytes is 0 (when the key value is reset, the length of its new value is not necessarily equal to the length of the old value, if the length of the new value is greater than the length of the old value, realloc expands the memory. If the length of the new value is smaller than that of the old value and the difference is greater than 4 bytes, realloc reduces the memory size, if the difference is less than 4, move the value forward and use empty_len to save the number of idle bytes)

    • 9th ~ 13 bytes to save the value "wuzhu"

  2. Hset (age, 30)

    Insert key-value pairs ("age", 30)


  3. Hset (nick, tide)

    Insert the key-value Pair ("nick", "tide"). Then, we can see that empty_len is 1,


1.4 ziplist Structure

If the redisObject type member value is of the REDIS_LIST type, the number of elem members in the list is smaller than the configured value: if the length of the hash-max-ziplist-entries or elem_value string is smaller than hash-max-ziplist-value, it can be encoded into REDIS_ENCODING_ZIPLIST storage to save memory. otherwise, Dict is used for storage.

Ziplist is actually a two-way linked list in the form of a string array. Its structure is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/2101434Z2-1.jpg "style =" border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; text-align: center; background-color: # f3f3f3; padding: 4px 5px 5px; margin: 10px; "alt =" Image011740.jpg "/>

  1. The ziplist header consists of three fields:

    • Ziplist_bytes: it is saved with a uint32_t to form the total length of the ziplist string array, including ziplist header,

    • Ziplist_tail_offset: A uint32_t is used to save and record the tail offset position of ziplist.

    • Ziplist_length: Use a uint16_t to save and record the number of elem in ziplist.


  2. Ziplist node is also composed of three parts:

    • Prevrawlen: the number of bytes occupied by the last ziplist node, including the number of bytes saved for prevarwlen, currawlen, and elem value.

    • Currawlen & encoding: number of bytes required for raw form deposits of the current elem value and the encoding method used for saving in ziplist (for example, the value can be converted to an integer, such as "1024 ", raw_len is 4 bytes, but it is converted to uint16_t for saving during ziplist storage, which occupies 2 bytes ).

    • (Encoded) value


You can use prevrawlen and currawlen & encoding to repeat ziplist.

Ziplist also provides some tips to save memory.

  • Len storage: If len is smaller than ZIP_BIGLEN (254), it is saved in one byte; otherwise, it needs to be saved in five bytes. ZIP_BIGLEN is saved in 1st bytes as the identifier.

  • Storage of value: If value is numeric, it is converted to ZIP_INT_16B, ZIP_INT_32B or ZIP_INT_64B based on the value range. Otherwise, it is saved in raw format.

1.5 adlist Structure



Typedef struct listNode {struct listNode * prev; struct listNode * next; void * value;} listNode; typedef struct listIter {listNode * next; int direction;} listIter; typedef struct list {listNode * head; listNode * tail; void * (* dup) (void * ptr); void (* free) (void * ptr); int (* match) (void * ptr, void * key); unsigned int len;} list;


Common two-way linked lists are not analyzed.

1.6 intset Structure

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/21014342N-2.jpg "style =" border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; text-align: center; background-color: # f3f3f3; padding: 4px 5px 5px; margin: 10px; "alt =" Image011750.jpg "/>

Intset uses an ordered integer array to implement set. struct intset is defined as follows:


Typedef struct intset {uint32_t encoding; uint32_t length; int8_t contents [];} intset;

Encoding: indicates whether the array is of the int16_t type, int32_t type, or int64_t type. as for how to select an array of that type, it is determined based on the value range of the saved values. The value is int16_t during initialization, and the maximum value in the set is [INT16_MIN, INT16_MAX]. the value range of [INT32_MIN, INT32_MAX], [INT64_MIN, INT64_MAX] to dynamically determine the type of the entire array. for example, set is of the int16_t type at the beginning. When a value with a value range of [INT32_MIN, INT32_MAX] is added to the set, the array that saves the set is upgraded to the int32_t array.


  • Length: The number of values in the set.

  • Contents: pointer to an integer Array


This article is from the "ant nest" blog, please be sure to keep this source http://feihan21.blog.51cto.com/1364153/1300020

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.