Use Cases of redis Data Structure
There are five types of data structures in redis. What are the application scenarios?
I. redis data structure application scenarios
I have read the redisbook book and I am familiar with the basic functions of redis. I have read the redis source code since last week. The current goal is to thoroughly understand the redis data structure. We all know that there are five data structures in redis. What are the application scenarios of each data structure?
- String -- string
- Hash-dictionary
- List -- list
- Set -- Set
- Sorted set -- Ordered Set
Next we will briefly describe their respective use cases:
1. String -- string
The string data structure is a simple key-value type. The value can be either a string or a number. (when the number type is long, encoding is an integer, others are stored in sdshdr as strings ). Using the strings type, you can fully implement the current memcached functions and improve efficiency. You can also enjoy redis's timed persistence (RDB mode or aof mode can be selected), operation logs, replication, and other functions. In addition to get, set, incr, and decr operations like memcached, redis also provides the following operations:
- Len niushuai: O (1) Get the string length
- Append niushuai redis: Append content to the string, and uses smart memory allocation (twice each time)
- Set and obtain a part of a string
- Set and obtain a bit of a string)
- Batch set the content of a series of strings
- Atomic counter
- To use the GetSet command, set a new value while clearing the old value, and use it with the atomic counter.
2. Hash-dictionary
In memcached, we often package some structured information into a hashmap, which is stored as a string value after the client is serialized (generally in JSON format ), such as the nickname, age, gender, and points of a user. At this time, when you need to modify one of the items, you usually need to extract the string (JSON), then deserialize it, modify the value of a specific item, and then serialize it into a string (JSON) for storage. Simply modifying an attribute will do so many things, which will consume a lot and will not apply to some scenarios where concurrent operations are possible (for example, You need to modify points for both concurrent operations ). The hash structure of redis allows you to modify only one attribute value just like updating an attribute in the database.
3. list -- list
To put it bluntly, a list is a linked list (a list implemented by redis using a double-ended linked list). I believe that anyone who has learned data structure knowledge should be able to understand its structure. Using the list structure, we can easily implement the latest message ranking and other functions (such as Sina Weibo's timeline ). Another application of list is message queue. You can use the * Push operation of list to store the task in list, and then the worker thread uses the pop operation to retrieve and execute the task. Redis also provides APIs to operate on a certain element in the list. You can directly query and delete the elements in a specific part of the list.
4. Set -- Set
Set is a set. The concept of a set is a combination of non-repeated values. Using the set data structure provided by redis, you can store some set data. For example, in a Weibo application, you can store all the followers of a user in a collection and store all the followers in a collection. Because redis is very user-friendly and provides operations such as intersection, union, and difference set for the collection, you can easily implement functions such as common concern, common preferences, and friends, you can also use different commands to return the result to the client or save the result to a new set.
- Mutual friends and second-degree friends
- With uniqueness, you can count all the independent IP addresses that access the website
- When a friend recommends a friend, the intersection is obtained based on the tag, which can be recommended if it is greater than a certain threshold.
5. Sorted set -- Ordered Set
Compared with sets, sorted sets adds a weight parameter score to the element in the set so that the elements in the set can be sorted by score, for example, a sorted set that stores the scores of the entire class, the set value can be the student's student ID, while the score can be the score for the exam. This way, when the data is inserted into the set, the data has been naturally sorted. In addition, sorted sets can be used for weighted queues. For example, the score of a common message is 1 and that of an important message is 2, then, the worker thread can obtain the task in descending order of score. Give priority to important tasks.
- Weighted elements, such as a game user score ranking
- Complex data structures. Generally, not many scenarios are used.
Ii. usage scenarios of other redis Functions
1. Subscription-publishing system
Pub/sub is literally publish and subscribe. In redis, you can set a key value for message publishing and message subscription, after a message is published on a key value, all clients that subscribe to it will receive the corresponding message. The most obvious usage of this function is to use it as a real-time messaging system, such as Common Instant chat and group chat functions.
2. Transaction -- transactions
Who said nosql does not support transactions, although redis's transactions does not provide strict acid transactions (for example, if a string of commands submitted and executed using exec, the server goes down during execution, some commands are executed, but the rest are not executed. However, transactions provides the basic command packaging and execution function (when the server is normal, it can be ensured that a series of commands are executed in sequence, and other client commands are inserted in the middle for execution ). Redis also provides a watch function. You can watch a key and then execute transactions. During this process, if the value of watched is modified, then this transactions will find and refuse to execute.
Use Cases of redis Data Structure