Redis application scenario (transfer)

(Source: http://www.cnblogs.com/shanyou/archive/2012/09/04/2670972.html)

Redis common data types

There are five most commonly used data types in Redis:

  • String
  • Hash
  • List
  • Set
  • Sorted set

Before describing these data types in detail, let's first use a picture to understand how these different data types are described in Redis's internal memory management:

First of all, Redis uses a redisObject object to represent all keys and values. The main information of redisObject is shown in the figure above: type represents the specific data type of a value object, and encoding is the storage method of different data types in redis, such as : type=string means that the value stored is a common string, then the corresponding encoding can be raw or int, if it is int, it means that the actual redis internal storage and representation of the string according to the numerical type, of course, the premise is this The string itself can be represented by a numerical value, such as: "123" "456" such strings.

The vm field needs to be specially explained here. Only when the virtual memory function of Redis is turned on, this field will actually allocate memory. This function is disabled by default, and this function will be described in detail later. From the above figure, we can find that Redis uses redisObject to represent all key/value data, which is a waste of memory. Of course, these memory management costs are mainly to provide a unified management interface for different data types of Redis. The actual author also provides There are various ways to help us minimize memory usage, which we will discuss in detail later.

Let's first analyze the use and internal implementation of these five data types one by one:

  • String

    Common commands:

    set,get,decr,incr,mget 等。

    Application scenarios:

    String is the most commonly used data type, and ordinary key/value storage can be classified into this category, which will not be explained here.

    Method to realize:

    String stored in redis is a string by default, which is referenced by redisObject. When incr, decr and other operations are encountered, it will be converted into a numeric type for calculation. At this time, the encoding field of redisObject is int.

  • Hash

    Common commands:

    hget,hset,hgetall 等。

    Application scenarios:

    Let's take a simple example to describe the application scenario of Hash. For example, we want to store a user information object data, including the following information:

    The user ID is the search key, and the stored value user object contains information such as name, age, birthday, etc. If it is stored in an ordinary key/value structure, there are mainly the following two storage methods:

    The first method uses the user ID as the search key, and encapsulates other information into an object and stores it in a serialized manner. The disadvantage of this method is that it increases the overhead of serialization/deserialization, and needs to modify one of the items. When the information is retrieved, the entire object needs to be retrieved, and the modification operation needs to protect the concurrency and introduce complex problems such as CAS.

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

    Then the Hash provided by Redis solves this problem very well. The Hash of Redis is actually a HashMap with the internally stored Value, and provides an interface to directly access the members of this Map, as shown in the following figure:

    That is to say, the key is still the user ID, the value is a map, the key of this map is the attribute name of the member, and the value is the attribute value, so that the modification and access to the data can be directly passed through the key of its internal map (in Redis The key of the internal Map is called field), that is, the corresponding attribute data can be manipulated through key (user ID) + field (attribute label), which neither requires repeated storage of data nor brings serialization and concurrent modification control. question. Great solution to the problem.

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

    Method to realize:

    As mentioned above, Redis Hash corresponding to Value is actually a HashMap. In fact, there will be two different implementations. When there are few members of this Hash, Redis will use a one-dimensional array-like method for compact storage in order to save memory, instead of using real HashMap structure, the encoding of the corresponding value redisObject is zipmap, when the number of members increases, it will be automatically converted into a real HashMap, at this time the encoding is ht.

  • List

    Common commands:

    lpush,rpush,lpop,rpop,lrange, etc.

    Application scenarios:

    There are many application scenarios of Redis list, and it is also one of the most important data structures of Redis. For example, twitter watch list, fan list, etc. can be implemented by Redis list structure, which is easier to understand and will not be repeated here.

    Method to realize:

    The implementation of Redis list is a doubly linked list, which can support reverse search and traversal, which is more convenient to operate, but brings some additional memory overhead. Many internal implementations of Redis, including sending buffer queues, also use this data structure.

  • Set

    Common commands:

    sadd,spop,smembers,sunion 等。

    Application scenarios:

    The function provided by Redis set to the outside world is similar to that of list, which is a list function. The special feature is that set can be automatically re-arranged. 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 judging whether a member is in a set collection, which is also not provided by list.

    Method to realize:

    The internal implementation of set is a HashMap whose value is always null. In fact, it is used to quickly arrange weights by calculating the hash. This is also the reason why set can judge whether a member is in the set.

Sorted set

Common commands:

zadd,zrange,zrem,zcard等

scenes to be used:

The usage scenario of Redis sorted set is similar to that of set, the difference is that set is not automatically sorted, while sorted set can sort members by providing an additional priority (score) parameter by the user, and it is inserted in order, that is, automatic sorting . When you need an ordered and non-repeating set list, you can choose the sorted set data structure. For example, the public timeline of twitter can be stored with the publication time as the score, so that it is automatically sorted by time when it is obtained.

Method to realize:

The interior of Redis sorted set uses HashMap and SkipList to ensure the storage and ordering of data. HashMap stores the mapping from members to scores, and the skip list stores all members. The sorting is based on the storage in HashMap. The score, using the structure of the jump table can obtain relatively high search efficiency, and is relatively simple in implementation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325516388&siteId=291194637