Original text: mp.weixin.qq.com/s/ZSQ9vCkWX…

After two weeks, I finally got my liver out, and finally finished the brain map of Redis related problems!! At the end of the article no routine share ~~ attached to get way!

The body of the

Redis, as a NoSQL in-memory database, has been loved by the majority of users since its launch for its rich data types, easy-to-use commands and high concurrency of up to 100,000 per machine (official data). The five data types of Redis are the required courses for us to learn Redis, but most people just learn its commands and APIS, but do not know which scenarios these data types can be applied to, so these commands will soon be forgotten, after all, just “on paper”.

Using these five data types will make your development a lot easier, will give your application a lot of performance improvement, and these five data types can play a lot of tricks.

However, most students only used 1-3 of Redis five data types in the actual development process, and some even used only one String type. Either string is sufficient for a business scenario, or you simply don’t know or don’t think another data type is more appropriate, so even if there are scenarios that are more suitable for a different data type, you may not realize it. So today we’re going to talk about the various data types of Redis and what they apply to.

This question is also the opening line of an interview when asked about Redis for two main reasons:

First, see if you have a comprehensive understanding of what functions Redis has, how to use it in general, what data types are used in what scenarios, even if you only know the simplest KV operation





Second, how have you played Redis in actual projects? Are you experienced

If you give a bad answer that doesn’t include a number of data types or scenarios, you’re done. The interviewer will probably think that you just do simple sets and gets without thinking about them.

Without further ado, let’s get to the point. Redis provides a total of five data types, namely String, Hash, List, Set, sorted Set (Zset). The following describes the basic common commands and usage scenarios of each data type.

String String

String Common command for the String structure

# Common string operationsMSET key value [key value...] SETNX key value // Store a non-existing string key value // obtain a string key value MGET key [key...] DEL key [key...] // Delete key EXPIRE key seconds // set key expiration time (s)Student: atomic plus or minusINCR key // Increase the number stored in the key by 1 DECR key // Decrease the number stored in the key by 1 INCRBY key Increment // Increase DECRBY key Decrement // Subtract decrement from the values stored in keyCopy the code

Here’s a list of common String commands, and let’s see where String commands can be used.

Application scenarios

1. Single-value caching

That is, the simplest key-value set and GET, such as the cache of an identifier, switch, etc

SET key value
GET key
Copy the code

Object caching

In addition to single-value caching, we can also use strings to cache objects in two ways:

# 1SET user:1 value(JSON string) GET user:1# 2MSET user:1:name Programming avenue user:1:sex 1 MGET user:1:name user:1:sexCopy the code

The first method is to convert the object into a JSON string and store it as a value in Redis. This method is relatively easy to obtain the object, and you can get the value directly from the key and convert it into an object. However, the disadvantage is that if you change a field of the object, you also have to deserialize the entire json string of the object. This will lead to unnecessary network overhead (even redis memory, but our actual application server and redis is isolation, do not underestimate network transmission overhead), and by the same token, frequent serialized deserialization will also bring a big performance overhead, if for system performance requirements are higher this would be a disaster.

While the second mode of storage object for the frequent change in one object field scene is friendly, each field and value are a kv to modify directly set k v cover, but it is not so easy when storing multiple fields, fortunately have mset batch operation command, network overhead by repeatedly into one.

3. Distributed locks

The following setnx command is short for set if not exit, which means to execute the set if the key does not exist. If the command is executed successfully by multiple threads, only one thread is considered to have obtained the lock. Then the thread that gets the lock performs the operation, deletes the lock, and releases the lock.

#setnx key value
SETNX  product:10001  trueSETNX product:10001trueDEL product:10001 // Release the lock after services are performedCopy the code

The problem with this approach is that the program terminates unexpectedly and the lock cannot be released, resulting in a deadlock. You can run the following command to set both the distributed lock and the key expiration time

SET product:10001 trueEx 10 nx // Prevents unexpected program termination from causing deadlocksCopy the code

Points Redis cloth lock detailed implementation can refer to the Redis distributed lock combat I wrote before

4. Counter

INCR article:readcount:{article id} GET article:readcount:{article id}Copy the code

Based on the Redis atomic increment command INCR can achieve such functions as counters, we all know that the public number of articles, weibo, blog have a reading amount of the concept, we can use this counter to achieve, and the performance is very high.

For example, the read count in the figure below can be implemented using redis increment

5. Web cluster Session sharing solution

In the case of cluster deployment, session sharing is the first problem to be considered. We can transfer the session managed by Tomcat to Redis to realize the function of distributed session. The Spring framework provides a solution for session sharing, that is, Spring Session + Redis realizes distributed session.

