Use of five types of data Redis

Source: Internet
Author: User
Tags data structures numeric value redis serialization set set
use of five types of data Redis


Redis Five types of data (excerpted from: http://tech.it168.com/a2011/0818/1234/000001234478_all.shtml)

1, 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.

2, 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:

User ID, for the lookup key,

The stored value user object contains information such as name, age, birthday birthday, and so on.

If you use a common key/value structure to store, there are mainly the following 2 ways of storage:

The first way to use the user ID as the lookup key, encapsulate other information into an object to be stored in a serialized way,

such as: Set u001 "Lie triple systems, 18,20010101"

The disadvantage of this approach is that the cost of serialization/deserialization is increased, and when one of the information needs to be modified, the entire object needs to be retrieved, and the modification operation needs to be secured concurrently, introducing complex issues such as CAs.

The second method is how many members of this user information object can be saved as key-value pairs, and the corresponding attribute is obtained by the user id+ the name of the corresponding property as a unique identifier.

such as: Mset User:001:name "lie triple Systems" User:001:age18user:001:birthday "20010101"

Although the serialization cost and concurrency problems are omitted, but the user ID is duplicate storage, if there is a large amount of such data, the memory waste is 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,

such as: Hmset user:001 name "Lie triple systems" Age Birthday "20010101"

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.

3, 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.

4, 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.

5, Sorted set

Common commands:

Zadd,zrange,zrem,zcard, etc.

Usage 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.

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.