I read teacher a Niu's masterpiece a few days ago Leader : Who uses the scheduled task to close the order , Get out of here ! It is found that the scheme has several flaws , Here are some suggestions for discussion .

In the electricity supplier 、 Payment and other fields , There are often such scenes , The user gave up payment after placing an order , The order will be closed after the specified time period , Careful, you must have found something like a treasure 、 A certain East has such logic , And the time is accurate , Error in 1s Inside ; How did they achieve it ?

There are several general implementation methods :

  1. Use rocketmq、rabbitmq、pulsar Wait for the delayed delivery function of the message queue
  2. Use redisson Provided DelayedQueue

There are some schemes that are widely circulated but have fatal defects , Don't use it to implement deferred tasks

  1. Use redis Expired monitoring for
  2. Use rabbitmq The dead letter line
  3. Use non persistent time wheels

redis Overdue monitoring

stay Redis The official Manual of keyspace-notifications: timing-of-expired-events It is clearly stated in :

Basically expired events are generated when the Redis server deletes the key and not when the time to live theoretically reaches the value of zero

redis The implementation of automatic expiration is : Scheduled tasks are scanned offline and deleted part Expiration key ; Lazy checking for expiration when accessing keys and deleting expired keys .redis There is no guarantee that it will be deleted and sent an expiration notice at the set expiration time . actually , It is also common that the expiration notification is several minutes later than the set expiration time .

This is a more “LOW” Solutions for , Please do not use .

Another big guy did the test Don't rely too much on Redis Expired monitoring for , If you are interested in it, you can check it by yourself .

rabbitmq Dead letter

Dead letter (Dead Letter) yes rabbitmq A mechanism provided . When a message meets one of the following conditions, it will become dead letter :

  • Message denied confirmation ( Such as channel.basicNack) And at this point requeue Property is set to false.
  • The message's lifetime in the queue exceeds the set TTL Time
  • The number of messages in the message queue has exceeded the maximum queue length

If dead letter queue is configured , Dead letter will be rabbitmq Put it in the dead letter queue .

stay rabbitmq The process of creating a dead letter queue in the :

  • Create a switch as a dead letter switch
  • Configure in the business queue x-dead-letter-exchange and x-dead-letter-routing-key, Set the switch in the first step as the dead letter switch of the service queue
  • Create a queue on the dead letter switch , And listen to this queue

Dead letter queue is designed to store messages that are not normally consumed , Easy to check and re deliver . The dead letter queue also does not guarantee the delivery time , Before the first message becomes a dead letter , The following messages will not be delivered as dead letters even if they are expired .

To solve this problem ,rabbit The official launch of the delayed delivery plug-in rabbitmq-delayed-message-exchange , It is recommended to use the official plug-in for delay messages .

Here's a digression , Use redis Overdue monitoring or rabbitmq The dead letter queue uses Middleware in a way unexpected to the designer , There are some hidden dangers in this unexpected behavior , Such as lack of consistency and reliability assurance , Low throughput 、 Resource leakage, etc . A famous example is that many people use redis Of list As message queue , So that the last author can't read and write disque And finally evolved into redis stream. Try not to abuse middleware at work , Do professional things with professional components

Time wheel

Time wheel is an excellent data structure for timed tasks , However, the vast majority of time wheel implementations are pure memory without persistence . After the process running the time wheel crashes, all the tasks in it will disappear , So I advise you to use it carefully .

redisson delayqueue

redisson delayqueue It's based on redis zset Implementation of delay queue based on .delayqueue One of them is called timeoutSetName Ordered set of , Of the elements score Is the delivery timestamp .delayqueue Can use regularly zrangebyscore Scan messages that have reached the delivery time , Then move them to the ready message list .

delayqueue Guarantee redis Messages are not lost without crashing , Try it when you don't have a better solution .

When the database index is well designed , The overhead of regularly scanning the database for outstanding orders is not as large as expected . In the use of redisson delayqueue The method of scanning the database can be used as the compensation mechanism when waiting for timed task middleware , Avoid task loss caused by middleware failure .

Conclusion

  1. First of all, it is recommended to use rocketmq、pulsar And other message queues with scheduled delivery function .
  2. When it is not convenient to obtain a professional message queue, you can consider using redisson delayqueue Based on redis Delay queue scheme , But for redis Design compensation protection mechanism in case of crash .
  3. When you can't use redisson delayqueue You can consider using the time wheel . Because the time wheel restart is far better than redis Restart frequently , Protection mechanisms such as regular library scanning are more important .
  4. Never use redis Overdue monitoring implements scheduled tasks .

