Data structure implementation of different data types of Redis

Source: Internet
Author: User

We know that Redis supports five types of data,

strings, hash tables (maps), lists, collections (sets), and ordered collections are, in contrast, similar to the Java Collection framework, and the data structures of different types are not the same.

Redisobject objects in the >>redis

Redis is written in C and internally implements a struct struct body Redisobject object,

Using the structure to imitate the "polymorphism" of object-oriented programming, as a support for the underlying data, Redisobject code:

/* Redis Object */typedef struct Redisobject {    //type    unsigned type:4;    Align the bit    unsigned notused:2;    Encoding method    unsigned encoding:4;    LRU time (relative to Server.lruclock)    unsigned lru:22;    Reference count    int refcount;    The value of the pointer to the object    void *ptr;} robj;

Where the type, encoding, and PTR3 properties are represented by:
Type of Type:redisobject, String, List, collection, ordered set, hash table
Encoding: Underlying implementation structure, string, Integer, jump table, compression list, etc.
PTR: A data structure that actually points to a saved value

If the Type property of a redisobject is redis_list, the encoding property is Redis_encoding_linkedlist,
Then this object is a Redis list, its value is stored in a double-ended linked list, and the PTR pointer points to the double-ended linked list;
If the Type property of a redisobject is Redis_hash, the encoding property is Redis_encoding_zipmap,
Then this object is a Redis hash table whose values are stored in a zipmap, and the PTR pointer points to the ZIPMAP.

The Redis_string/redis_list/redis_zset/redis_hash/redis_set in the following image is for the type in Redisobject,
The redis_encoding_linkedlist pointed to is the ENCODING field.

There are several underlying data structures for Redis:
Simple Dynamic string SDS
Double-ended linked list (LinkedList)
Dictionary (MAP)
Jumping table (skiplist)

The following is a study of the underlying data structures for the five types.

>>string

If a string type of value can be saved as an integer, the ENCODING of the corresponding Redisobject object is modified to Redis_encoding_int, and the PTR value corresponding to the RobJ object is changed to the corresponding value.
If you cannot convert to integers, keep the original ENCODING as Redis_encoding_raw.
Therefore, data of type string may be stored using the original string storage (actually sds-simple Dynamic Strings, corresponding ENCODING to Redis_encoding_raw) or integer.

Redis can view the encoding value of an object directly:

Redis:6379> set strtest 1okredis:6379> OBJECT ENCODING strtest "int" redis:6379> set strtest blogokredis:6379 > OBJECT ENCODING strtest "Raw"

  

>>list

There are 2 types of bottom-level implementations of the list:
Redis_encoding_ziplist
Redis_encoding_linkedlist
Ziplist saves memory compared to LinkedList,
When you create a new list, the default is to use a compressed list as the underlying data structure.
The internal Redis will judge the relevant operations,
When the list elem number is less than the configured value: Hash-max-ziplist-entries or Elem_value string is less than hash-max-ziplist-value and can be encoded as Redis_encoding_ Ziplist type storage to conserve memory;
But because adding and deleting elements in the ZIP list involves data movement,
Therefore, when the list content is large, a doubly linked list is used.

>>hash

When you create a new hash type, you also use Ziplist to store value by default, and hast table when you save too much data.

>>set

There are also two underlying implementations of a collection:
Redis_encoding_intset
Redis_encoding_ht (dictionary)
When you create a key-value of a set type, value is saved with the Intset type if value can be represented as an integer.
When the amount of data is large, switch to use hash table to save each value.

>>sorted Set

The bottom-up implementation of an ordered set is also 2 kinds:
Redis_encoding_ziplist
Redis_encoding_skiplist

For a jump table in Redis, check out this article: Jumping tables
The use of jumping tables in Redis is to implement an ordered set of data types.

Non-primary data, correlation analysis from forum blogs, etc., version Redis 2.6.

Learn more:Redis data Types

Data structure implementation of different data types of Redis

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.