The solution to closing the order without payment due to order payment timeout

The solution to closing the order without payment due to order payment timeout

insert image description here

Assuming that a customer places an order but has not paid for it, what kind of problems will arise? Let’s think about Jingdong Taobao Pinduoduo, think about how people do shopping in the scene of everyone shopping, and then take a look at our article article! ! !

Problems that may arise if the payment timeout is not closed

If the order payment timeout is not closed, it may cause some of the following problems:

  1. Inventory issues : If an item is ordered but not paid for, the inventory is locked and cannot be purchased by other customers. This can lead to confusion in inventory management, with under-stocks or over-stocks occurring.

  2. Order management clutter : Unclosed overdue orders can clutter an order management system as the system needs to keep track of a large number of unpaid orders that can pile up over time.

  3. Decreased customer experience : If a customer has trouble paying after placing an order, and then finds that the order was held for a long time without being closed, they may become frustrated and have a negative perception of your service, affecting customer satisfaction and loyalty.

    We won't say more about the others, we mainly ask questions and solve them!

Payment timeout not closed solution - stand-alone mode

In the stand-alone mode, to solve the problem that the payment timeout is not closed, various technologies and data structures can be used to manage the payment time limit of the order and automatically close the overtime unpaid order. Here are some solutions including delay queues and clock wheels in Java:

  1. Delay Queue (Delay Queue) :
    insert image description here

    A delay queue is a priority queue in which elements have a delay time. In Java, you can use java.util.concurrent.DelayQueueto implement a delay queue. You can put the order in the delay queue and set the order payment deadline as the delay time. A background thread will periodically check the elements in the queue, and once the delay time for the element is reached, it will be taken from the queue and processed accordingly, such as closing the order.

  2. Clock Wheel :
    insert image description here

    The clock wheel is a time management mechanism based on timing triggers, which can handle timing tasks in a stand-alone environment. In Java, you can use a third-party library such as "HashedWheelTimer" to implement a clock wheel. You can add orders to the clock wheel, set the cut-off time for order payment, and when the clock wheel is triggered, the overtime order can be processed.

  3. Timed task scheduler :
    The timed task scheduler in Java, such as java.util.Timerand java.util.concurrent.ScheduledExecutorService, can be used to implement timed tasks. You can create a scheduled task, set the trigger time of the task according to the order payment deadline, check the order status and close the overdue unpaid order when the task is triggered.

  4. Database timing cleanup :
    Another method is to store the payment deadline and status of the order in the database, and then use a scheduled task to poll the database to find and close orders that have not been paid after timeout. You can use the scheduled task scheduler or @Scheduledannotations in the Spring framework to implement this scheduled cleaning process.

Payment Timeout Unclosed Solution - Distributed Deployment

In a distributed deployment environment, solving the problem of unclosed payment timeout requires a more complex solution. The following are some solutions, including the application of scanning table round robin, lazy deletion, message queue and Redis, which can be compared with the above stand-alone mode:

  1. Sweep table round robin :
    insert image description here

    In a distributed environment, you can use scheduled tasks to scan the order table in the database to find out the orders that have not been paid over time, and perform corresponding processing, such as closing the order. This approach is fine for relatively simple systems, but can put a lot of pressure on the database, especially if the order volume is high. Performance can be optimized through reasonable paging query and index design.

  2. Lazy delete :
    insert image description here

    Lazy deletion means that when an order is created, the payment deadline of the order is recorded, and a scheduled task is set in the payment system to check whether these orders have timed out. If the payment is not made within the timeout, the closing operation is performed. This method can reduce the burden on the database, but it needs to ensure the accuracy and stability of the scheduled tasks.

  3. message queue :
    insert image description here

    Using message queues (such as Kafka, RabbitMQ, etc.) can effectively handle the payment timeout problem in a distributed environment. When an order is created, the order information is sent to the message queue, and the order payment deadline is set. The payment system subscribes to the message queue, and once it times out, the payment system will receive the message and perform a shutdown operation. This method can realize decoupling and asynchronous processing, and improve the scalability and stability of the system.

  4. Redis implementation :
    insert image description here

    Redis is a high-performance memory database that can be used to solve payment timeout problems. You can store the order information in Redis, and use the expiration time of Redis to simulate the payment deadline of the order. Once the order is overdue and unpaid, Redis will automatically delete the order information, and you can set a scheduled task to check the deleted order and execute the closing operation. This method can reduce the burden on the database, but you need to pay attention to data consistency and the memory limit of Redis.

  5. Distributed timing tasks :
    In a distributed environment, you can use a distributed timing task framework (such as Quartz, Elastic Job, etc.) to manage the closing operation of overtime orders. Each node can perform scheduled tasks, but it is necessary to ensure the uniqueness and consistency of the tasks to avoid repeated closing orders or missing closing orders.

overview

Whichever option you choose, there are a few things to keep in mind:

  • Thread Safety : Ensure thread safety is maintained when using these data structures in a multi-threaded environment.
  • Data consistency : Ensure that the change of order status is consistent with the timeout closure, so as to avoid repeated or missed closures.
  • Performance and Latency : Choose an appropriate data structure and timing strategy to find a balance between performance and real-time.

Choose to adapt to the situation and make a decision according to everyone's needs. There are different solutions, so learn and use! ! !

Guess you like

Origin blog.csdn.net/weixin_53742691/article/details/132240426