Leader : Who uses it again redis Overdue monitoring enables closing orders , Get out of here ! More articles about

  1. redis Event listening and its use in the order system

    https://blog.csdn.net/qq_37334135/article/details/77717248 Usually buy good things online , Or after the mobile phone scans the code , Click pay , This will send a request to the background , Generate order letter ...

  2. springboot Use Redis, monitor Redis Key expiration event settings and usage code

    I'm using Windows Under the Redis service , So for a moment Redis The settings are all in Windows Platform operation . 1. modify Redis The configuration file 1.1:Windows Under the Redis There are two profiles Modify with servic ...

  3. Don't rely too much on Redis Expired monitoring for !!

    author : Di shell https://juejin.im/post/6844904158227595271 Redis Expired listening scenarios In the business, there is a need to wait for a certain period of time to perform a certain behavior , such as 30 Close in minutes ...

  4. spring boot Realization redis Of key Expired monitoring for , Run your own business

    In recent days, I have learned more about redis, Found out key Expired monitoring function of , The implementation is as follows : stay redis Configuration file for redis.conf Find "EVENT NOTIFICATION" modular , ...

  5. SpringBoot monitor redis Subscribe to listen and publish subscriptions

    Preface We can do it in redis Publish a subscription to the channel , All those who have listened to this channel can receive the published content ! redis Subscription listening configuration class The code is as follows : RedisListenerConfig.java package ...

  6. Java panel Panel Use , Listen for window closing events

    panel Panel Use Problems to be solved : 1. Design patterns : Adapter pattern 2.frame.setLayout(null); package GUI; import javax.swing.*; import ja ...

  7. monitor JVM close

    Use Runtime Of addShutdownHook(thread) Method : for(int i=0; i<5; i++){ System.out.println(i); } Thread th = ...

  8. JS The listening page closes

    JS You can monitor the closing of the browser page , Mainly used window Object's onbeforeunload Method In the past ( In older browsers ), You can customize the prompt copy window.onbeforeunload = functi ...

  9. Android How to listen for input method close events

    Suppose there's an interface like this ( The input field above the input method uses Dialog Realized ) When the input method is off ,Dialog And shut them down together , So the user doesn't have to return twice . I found a lot of information on the Internet, but it didn't solve this problem very well , The input method is the third equation ...

  10. Flex Monitor browser off

    stay creationComplete In the event , Add the following : if(ExternalInterface.available)// Is the external interface available     {     var js:String= " ...

Random recommendation

  1. pixi.js webgl library

    analysis pixi Source code , Just built environment gulp+webpack, I'm looking at ... https://github.com/JsAaron/webgl-demo

  2. web The difference between different device browsers in development

    It's usually used to distinguish between different devices JavaScript Medium navigator.userAgent.toLowerCase() Get the browser's userAgent Information // Use javascript Judge whether it is iPh ...

  3. servlet Read web.xml Parameters

    1 Initialize parameters init-param init-param Is configured in web.xml Of <servlet> Inside the label , in other words , It's your turn servlet All alone . example <servlet> ...

  4. QT stay Windows Output from the console

    The original address :http://blog.csdn.net/fjb2080/article/details/9013047 stay windows Under the control panel of , Need to be in pro Add... To the document : CONFIG += cons ...

  5. 【HDU 5833】Zhu and 772002( Explanation of Gauss elimination for XOR equations )

    The main idea of the topic : give n A digital a[], take a[] Decomposition into qualitative factors ( Ensure that the quality factor obtained by decomposition is not greater than 2000), Select one or more qualitative factors , Let the product be a perfect square . Find the method number . The seniors and sisters did it during the competition , At that time, I was so confused that I couldn't do it …… therefore ...

  6. Java Medium List convert to JSON Report errors ( Two )

    1. Error description Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/loggi ...

  7. jsonp Cross domain data mining

    jsonp Cross domain data mining Because browsers have the same origin policy , So if you want to get non homologous ( agreement , domain name , There is a difference among the three ports. They are all non homologous ) The page data of , We have to do cross domain (1) jsonp principle because script Labeled src Properties can be accessed ...

  8. assert (boxes[:, 2] &gt;= boxes[:, 0]).all() Report errors

    According to the error information , Print the following : The code is as follows : for i in xrange(num_images): #print ('in append_flipped==================',self ...

  9. JavaScript( Variable 、 Scope and memory issues )

    JavaScript It's a language with loose variables .( Unlike Java Just as strongly typed languages .) JavaScript There are two kinds of variables : Basic types ( Simple data segments ) And reference types ( object ). One . Basic data type (5 Kind of ) Undefine ...

  10. Java The basic chapter ——JVM And GC principle ( Dry cargo is full. )

    Originality is not easy. , If you want to reprint , Please indicate the source https://www.cnblogs.com/baixianlong/p/10697554.html , A lot of support ! One . What is? GC? GC It's garbage collection , Memory processing is ...