Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the production and consumption pattern in Redis

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to achieve the production and consumption pattern in Redis. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Simple queue implementation

So our Redis happens to have a data type that matches this, that is, List. List can implement queues (first-in, first-out) and stacks (first-in, first-out), so the list has two ways to insert data: head insertion and tail insertion. So the structure we use today is the queue, using tail interpolation, and the key commands are rpush (tail insertion) and lpop (header acquisition). As shown in the following figure:

> rpush squeue zhangsan lisi mango # insert queue (integer) 3 > lpop squeue # get queue "zhangsan" > rpush squeue wangwu (integer) 3 > lpop squeue "lisi" > lpop squeue "mango" > lpop squeue "wangwu" > lpop squeue # empty queue (nil)

The above is an example of a combination of rpush/lpop. You can also use lpush/rpop in combination with the same effect.

Note: we are thinking about a problem here, in our actual development, when we get queue data, if there is no value in the queue, we will always pop, so that our program has an endless loop, and at this time the redis will constantly process the server's pop instructions to increase its memory.

At this time, mango has a flash of light, and every time we pop, we judge that if the queue has a value, we get this value to operate. If the queue is empty, then we take a break for three seconds and then request, emmm, add drumsticks.

> lpop squeue # empty queue (nil). Sleep 3s old > lpop squeue... Queue blocking

It is a good way to control the rest time of the redis server through the back-end program, but there is a problem here. If the server is dormant, a value suddenly comes in the queue, and at this time you need to respond in time to obtain this value.

Of course, redis has long considered this problem. So provides a method called queue blocking read, which commands blpop and brpop, which is the blocking read method of lpop and rpop.

Blpop [first parameter: key]... [second parameter: time] key can have multiple key waiting for time is blocking time. Default is second.

Well, it seems to solve the above method perfectly.

Here's the problem again. If it takes a little longer to set up here, and there are a little more client connections blocking requests, then the next problem will be idle connections. If the thread has been blocked somewhere, the Redis client connection will become an idle connection, idle for too long, the server will generally take the initiative to disconnect, reducing the occupation of idle resources. At this point, blpop/brpop will throw an exception.

Therefore, you can't have both. Developers should note that the exception needs to be caught here, and then request it again.

Delay queue

We introduced the blocking queue above, knowing that there is an abnormal problem when idle connections are reclaimed, so can we implement a delay queue? I get the elements in the queue a period of time in advance, such as 5 seconds. When the recorded time of the element arrives, I execute this value, and then five seconds in advance to get the value in the queue, repeated in turn. It is guaranteed that I will execute the corresponding time value in the queue at a certain time.

Let's analyze, first make sure that the queue is orderly before we can execute it in turn. Here we use ZSet because it has sorting and does not repeat, to ensure that the client does not submit duplicate data, then the value is guaranteed, how to design this sort? We can't use 'yyyyMMddHHmmssSSS', the first one that doesn't fit this sort type may exceed, and the second is that each conversion consumes the operation conversion, so here we use a timestamp. Then zset provides an instruction zrangebyscore to get a certain segment of existing data, which can get the data we want, and then delete the value in zset through zrem to complete the consumption.

# assume that the current timestamp is 0 > zadd dqueue 4 zhangsan 8 lisi 12 mango # add element (integer) 3 deleted timestamps # data 5 seconds after early acquisition > zrangebyscore dqueue-inf 5 withscores # data within 5 seconds ahead of time 1) "zhangsan" # value 2) 4.0 # current sorting index > zrem dqueue zhangsan # delete elements after fee removal # the current timestamp has reached 5 seconds Add this > zrangebyscore dqueue-inf 10 withscores # to get data less than 10 seconds in advance, and call 1) "lisi" 2) 8.0 the above is how to implement the production and consumption pattern in Redis. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report