6. Distributed system global serial number

To ensure global sequence number uniqueness in distributed systems, Redis can be used to maintain an increment sequence.

Run the following command to obtain the self-added ID from Redis:

#INCR is an atomic increment command
INCR orderId
Copy the code

In the distributed system, Redis is used to ensure the self-increment and uniqueness of the ID. You need to interact with Redis every time to obtain the ID through this command. If the traffic is heavy, this will be very frequent.

So you can get a certain number of ids at once and store them in JVM memory, and then get them from Redis when you run out. This reduces frequent network overhead, but the downside is that you may lose (or waste) some oF your ids, because the service may hang after you get it, and the unused ids may be wasted (you can use some means to ensure that you don’t waste, but it’s not necessary to waste a little).

As follows, 1000 at a time

# Redis batch generates serial numbers to improve performance
INCRBY  orderId  1000
Copy the code

HASH structure

Common Hash Operations

HSET key field value HSETNX key field Value HSETNX key field Value HMSET key field value [field value... HGET key field HMGET key field [field...] HDEL key field [field...] HLEN key HGETALL key HINCRBY key field HGETALL key HGETALL key HINCRBY key field Increment // Add increment increment to the field key in the hash tableCopy the code

Application scenarios

Object caching

Combined with the key-field-value feature of the HASH structure, which is similar to the HASH map in Java and is also in the form of “key-value” internally, field can just store the attribute name of the object. Suppose there is the following data:

We can use the HMSET command to set the field-value in batches. The ID of the user is concatenated before to ensure that the data of multiple users will not be repeated. HMGET batch fetch field; MSET modifies a field.

HMSET achievement {userId}:name Xiaoming {userId}:score 89 HMSET achievement 1:name Xiaoming 1: Score 89 HMSET achievement 2:name Xiaohua 2:score 92 HMGET achievement 1:name 1:scoreCopy the code

The relationship between the object and HSAH becomes the following

2. E-commerce shopping cart

Regular operation of shopping cart can be realized by taking user ID as key, commodity ID as field and commodity quantity as value.

Shopping cart operation:

# Add goods
hset cart:10001 50005 1
# Increase the quantity of an item
hincrby cart:10001 50005 1
# Total number of items in cart
hlen cart:10001
# delete merchandise
hdel cart:10001 50005
Get all items in cart
hgetall cart:10001
Copy the code

Several common operations corresponding to the shopping cart can be imagined using Redis how to achieve

Advantages and disadvantages of ###Hash

advantages

  • Classify and store similar data (with the same key) for convenient data management

  • Less memory and CPU consumption than String operations

  • More space efficient than String storage

disadvantages

  • Expiration can’t be used on fields, only on keys

  • Redis cluster architecture is not suitable for large-scale use

The List structure

List Common Operations

We can think of the left side of the list as head and the right side as tail

Common commands

LPUSH key value [value ...] // Insert one or more values into the head of the key list (leftmost)Copy the code

Application scenarios

1. Implement common data structures

List-based features and rich commands enable common centralized data structures:

1) Stack = LPUSH + LPOP, FILO first in, then out

Combining LPUSH and LPOP commands to realize the advanced and then out characteristics of the stack, LPUSH from the left side of the stack, LPOP from the left side of the stack, enter first and then out. The entrance and exit are one.

Queue = LPUSH + RPOP, FIFO first in first out

The combination of LPUSH and RPOP commands to achieve the first in first out characteristics of the queue, LPUSH from the left in the queue, RPOP from the right out of the queue, first in first out. The entrance and exit are on both sides.

3) Blocking MQ = LPUSH + BRPOP = LPUSH + BRPOP = LPUSH + BRPOP = LPUSH + BRPOP = LPUSH + BRPOP

2. Microblog message and wechat official account message

For example, Walking himself follows big VS like People’s Daily Online, Huawei China and Beijing-Hong Kong Metro. If People’s Daily Online sends a microblog with ID 30033 and I follow him, then I will push this microblog ID to my MSG queue. When I open my microblog, I will take the first few weibo ids from my own MSG queue and show them to me, so this brings up a few key points:

1) People's Daily Online sent a microblog with ID 30033, and the message ID joined the team

LPUSH  msg:{walking-ID}  30033
Copy the code

2) Huawei China sends a microblog with ID 30055, and the message enters the team

LPUSH  msg:{walking-ID} 30055
Copy the code

3) IF I log in, it will show me the latest micro blog messages, so take the latest top 5 messages from my message queue and display them on the home page

