How to Prevent Lost Orders in Payment Systems: Proven Backend Strategies
This article explains the typical order‑payment flow, identifies common causes of lost orders—both external and internal—and provides concrete backend techniques such as intermediate payment states, timeout queries, idempotent notifications, and Redis‑based duplicate‑submission prevention to ensure reliable order processing.
In a simplified order process, a user submits an order, then proceeds to payment through a payment gateway which interacts with third‑party channels (WeChat, Alipay, UnionPay). After a successful payment, the gateway sends an asynchronous notification to the payment center, which updates its own order status and then notifies the business application; each business updates its own order status accordingly.
During this process, order loss can occur. External loss happens when callbacks are missed or the program crashes; internal loss occurs due to failures in subsequent steps. Both result in a paid order whose status is not updated, leading to complaints or duplicate payments.
Add an intermediate status "paying" to the payment record. When the same order attempts payment, check for a "paying" record and lock the pre‑pay operation. After payment succeeds, update the record to "paid".
Define a timeout (e.g., 30 seconds) in the payment center. If no success callback is received within this window, actively query the payment result at intervals (e.g., 10 s, 20 s, 30 s). If the maximum number of queries is reached without a result, handle the exception.
When the payment center receives the result, synchronize it to the business system via MQ or direct calls; direct calls should include retry mechanisms such as Spring Boot Retry.
Both the payment center and business applications must ensure idempotency when processing payment notifications, handling each message only once.
The business application should also proactively query the payment result on timeout.
Implement a scheduled task that scans a table of pending payment orders and performs the timeout queries.
To prevent duplicate order submissions, compute a hash of the order information and check Redis for an existing key. If the key exists, reject the duplicate; otherwise, create a new key with an expiration time and proceed with order creation, effectively blocking identical operations within a short period.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
