I. Redis Data Structure Usage Scenarios
I previously read the book "redisbook" and am already familiar with Redis's basic functions. I started reading Redis's source code last week. The current goal is to thoroughly understand Redis's data structures. As we all know, there are 5 data structures in Redis in total. What are the usage scenarios for each data structure?
- String — string
- Hash — dictionary
- List — list
- Set — set
- Sorted Set — sorted set
Below we will briefly explain their respective usage scenarios:
1. String — string
The String data structure is a simple key-value type. The value can not only be a String, but also a number (when the numeric type can be represented by Long, the encoding is integer; otherwise, it is stored in sdshdr as a string). Using the String type, you can fully implement the current functionality of Memcached with higher efficiency. You can also enjoy Redis's scheduled persistence (with RDB mode or AOF mode to choose from), operation logs, Replication, and other features. In addition to providing the same get, set, incr, decr and other operations as Memcached, Redis also provides the following operations:
LEN niushuai: O(1) get string lengthAPPEND niushuai redis: Append content to the string, and it uses smart memory allocation (2x each time)- Set and get a segment of the string
- Set and get a certain bit of the string
- Batch set the contents of a series of strings
- Atomic counter
- The clever use of the GETSET command: set a new value while clearing the old value, and use it together with the atomic counter
2. Hash — dictionary
In Memcached, we often package some structured information into a hashmap, serialize it on the client side, and store it as a string value (usually in JSON format), such as a user's nickname, age, gender, points, etc. At this point, when you need to modify one of these items, you usually need to take out the string (JSON), deserialize it, modify the value of that item, then serialize it back into a string (JSON) and store it again. Simply modifying one attribute requires doing so many things, which is certainly very costly and not suitable for situations that may involve concurrent operations (for example, two concurrent operations both need to modify points). Redis's Hash structure, however, allows you to modify only the value of a certain attribute, just like updating an attribute in a database.
- Store, read, and modify user attributes
3. List — list
List is, frankly speaking, a linked list (Redis implements List using a doubly linked list). Anyone who has learned data structures should be able to understand its structure. With the List structure, we can easily implement functions such as ranking of latest messages (for example, Sina Weibo's TimeLine). Another application of List is as a message queue: you can use List's *PUSH operations to store tasks in a List, and then worker threads use POP operations to take out the tasks for execution. Redis also provides an API for operating on a segment of elements in a List, so you can directly query or delete a segment of elements in the List.
- Weibo TimeLine
- Message queue
4. Set — set
A Set is a collection; the concept of a collection is a combination of a bunch of non-repeating values. Using the Set data structure provided by Redis, you can store some collection-type data. For example, in a Weibo application, you can store all the users a user follows in one set, and store all their fans in another set. Because Redis, in a very user-friendly way, provides operations such as intersection, union, and difference for sets, you can very conveniently implement functions such as mutual follows, common interests, and second-degree friends. For all the above set operations, you can also use different commands to choose whether to return the result to the client or store it in a new set.
- Mutual friends, second-degree friends
- Using uniqueness, you can count all unique IPs that visit the website
- When recommending friends, find the intersection based on tags; if it is greater than a certain threshold, you can make a recommendation
5. Sorted Set — sorted set
Compared with Sets, Sorted Sets add a weight parameter, score, to the elements in a Set, so that the elements in the set can be arranged in order by score. For example, in a Sorted Set storing the scores of all classmates, its set value can be the student ID, and the score can be their exam score. In this way, when data is inserted into the set, it is already naturally sorted. In addition, Sorted Sets can be used as a weighted queue. For example, normal messages have a score of 1, and important messages have a score of 2; then worker threads can choose to fetch work tasks in descending order of score, allowing important tasks to be executed first.
- Elements with weights, such as a game's user score leaderboard
- A relatively complex data structure, generally not used in too many scenarios
II. Other Redis Feature Usage Scenarios
1. Publish-Subscribe System
Pub/Sub, literally understood, is publishing (Publish) and subscribing (Subscribe). In Redis, you can set up message publishing and message subscription for a certain key. When a message is published on a key, all clients subscribed to it will receive the corresponding message. The most obvious use of this feature is as a real-time messaging system, such as ordinary instant messaging, group chat, and other functions.
2. Transactions
Who said NoSQL doesn't support transactions? Although Redis's Transactions do not provide strict ACID transactions (for example, if a series of commands submitted for execution via EXEC causes the server to crash during execution, then some of the commands will have been executed and the rest will not), the Transactions still provide the basic function of packaging commands for execution (when the server does not have problems, it can guarantee that a series of commands are executed sequentially together, and no other client commands will be inserted in between). Redis also provides a Watch feature: you can Watch a key and then execute Transactions. During this process, if the watched value is modified, the Transactions will detect it and refuse to execute.