LRANGE  msg:{walking-ID}  0  5
Copy the code

The SET structure

Set common operations

SADD key member [member ...] SREM key member [member...] SREM key member [member... Smember key smember key SCARD key SISMEMBER key member SRANDMEMBER key [count] SPOP key [count] SRANDMEMBER key [count] SPOP key [count] SRANDMEMBER key [countCopy the code

Set operation

SINTER key [key ...] // Intersection operation SINTERSTORE destination key [key..] SUNION key [key..] SUNIONSTORE destination key [key...] SDIFF key [key...] SDIFFSTORE destination key [key...] // Store the difference set result ina new collection destinationCopy the code

Application scenarios

1. Wechat lottery small program

You must have used the small lottery program in Wechat, as shown in the picture below, we can click to participate in the lottery immediately, and view all participants. Finally, there are three key points

Let’s look at how these three key points are implemented with the set data type:

1) Click to participate in the lottery and add the user ID to the collection

SADD key {userlD}
Copy the code

2) View all users participating in the lottery

SMEMBERS key
Copy the code

3) Draw count winners

SRANDMEMBER key [count]// Returns but does not followsetIn rejectingCopy the code

If the first prize second prize third prize… , and each person can only get one, you can use SPOP key Count

2. Likes, favorites and labels on wechat and Weibo

For example, Walking posted a post on his moments and someone liked it

1) "like" adds the ID of the person who "like" to the set of likes

SADD like:{message ID} {user ID}Copy the code

2) Unlike removes the user ID from the collection

SREM like:{message ID} {user ID}Copy the code

3) Check whether the user has clicked "like"

SISMEMBER like:{message ID} {user ID}Copy the code

4) Get a list of likes

SMEMBERS like:{message ID}Copy the code

5) Get the number of likes

SCARD like:{message ID}Copy the code

The application scenario of Set operation

Based on the rich commands provided by the Redisset collection, we can easily implement intersection and difference operations on the collection. For example, the existing sets set1, set12, set3 have the following elements:

set1: {a, b, c}Copy the code

Perform intersection, union, and difference operations on sets

SINTER set1 set2 set3 // Intersection --> {c}Copy the code

With these basic operations we can see what business requirements can be achieved.

3. Collective operation to realize the social software concern model

The user following model of social software, such as QQ friends, Weibo followers, Douyin, Kuaishou followers, wechat official account followers, all of these social software will do such a function, that is, the user relationship of the following model recommendation, including common followers, people you may know,

First, let’s take a look at the people walking, Chenmowanger and Hollis paid attention to as follows:

1) Walking:

walkingSet-->{chenmowanger, ImportNew, Hollis}
Copy the code

2) People chenmowanger cares about:

chenmowangerSet-->{walking, ImportNew, Hollis, JavaGuide}
Copy the code

3) People concerned by Hollis:

HollisSet--> {waking, ImportNew, JavaGuide, feichao, CodeSheep}
Copy the code

(Just kidding, they don’t follow me, haha 😂)

Everyone’s follow list is a set of Redis, and when walking hits Chenmowanger’s home page, there’s an area dedicated to my and my brother’s concerns:

4) Common concern of Walking and Chenmowanger:

Which is who’s in my set and who’s in my brother’s set

SINTER walkingSet zhangyixingSet--> {ImportNew, Hollis}Copy the code

5) The people I care about also care about him (Chenmowanger):

See if there is someone in the list of people I follow. For example, if I go to Chenmowanger’s home page, I can show who else in my following list also follows Chenmowanger

SISMEMBER ImportNewSet chenmowanger
Copy the code

6) People I might know:

Seek difference set, in front of this set shall prevail, look at the second brother concerned about those people which I have not paid attention to, so I hastened to pay attention to JavaGuide (Guide brother)

SDIFF chenmowangerSet walkingSet->{walking, JavaGuide}
Copy the code

4, set operation to achieve e-commerce commodity screening

Let’s see if this graph is familiar. When you buy a mobile phone, there is a screening function

As shown in the picture above, e-commerce website to buy mobile phone, enter this page according to various conditions to search mobile phone, we think how to achieve with Redis? (Of course, this is not to say that people use Redis to implement this set of search, in fact, mainly use the search engine middleware, here is only to show that you can use Redis to implement ~)

Maintain the goods when putting the goods on the shelf. Add the corresponding goods to the corresponding set at the same time, as shown in the following example

