1. Introductory remarks
I think I am a lazy person, seldom to write something, even reading, I have never read a book. I have bought a lot of books, but almost every book did not read One-third, one is because I am lazy, followed by a book for me the amount of effective information may be less than 20% or even lower, I need to sift through some of my interests or to say useful to me, which makes me lose interest in it. I have almost all the ability to learn from the Internet, thanks to those who are willing to share their experience of colleagues. Finally determined to write some things on their own to repay, and strive to effective information volume of more than 60%, write some useful to the reader, the reader is willing to see things.
Luo Li Bar for a long, start to get to the point, this is I will write down the first blog, idempotent sex. The reason why I write idempotent is because I think idempotent is the basis of architecture design. , there's no guarantee of idempotent, and we can't talk about data consistency and the integrity of things next. 2, what is idempotent
The
is copied in a mathematical definition: F (f (x)) = f (x). X is the same as the result of function f acting once and acting infinitely. Idempotent application in a software system, I define it as: a function or an interface using the same parameters to call once or indefinitely, the consequences are the same , in the actual application of the interface is generally for the power of the design. To hold a chestnut, in the system, when caller a calls System B's interface for the user's charge operation, because the network is unstable, a retries the request n times, then regardless of how many times B receives the request, should guarantee that the user will only deduct one fee. 3, idempotent design idempotent generally used in protocol design, TCP protocol support idempotent? The answer is yes, in the network instability, the operating system can be unscrupulous to re-send TCP message fragments. The TCP protocol ensures that the core of the idempotent is the sequence number field, and that a sequential sequence will not repeat for a longer period of time. For the protocol design of the application layer, the principle is similar to TCP, and we need a serial number that is not duplicated. To put it simply, in the process of a business process, we need a non-repetitive business serial number to ensure idempotent.
for a practical scenario: User A on the Web page to launch a game recharge request, the browser guide users to the bank to pay, after the successful payment system to users to recharge.
Protocol design, we through the global unique recharge order number throughout the business process, so that the business support idempotent. In the implementation of the
application, we list the callback system after the bank pays successfully, and the steps to recharge it are explained.
Func pay_notify (orderid,value,state) {//a problematic implementation
order = Db.query ("select * from Payorder where orderid= $orderid");
Check (order,orderid,value,state);//Determine whether the payment amount is consistent with the order amount and determine if the payment is a successful callback.
if (order.state== ' not paid ') {
db.update ("Update payorder set state= ' paid ' where orderid= $orderid");
Charge (Order.username,value);//Perform recharge
}else{return
result ("Order processed")//return order processed, or return processing success
}
}
The problem with the above implementation is that when the callback appears concurrency, Order.state is already dirty read, it is possible to repeat the top-up, the implementation is not 100% guaranteed idempotent. List of three ways to improve: 1, pessimistic lock, select for update, the entire execution process to lock the record corresponding to the order. 2. Optimistic lock, affectrows = Db.update ("Update payorder set state= ' paid ' where orderid= $orderid and state= ' unpaid '), if affectrows= 1, perform the recharge, otherwise the return has been processed. 3, define the Notifylog table, OrderID is a unique key or primary key, before execution, insert, if the insert is successful to carry out the recharge, otherwise the return has been processed. The above simple example is used to illustrate the implementation of idempotent common applications, in the SOA system, may be a lot of atomic functions are split into different processes, such as charge recharge this function, perhaps in another process, the whole business will be a longer link, may be successful callback, but the recharge failed. Similarly, as long as the top-up interface to ensure the idempotent, for the callback has been but not return the result of the request, callback receiver program, should repeatedly initiate the recharge request. More in-depth, more complex scenarios, and more in data consistency.
4. SummaryWhen a business layer is designing an agreement, the requester is asked to define a business serial number that is not duplicated. When the application is implemented, the power of concurrency is ensured by using the database optimistic lock and inserting the log of unique key. Idempotent check link, in the Protocol Design Review, review the important business RPC or HTTP interface support Idempotent, code review, the focus on the request concurrency, whether it can still guarantee idempotent. Designers and concrete implementation of personnel in the implementation process, should also be at all times from the power of the realization of the implementation of the clearance.