I have mentioned it many times before with development, and today someone has made a mistake. It seems that it is not often mentioned.
Here we record all the notes that developers can see:
This3Problems often occur. Otherwise, they will cause economic losses.
1The same request is sent multiple times.
possible causes: (1) interworking with other people's interfaces. The same data is sent multiple times
(2) when you " submit " click multiple times
(3) other malicious calls, especially when it comes to payment, the risk is very high
Solution:(1)On the webpage, click"Submit"And then clickDisableDrop
(2)Perform unique key control for the received data to be inserted into the database or in other places.
It can determine the unique order number, or combine several fields, or take the time into consideration, accurate to minutes. The entire MD5 is put into a field and a unique key is added.
2Multiple requests within the same second
This is concurrency control, involving lottery, and so on, where the quantity needs to be controlled. If the control is not good, there may be situations such as excessive lottery drawing and overselling selling items.
The cause is also clear. Multiple requests are received within one second. Distributed requests may be distributed to different machines orProgramRun the command to read the counter.(Record the quantity sold)For example:1Each request performs a read operation.1, Not exceeding1And then modify the counter0And then both of them deliver the goods or send the prize. As a result, the sales volume exceeded.
Solution:
Use a database or other program with concurrency control to implement a lock Logic
If you use the database, there is a small tip for you
PseudoCodeAs follows:
// Field a stores counter numbers and controls the maximum number of prizes. The logic of one prize and multiple prizes is somewhat different, note the following pseudocode // if it is a prize select a from field B; $ A = A; if ($ A = 1) {update a = 0 where a = 1; // if the execution is successful, then you can get the prize // This way you can control the concurrency to sell only one prize} // if it is n prizes select a from field B; $ A =; if ($ A <= N) {update a = A-1 where a <= N and a> 0; // if the execution is successful, then you can get the prize // This way you can control the concurrency to sell only N prizes}
3Time-out control in Distributed Systems
If there is such a distributed business: the user purchases things, after the deduction is successful, the delivery fails, the money will be refunded to the user
IfAResponsible for processing business logic
BCharge deductionCResponsible for deliveryDRefund
Normal Logic 1 : A Call B If the deduction is successful, C Delivery
Normal Logic 2 : A Call B Deduction, successful, called C Delivery, C Delivery failed, called D Refund
So A Call C The timeout time must be large enough, greater C Time when shipment is processed
Otherwise, a situation occurs:
ACallCDelivery, timeout,AIf the shipping fails, callDThe refund is returned.CDelivery is successful,DI also refunded the money.
SoACallCThe system timeout time for delivery must be far greaterCMaximum time to process delivery