The usage scenarios of the five data structures in Redis are introduced

  • 2020-05-09 19:36:56
  • OfStack

1. redis data structure usage scenario

Originally read the book redisbook, the basic functions of redis have been familiar with, from last week began to look at the source of redis. The goal now is to understand the data structure of redis. As we all know, there are 5 data structures in redis, so what are the usage scenarios for each data structure?

String - string
Hash - dictionary
List list -
Set collection -
Sorted Set -- ordered collection

Below, we will briefly explain their respective usage scenarios:

1. String -- string

The String data structure is of the simple key-value type. value can be either String or a number (encoding is an integer when the number type can be represented by Long; the rest is stored as a string in sdshdr). With the Strings type, you can fully implement the current Memcached functionality and be more efficient. You can also enjoy the scheduled persistence of Redis (you can choose RDB mode or AOF mode), operation logging, Replication and other functions. In addition to providing get, set, incr, decr operations like Memcached 1, Redis also provides the following operations:


1.LEN niushuai : O(1) Get string length
2.APPEND niushuai redis : to the string append Content, but also USES intelligent allocation of memory (each time 2 Times)
3. Sets and gets something of a string 1 paragraph
4. Sets and gets something of a string 1 A ( bit )
5. Volume set 1 The contents of a series of strings
6. Atomic counter
7.GETSET For the best use of the command, set it while clearing the old value 1 Two new values, used in conjunction with the atomic counter

2. Hash -- -- a dictionary

In Memcached, we often package some structured information into hashmap and store it as the value of a string (usually in JSON format) after the client serializes, such as the user's nickname, age, gender, credits, etc. When you need to modify one of the items, you usually need to extract the string (JSON), deserialize it, modify the value of the item, and serialize it back to the string (JSON). Simply changing one property to do so much must be expensive, and not suitable for situations where two concurrent operations might require modifying the integral. Redis's Hash structure allows you to change the value of only one property as you would in a database Update with one property.


Store, read, and modify user properties

3. List - list

List is basically a linked list (redis USES double-ended linked lists to implement List), which anyone who has studied data structures should be able to understand. Using the List structure, we can easily implement functions such as the latest news ranking (such as sina weibo's TimeLine). Another application of List is the message queue, which can take advantage of the *PUSH operation of List to save the task in List, and then the worker thread can use the POP operation to take out the task for execution. Redis also provides API for manipulating elements in a segment of List. You can query directly to delete elements in a segment of List.


1. weibo TimeLine
2. The message queue

4. Set - collection

Set is just a set, and the concept of a set is a combination of a heap of unrepeated values. Some aggregate data can be stored using the Set data structure provided by Redis. For example, in the microblog application, all the followers of one user can be stored in one set, and all their fans can be stored in one set. Because Redis very personalized offers set intersection, and set, difference set, such as operation, can be very convenient to realize such as mutual concern and common be fond of, 2 degrees of friends, and other functions, all of the above set operations, you can also use a different command choose to return the results to the client or set into a new collection.

1. Mutual friends and double friends
2. You can count all the independent IP that visit the website by taking advantage of the uniqueness
3. When friends recommend you, find the intersection according to tag. If it is larger than a certain threshold, you can recommend it

5. Sorted Set -- an ordered collection

Compared with Sets Sorted Sets is increased by 1 of the elements in Set score weighting parameters, make the elements in the collection by score ordered arrangement, such as a storage performance of the whole class Sorted Sets, its collection value can be the student number of students, and score can is the exam score, so that when data is inserted into the collection, is the natural order. In addition, Sorted Sets can be used to make a weighted queue, for example, score for normal messages is 1, score for important messages is 2, and then the worker thread can choose to get the work task in reverse order of score. Prioritize important tasks.

1. Elements with weights, such as the leaderboard of a game's users
2. For complex data structure, there are not too many scenes used in 1

2. Usage scenarios of other functions of redis

1. Subscription-publish system

Pub/Sub literally means publish (Publish) and subscribe (Subscribe). In Redis, you can set a message to publish and subscribe to a certain key value. When a message is published on an key value, all clients that subscribe to it will receive the corresponding message. The most obvious use of this feature is as a real-time messaging system, such as regular im, group chat, etc.

2. Transaction - Transactions

Who says NoSQL doesn't support transactions, although Redis Transactions provides is not strict ACID transactions (such as string of 1 with EXEC submit the command executed, in the execution server goes down, so there will be a part of the command execution, the remaining didn't execute), but the Transactions or provides basic commands to perform packaging function (in the case of a server is not a problem, can guarantee a series of commands is order in 1, is there any other client orders, there is inserted). Redis also provides an Watch function. You can Watch an key and then Transactions. In the process, if the value of Watched is changed, Transactions will find it and refuse to execute it.


Related articles: