Redis common data types at a glance

Source: Internet
Author: User
Tags memory usage serialization set set

Redis most commonly used data types are the following five: String Hash List Set Sorted set

Before describing these types of data, let's take a picture of how these different data types are described in Redis internal memory management:

First, Redis internally uses a Redisobject object to represent all key and value,redisobject most important information as shown above: type represents the specific data type of a value object. Encoding is the way in which different data types are stored inside Redis, for example: Type=string represents a normal string for value, so the corresponding encoding can be raw or int, An int means that the actual redis internally is stored and represented by a numeric class, assuming that the string itself can be represented numerically, such as "123" "456" strings.

This requires a special description of the VM field, only if the Redis virtual Memory feature is turned on, this field will actually allocate memory, which is turned off by default, which is described later. The above figure shows that Redis use Redisobject to represent all the key/value data is a waste of memory, of course, these memory management costs are mainly to give redis different data types to provide a unified management interface, The actual author also provides a variety of ways to help us save memory usage as much as possible, and we'll discuss it later.

Let's first analyze the use and internal implementation of these five types of data: String common commands:

Set,get,decr,incr,mget and so on. Application Scenario:

String is the most commonly used type of data, and ordinary key/value storage can be categorized as such, and this is not explained here. Implementation mode:

String inside Redis The default is a string that is referenced by Redisobject and is converted to a numeric value when a INCR,DECR operation is encountered, at which point the encoding field of Redisobject is int. Hash Common commands:

Hget,hset,hgetall and so on. Application Scenario:

We simply cite an example to describe the application scenario for the hash, for example, we want to store a user information object data containing the following information:

The user ID is lookup key, the stored value user object contains the name, the age, the birthday and so on information, if uses the ordinary key/value structure to store, mainly has the following 2 kinds of storage way:

The first way is to use the user ID as the lookup key, the disadvantage of encapsulating other information as a serialized object is to increase the cost of serialization/deserialization and to retrieve the entire object when one of the information needs to be modified, and the modification operation requires concurrency protection. Introduce complex issues such as CAs.

The second method is how many members of the user information object are stored as Key-value pairs, the value of the corresponding attribute is obtained with the name of the corresponding property of the user id+, although the serialization cost and concurrency problems are omitted, but the user ID is duplicate storage, and if there is a large amount of such data, The memory waste is still very considerable.
So Redis provides a good solution to this problem, the hash of Redis is actually the internal storage value of a hashmap, and provides a direct access to this map member of the interface, the following figure:

In other words, the key is still the user ID, value is a map, the map key is the member's attribute name, value is the attribute value, so that the modification and access to the data can be directly through its internal map key (Redis called the internal Map key field), That is, the key (user ID) + field (attribute tag) can manipulate the corresponding attribute data, neither need to duplicate the data storage, nor bring serialization and concurrency modification control problems. Solved the problem very well.

At the same time, note that Redis provides an interface (Hgetall) to directly fetch all of the attribute data, but if there are many members of the internal map, then it involves traversing the entire internal map, which may be time-consuming due to the Redis single-threaded model, The other client's request is not responding at all, which requires extra attention. Implementation mode:

Above already said Redis hash corresponds to value inside actually is a hashmap, actually there will be 2 different implementations, this hash member is relatively young redis in order to save memory will be similar to a one-dimensional array of methods to compact storage, and will not adopt a real HASHMAP structure , the corresponding value Redisobject encoding is Zipmap, and when the number of members increases, it automatically turns into a real hashmap, at which point the encoding is HT. List Common commands:

Lpush,rpush,lpop,rpop,lrange and so on. Application Scenario:

Redis list has a lot of applications and is one of the most important data structures in Redis, such as Twitter's attention list, fan list and so on can be achieved with the Redis list structure, which is better understood and not repeated here. Implementation mode:

Redis list is implemented as a two-way linked list, which can support reverse lookup and traversal, more convenient operation, but brings some extra memory overhead, many implementations within Redis, including sending buffer queues are also used in this data structure. Set Common commands:

Sadd,spop,smembers,sunion and so on. Application Scenario:

Redis set provides a function that is similar to a list, especially if the set is automatic, and when you need to store a list of data and do not want duplicate data, set is a good choice, and set provides an important interface for determining whether a member is within a set set, which is not available in the list. Implementation mode:

The internal implementation of set is a HashMap value that is always null, and is actually used to compute the hash in order to quickly row the weight, which is why set provides a way to judge whether a member is within a set. Sorted Set Common commands:

Zadd,zrange,zrem,zcard and other use scenarios:

The use scenario for the Redis sorted set is similar to set, except that the set is not automatically ordered, and sorted set can be sorted by the user providing an additional priority (score) parameter, and is inserted in an orderly, automatic sort. When you need an ordered and not duplicated list of collections, you can choose to sorted set data structures, such as Twitter's public timeline can be stored as score by publication time, which is automatically sorted by time. Implementation mode:

Redis sorted set of the internal use of HashMap and jump Table (skiplist) to ensure the storage and order of data, HashMap is a member to the score of the map, and the jump tables are stored in all the members, sorted according to the HashMap in the score , the use of jump table structure can obtain a relatively high search efficiency, and in the implementation is relatively simple.

Turn from: http://www.cnblogs.com/shanyou/archive/2012/09/04/2670972.html

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.