Redis Application Scenario redis Introduction 2 -- common basic types of redis vs Oracle advance queue performance comparison (I) redis vs Oracle advance queue performance comparison (II)

Source: Internet
Author: User

Redis has created a new way of storing data. When using redis, we don't have to focus on how to put elephants in the refrigerator in the face of monotonous databases, instead, redis uses flexible data structures and data operations to build different refrigerators for different elephants.

Common redis Data Types

Redis has the following data types:

    • String
    • Hash
    • List
    • Set
    • Sorted set

Before describing these data types, let's take a look at how redis's internal memory management describes these different data types:

First, redis uses a redisobject object to represent all keys and values. The primary information of redisobject is shown in: Type represents the specific data type of a value object, encoding is the storage method of different data types in redis. For example, if type = string indicates that value is stored as a common string, the corresponding encoding can be raw or Int, if it is an int, it indicates that the actual redis instance stores and represents the string by numeric type. Of course, the premise is that the string itself can be represented by numerical values, for example: "123" "456.

Note the VM field here. This field is actually allocated only when the redis virtual memory function is enabled. This function is disabled by default, this function will be described later. Redis uses redisobject to indicate that all key/value data is a waste of memory, of course, these memory management costs are mainly paid to provide a unified management interface for different redis data types. The actual author also provides a variety of methods to help us minimize memory usage, we will discuss it in detail later.

Next we will analyze the usage and internal implementation of these five data types one by one:

  • String

    Common commands:

    Set, get, decr, incr, mget, etc.

    Application scenarios:

    String is the most common data type. Common Key/value storage can be classified as this type, so we will not explain it here.

    Implementation Method:

    String is a string by default stored in redis and is referenced by redisobject. When an incr, decr, or other operation is performed, it is converted to a numeric type for calculation. In this case, the encoding field of redisobject is int.

  • Hash

    Common commands:

    Hget, hset, hgetall, etc.

    Application scenarios:

    Let's take an example to describe the hash Application Scenario. For example, we want to store a user information object data that contains the following information:

    The User ID is the search key. The stored value user object contains the name, age, birthday, and other information. If you use a common key/value structure for storage, there are two storage methods:

    The first method is to use the user ID as the search key and encapsulate other information into an object for storage in serialized mode. The disadvantage of this method is that it increases the serialization/deserialization overhead, in addition, when you need to modify one of the items, You need to retrieve the entire object, and the modification operation needs to protect concurrency and introduce complicated problems such as CAS.

    The second method is to store the key-value pair as many members of the user information object, and use the user ID + name of the corresponding attribute as a unique identifier to obtain the value of the corresponding attribute, although serialization overhead and concurrency problems are saved, the user ID is retained. If there is a large amount of such data, the memory waste is still considerable.

    The hash provided by redis can solve this problem very well. redis's hash is actually a hashmap of internally stored values and provides interfaces for direct access to this map member, such:

    That is to say, the key is still the user ID, the value is a map, the key of the map is the property name of the member, and the value is the property value, in this way, you can directly modify and access data through the internal map key (the internal map key in redis is called field), that is, through the key (User ID) + field (attribute tag) can operate the corresponding attribute data, neither storing data repeatedly nor causing serialization and concurrency modification control problems. Solved the problem.

    At the same time, it should be noted that redis provides an interface (hgetall) to directly retrieve all attribute data. However, if there are many internal map members, it involves traversing the entire internal map operation, because of the redis single-threaded model, this traversal operation may be time-consuming, and requests from other clients do not respond at all. This requires special attention.

    Implementation Method:

    As mentioned above, the value corresponding to redis hash is actually a hashmap. There are two different implementations here, when there are few Members of this hash, redis will adopt a compact storage like a one-dimensional array to save memory, instead of a real hashmap structure. The corresponding value redisobject's encoding is zipmap, when the number of members increases, it is automatically converted to a real hashmap. In this case, encoding is ht.

  • List

    Common commands:

    Lpush, rpush, lpop, rpop, and lrange.

    Application scenarios:

    Redis list has many application scenarios and is also one of redis's most important data structures. For example, the Twitter follow list and fans list can all be implemented using the redis list structure, which is easy to understand, this is not repeated here.

    Implementation Method:

    The implementation of redis list is a two-way linked list, that is, it supports reverse lookup and traversal to facilitate operations. However, it brings some additional memory overhead and many internal implementations of redis, this data structure is also used, including the sending Buffer Queue.

  • Set

    Common commands:

    Sadd, spop, smembers, and sunion.

    Application scenarios:

    The functions provided by redis set are similar to those provided by list. The special feature is that set can automatically record duplicates. When you need to store a list of data, if you do not want duplicate data, set is a good choice, and set provides an important interface to determine whether a member is in a set. This is also not provided by list.

    Implementation Method:

    The internal implementation of set is a hashmap whose value is always null. Actually, it is to calculate the hash method to quickly remove duplicates, this is also why set can determine whether a member is in the set.

Sorted set

Common commands:

Zadd, zrange, zrem, zcard, etc.

Use Cases:

The usage scenario of redis sorted set is similar to set. The difference is that set is not automatically ordered, while sorted set can sort members by providing an additional priority (score) parameter, it is insert-ordered, that is, automatic sorting. When you need an ordered and non-repeated list of sets, you can choose sorted set data structure. For example, Twitter's public timeline can be stored as score by posting time, in this way, the query is automatically sorted by time.

Implementation Method:

In redis sorted set, hashmap and skiplist are used internally to ensure data storage and order. In hashmap, Members are mapped to scores, the hop table stores all the members, and the sorting is based on the score saved in hashmap. using the structure of the hop table, you can get a high search efficiency and the implementation is relatively simple.

 

    • Redis application scenarios
    • Why use redis and its product positioning
    • Redis memory usage optimization and storage
    • Redis replication and Scalable Cluster Creation
    • Redis in the real world
    • Redis Introduction 2-common basic types
    • Implementation of the redis Message notification system
    • Redis vs Oracle
      Advance queue performance comparison (1)
    • Redis vs Oracle advance queue performance comparison (2)
    • Redis Practice Notes
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.