Five commonly used data types in redis

The following is my summary of the five commonly used APIs for data categories, arranged from first to back according to frequency of use.
Insert picture description here

1.String type

String is the most basic type of redis. In redis, a key corresponds to a value.
The String type is binary safe, which means that the redis string can contain any data, such as jpg images or serialized objects. The Strings data structure is a simple key-value type. The value is actually not only a String, but also a number.

Common commands: set, get, decr, incr, mget, etc.

Application scenario: String is the most commonly used data type, and ordinary key/value storage can be classified into this category. That is, it can fully realize the current Memcached function and is more efficient. You can also enjoy Redis's timing persistence, operation log and Replication functions. In addition to providing the same get, set, incr, decr and other operations as Memcached, Redis also provides the following operations:

  • Get string length
  • Append content to string
  • Set and get a certain part of the string
  • Set and get a bit of the string
  • Set the contents of a series of strings in batch

Implementation method: String is stored in redis by default as a string, which is referenced by redisObject. When it encounters incr, decr and other operations, it will be converted to a numeric value for calculation. At this time, the encoding field of redisObject is int.

2.Hash

Redis hash is a collection of key-value pairs. Redis hash is a mapping table between field and value of string type, and hash is particularly suitable for storing objects. Similar to Map<String,Object> in Java.

The KV mode remains unchanged, but V is a key-value pair.

Common commands: hget, hset, hgetall, etc.

Application scenario: In Memcached, we often package some structured information into a HashMap, and store it as a string value after serialization on the client side, such as the user's nickname, age, gender, points, etc., which need to be modified at this time For a certain item, it is usually necessary to remove all the values ​​after deserialization, modify the value of a certain item, and then serialize and store it back. This not only increases the overhead, but also does not apply to situations where concurrent operations are possible (for example, two concurrent operations need to modify the integral). The Hash structure of Redis allows you to modify only one attribute value just like updating an attribute in the database.

Let's briefly give an example to describe the application scenario of Hash. For example, we want to store a user information object data, which contains the following information:

The user ID is the key to be looked up, and the stored value user object contains information such as name, age, birthday, etc. If a common key/value structure is used to store it, 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 to be stored in a serialized manner. The disadvantage of this method is that it increases the serialization/deserialization overhead and needs to modify one of them. When information is needed, the entire object needs to be retrieved, and the modification operation needs to protect the concurrency, introducing complicated issues such as CAS.

The second method is to store as many key-value pairs as there are members of the user information object, 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 issues are eliminated , But the user ID is stored repeatedly. If there is a large amount of such data, the memory waste is still considerable.

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

In other words, 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 Call the key of the internal Map field), that is, through key (user ID) + field (attribute label), the corresponding attribute data can be manipulated. There is no need to store data repeatedly, and it will not bring serialization and concurrent modification control. problem. Solved the problem very well.

At the same time, it should be noted that Redis provides an interface (hgetall) to directly get all the 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 Redis single-threaded model, this traversal operation It may be time-consuming, and other client requests do not respond at all. This requires special attention.

Method to realize:

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

3.List

The redis list is a simple list of strings, sorted in the order of insertion. You can add an element to the head (left) or tail (right) of the list. Its bottom layer is actually a linked list.

Common commands: lpush, rpush, lpop, rpop, lrange, etc.

Application scenarios:

There are many application scenarios for Redis list, and it is also one of the most important data structures of Redis. For example, Twitter's watch list and fan list can all be implemented using Redis's list structure.

Lists are linked lists. I believe anyone with a little knowledge of data structure should be able to understand its structure. Using the Lists structure, we can easily implement the latest news ranking and other functions. Another application of Lists is the message queue.
You can use the PUSH operation of Lists to store tasks in the Lists, and then the worker thread uses the POP operation to take out the tasks for execution. Redis also provides an api to manipulate a certain section of Lists, you can directly query and delete a certain section of elements in Lists.

Method to realize:

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

4.Set

Redis set is an unordered collection of string type.

Common commands:
sadd, spop, smembers, sunion, etc.

Application scenarios:

The external function provided by Redis set is similar to the function of a list. The special feature is that the set can automatically sort the weight. 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.

The concept of Sets is a combination of unique values. Using the Sets data structure provided by Redis, some collective data can be stored. For example, in a Weibo application, all the followers of a user can be stored in a collection, and all of its fans can be stored in a collection. Redis also provides operations such as intersection, union, and difference for sets, which can be very convenient to implement functions such as common attention, common preference, second-degree friends, etc. For all the above set operations, you can also use different command options Whether to return the result to the client or save the collection to a new collection.

Method to realize:

The internal implementation of set is a HashMap whose value is always null. In fact, it is quickly sorted by calculating the hash. This is the reason why set can determine whether a member is in the set.

5.Sorted Set

On the basis of set, add a score value. Previously set was k1 v1 v2 v3, now zset is k1 score1 v1 score2 v2.
Sorted Set is also a collection of string type elements like set, and duplicate members are not allowed. The difference is that each element is associated with a double type score. Redis uses scores to sort the members of the set from small to large. The members of zset are unique, but the score (score) can be repeated.

Commonly used 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 ordered, 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.

In addition, you can also use Sorted Sets to make a weighted queue. For example, the score of ordinary messages is 1, and the score of important messages is 2, and then the worker threads can choose to obtain work tasks in the reverse order of the score. Prioritize important tasks.

Method to realize:

Redis sorted set uses HashMap and skip list (SkipList) internally to ensure the storage and order of data. HashMap puts the mapping from members to score, while the skip table stores all members, and the sorting basis is stored in HashMap. For the score, use the structure of the jump table to obtain a relatively high search efficiency, and the implementation is relatively simple.

Guess you like

Origin blog.csdn.net/qq_43458555/article/details/108213265