This article brings you relevant knowledge about data types in Redis. It mainly introduces issues related to common data type usage scenarios. I hope it will be helpful to everyone.
Recommended learning: Redis learning tutorial
Redis data types and usage scenarios
Compared with other KV databases, one of the major features of Redis is that it supports rich data types. It supports a total of 5 data types. These 5 data types, their usage scenarios and internal implementation are introduced one by one below.
string
Introduction: The string type is the most basic data type in Redis, the most commonly used data type, and is even used by many players as the only data type of redis. The string type is binary safe in redis, which means that the string value cares about the binary string and does not care about the specific format. You can use it to store strings in json format or JPEG image format.
Get the string length
Append content to the string
Set and get the string A certain piece of content
Set and get a certain bit of the string
Set the contents of a series of strings in batches
Commonly used commands: set, get, decr, incr, mget, etc.
Application scenario:
(1) Store the value of a field in MySQL
Design the key as table name: primary key name: primary key value: field Name
(2) Storage object
The string type supports strings in any format. The most commonly used one is to store strings formatted by json or other objects. (It is recommended to use hash data type in this scenario)
set user:id:1 '[{"id":1,"name":"zj","email":"156577812@qq.com"},{"id":1,"name":"zj","email":"156577812@qq.com"}]'
(3) Generate auto-incrementing id
When the value of the string type of redis is in integer form, redis can automatically treat it as an integer. Increment (incr) and decrement (decr) operations. Since all redis operations are atomic, there is no need to worry about transaction problems that may occur when multiple clients connect.
Commonly used commands: hget, hset, hgetall, etc.
Application scenarios:
Storage structured data, such as user information. In Memcached or CKV, for user information such as the user's nickname, age, gender, points, etc., we need to serialize it first and store it as a string value. At this time, when we need to modify one of the items, we usually need to After the value is taken out and deserialized, the value of an item is modified, and then serialized and stored back. This not only increases the overhead, but is also not suitable for situations where concurrent operations are possible (for example, two concurrent operations require modification of points). The Hash structure of Redis allows you to modify only a certain attribute value just like updating an attribute in the database. As shown below:
Key is the user ID, value is a Map, the key of this Map is the attribute name of the member, and the value is the attribute value. In this way, the modification and access of the data can be directly through its internal Map. Key (the key of the internal Map is called field in Redis), that is, the corresponding attribute data can be manipulated through the key (user ID) field (attribute label). There is no need to store data repeatedly, and it will not cause serialization and concurrency. Modify control issues.
However, it should be noted here that Redis provides an interface (hgetall) to directly obtain all attribute data. However, if there are many members of the internal Map, it involves traversing the entire internal Map. Due to the Redis single-threaded model Because of this, this traversal operation may be time-consuming, and it will not respond to requests from other clients at all. This requires special attention.
Can be used to build indexes. For example, User objects, in addition to id, sometimes also need to be queried by name. You can build a Hash with the key user:name:id. When inserting the User object (set user:101{"id":101,"name":"calvin ”}), by the way, insert an entry into this hash (hset user:name:id calvin 101). At this time, calvin is used as a key in the hash, with a value of 101. When querying by name, use hgetuser:name:id calvin to get the id from the key named calvin. If you need to use multiple indexes to find a certain piece of data, you can use one hash key to avoid using multiple string keys to store index values.
HINCRBY can also be used to implement idmaker. Compared with the string type idmaker, each type requires a key, and the hash type only needs one key.
The hash data type has the advantage of being more flexible and faster than the string type when storing the above types of data. Specifically, using the string type to store, it is necessary to convert and parse the json format string, even if it is not Conversion is required. In terms of memory overhead, hash still has the advantage.
Method to realize:
Redis Hash corresponding to Value is actually a HashMap. There are two different implementations here. When the Hash has fewer members, Redis will use a one-dimensional array-like method to compactly store it in order to save memory, instead of using a real HashMap. Structure, the encoding of the corresponding value redisObject is zipmap. When the number of members increases, it will automatically be converted into a real HashMap, and the encoding is ht.
Introduction:
list is a linked list of strings sorted in order of insertion, which can be Insert new elements at the head and tail (implemented by a doubly linked list, the time complexity of adding elements at both ends is O(1)). When inserting an element, if the key does not exist, redis will create a new linked list for the key. If all elements in the linked list are removed, the key will also be removed from redis.
Commonly used commands: lpush, rpush, lpop, rpop, lrange, etc.
Application scenarios:
Various lists, such as Twitter’s follow list, fan list, etc., latest news rankings, comments on each article, etc. can also be implemented using Redis’ list structure.
Message queue, you can use the PUSH operation of Lists to store tasks in Lists, and then the worker thread uses the POP operation to take out the tasks for execution. The message queue here does not have an ack mechanism. What if the consumer gives the task to Pop and crashes before finishing it? One solution is to add an additional sorted set. When distributing, send it to both the list and the sorted set at the same time. The distribution time is used as the score. After the user completes the task, he must use ZREM to eliminate the jobs in the sorted set and periodically remove the jobs from the sorted set. Remove the unfinished tasks that have timed out and put them back into the list. Another approach is to add an extra list for each worker, use RPopLPush when popping up tasks, put the job into the worker's own list at the same time, and use LREM to eliminate it when completed. If the cluster management (such as zookeeper) finds that the worker has died, the worker's list content will be returned to the main list.
Using LRANGE can easily realize the paging function of list content.
Operation to get the latest N data: LPUSH is used to insert a content ID and store it as a keyword at the head of the list. LTRIM is used to limit the number of items in the list to a maximum of 5000. If the amount of data the user needs to retrieve exceeds this cache capacity, then the request needs to be sent to the database.
Implementation method:
The implementation of Redis list is a two-way linked list, which can support reverse search and traversal, which is more convenient to operate, but it brings some additional memory overhead. Redis internal Many implementations, including send buffer queues, etc., also use this data structure.
Introduction: It is an unordered set. The elements in the set have no order. repeat. Putting duplicate elements into a Set will automatically remove them.
Common commands:
sadd, spop, smembers, sunion, etc.
Application scenarios:
Some lists need to be deduplicated, and set provides an important interface to determine whether a member is in a set collection, which is something that list cannot provide.
You can store some collective data. For example, in the Weibo application, you can store all the followers of a user in a collection, and store all their fans in a collection. Redis also provides operations such as intersection, union, and difference for collections, which can be very convenient to implement functions such as joint attention, common preferences, and second-degree friends. For all the above collection operations, you can also use different command selections. Return the results to the client or save them in a new collection. For another example, QQ has a social function called "Friend Tags". You can tag your friends, such as "big beauty", "tycoon", "Ouba", etc. Here you can also store each user's tags in within a collection.
If you want to know how many specific registered users or IP addresses have visited a certain page, you can do this: SADD page:day1:
Implementation method:
The internal implementation of set is a HashMap whose value is always null. In fact, it is quickly sorted by calculating hash. This is also what set can provide to determine whether a member is Reason within the set.
Introduction: Ordered sets, compared to sets, need to be provided when elements are put into the set The element's score can be automatically sorted based on the score.
Commonly used commands:
zadd, zrange, zrem, zcard, etc.
Usage scenarios:
Store an ordered and non-duplicate set list, For example, Twitter's public timeline can be stored with the publication time as the score, so that it will be automatically sorted by time when retrieved.
You can make a weighted queue. For example, the score of ordinary messages is 1, and the score of important messages is 2. Then the worker thread can choose to obtain the work tasks in the reverse order of the score. Prioritize important tasks.
Expired item processing: Use unix time as the key to keep the list sorted by time. Retrieve current_time and time_to_live to complete the difficult task of finding expired items. Another background task uses ZRANGE...WITHSCORES to query and delete expired entries.
Implementation method:
Redis sorted set internally uses HashMap and skip list (SkipList) to ensure the storage and ordering of data. HashMap places the mapping from members to scores, and skip All members are stored in the table, and the sorting basis is the score stored in the HashMap. Using the jump table structure can achieve higher search efficiency, and is relatively simple to implement.
Recommended learning: Redis video tutorial
The above is the detailed content of Summarize Redis data types and usage scenarios. For more information, please follow other related articles on the PHP Chinese website!