// Brand - Huawei SADD Brand: HUAWEI P30 Mate30 Glory Play4 Nova7 // brand - Xiaomi SADD Brand: Xiaomi MI6 MI8 MI9 MI10 // brand -iPhone SADD Brand :iPhone iphone8 iphone8Plus iphoneX iphone11 // Operating system -Android SADD OS: Android P30 Mate30 Glory Play4 Nova7 MI6 MI8 MI9 Mi10 //CPU brand - Snapdragon SADD CPU :brand: Xiaolong iphone8 iphone8Plus iphoneX iphone11 MI6 MI8 MI9 MI10 CPU brand - Kirin SADD CPU: Brand: Qilin P30 Mate30 Glory Play4 Nova7 // Running Memory -8G SADD RAM :8G P30 Mate30 Glory Play4 Nova7 MI6 MI8 MI9 MI10 iphone8 Iphone8plus iphoneX iphone11 SINTER OS: Android CPU: Brand: Xiaolong RAM :8G -->{MI6 MI8 MI9 MI10}Copy the code

Screenshots are easier to see:

Assuming that we maintain various brands, such as the operating system to which the phone belongs, CPU brand, running memory, etc., then we can use the selected sets to find the intersection of his line when checking conditions.

ZSet ordered set

A zset is an ordered collection of sets, sorted by the score passed in

ZSet Common operations

ZADD key score member [score member]... // add ZREM key member [member...] ZINCRBY key increment member ZINCRBY key increment member ZINCRBY key increment member ZINCRBY key increment member ZREVRANGE key start stop [WITHSCORES] ZREVRANGE key start stop [WITHSCORES] ZREVRANGE key start stop [WITHSCORES]// Retrieves the ordered set key from start subscript to stop subscriptCopy the code

Zset set operation

ZUNIONSTORE destkey numkeys key [key ...] ZINTERSTORE destkey numkeys key [key... // Calculate the intersectionCopy the code

Application scenarios

1, Zset set operation to achieve the leaderboard

As we all know, weibo hot list, news hot list, poll hot list and so on all have a ranking concept. Baidu Hot List as shown in the following figure shows real-time news with high clicks (assuming the ID of these news is 1001-1010). Each news has a hot value. 1002, this is 467W, real time, maybe we’ll see it later, so let’s see how it works with Redis.

1) Click the news

Every time someone clicks on the news, the ius adds 1 to his score

ZINCRBY hotNews:20200722 1 1001 // The score for news with ID 1001 is increased by oneCopy the code

2) Display the top 10 of the day

Take the first 10 elements of the set

ZREVRANGE  hotNews:20200722  0  10  WITHSCORES
Copy the code

3) Seven-day hot list calculation

ZUNIONSTORE  hotNews:20200715-20200721  7 hotNews:20200715 hotNews:20200716... hotNews:20200721
Copy the code

4) Display the top ten of the seven days

ZREVRANGE hotNews:20190813-20190819  0  10  WITHSCORES
Copy the code

More Application Scenarios

  • Wechat < shake >< Grab a red envelope >

  • Didi Dache, Mobike < nearby car >

  • Meituan and Ele. me (Nearby Restaurant)

  • Search auto completion

  • Bloom filter

conclusion

This article mainly describes the five Redis data types can use the corresponding command to achieve business scenarios, through our life common business scenarios to help understand the use of Redis data types, combined with the scene can also facilitate us to more vividly understand and learn the Redis data types of various operation commands. At the same time, Walking also hopes that the guidance of this article can inspire readers to Redis data type application scenarios, so that they can think about whether it is better to use Redis when designing the system. It can also be a reference to you. Once again, some of the application scenarios listed in this article may not be the most appropriate to use Redis, and some are just explaining that you can use Redis to achieve, I hope you can consider how to design according to your actual business scenarios. Please help to point out any questions. At last, I hope this article will be helpful to you. If it is helpful, please give walking a little encouragement and like it ❤ and forward 🤞

Don’t go away, there are eggs ↓ ↓ ↓

Walking personal maintenance of this public account has been two years, at first is for their own summary, later some people pay attention to, I think through my articles and sharing can help more people that is very worthwhile. Thanks to the friends who have been following this public account ❤~

Although the output is not high, I have been writing, also wrote a lot of articles, each article is after a few days or even a week of polishing, deliberation before dare to send out. But I have been indifferent, even if no one pays attention to, no one praises, I will stick to it, because here is my accumulation, is my traces ~

We have any suggestions or opinions welcome to leave a message or private letter oh ~ below is I sorted out two weeks of Redis knowledge, make a mind map, convenient for everyone to review, has turned into a variety of formats, welcome to pay attention to the public number programming avenue, reply: Redis, free access to ****


Reference: www.bilibili.com/video/BV1if…