How to ensure the idempotency of the interface

What is idempotency

An interface is called repeatedly, and the impact on the system is consistent. Whether it is one or more times, the data is consistent and the effect is the same.

When is idempotent needed?

SELECT col1 FROM tab1 WHER col2 = 2, no matter how many times it is executed, it will not change the state, which is a natural idempotent.
UPDATE tab1 SET col1 = 1 WHERE col2 = 2, the state is consistent no matter how many times the execution is successful, so it is also an idempotent operation.
UPDATE tab1 SET col1 = col1 + 1 WHERE col2 = 2, the result of each execution will change, this is not idempotent.
insert into user (userid, name) values ​​(1, 'a') If userid is the only primary key, that is to repeat the above operation, only one piece of user data will be inserted, which is idempotent.
If the userid is not the primary key, it can be repeated. If the above business is operated multiple times, multiple data will be added without idempotency.
delete from user where userid = 1, multiple operations, the same result, with idempotency

How to guarantee idempotency

Unique primary key

Use the unique characteristics of the database primary key to solve the idempotency problem when executing the insert statement. (Invalid in case of sub-library and table division)

Deduplication table

Applicable to the insertion scenario with a unique identifier. Create a new deduplication table and add a unique index. When performing the insert operation, save the unique identifier to the unique index field of the deduplication table. Throw an exception and roll back.

Optimistic locking mechanism

Suitable for update scenarios, update t_goods set count = count -1, version = version + 1 where good_id = 2 and version = 1
optimistic locking only locks the table at the moment of updating the data, and does not lock the table at other times

State machine control

This method is suitable in the case of state machine circulation, such as order creation and payment. The payment for the order must be before. At this time, we can use the int type and the value type when designing the status field. The size is idempotent, for example, the order creation is 0, and the payment is 100. Payment failed for 99

How to ensure the idempotency of the API that provides the interface to the outside world

To provide external interfaces In order to support idempotent calls, the interface has two fields that must be passed, one is the source source, and the other is the source serial number seq. These two fields are combined and uniquely indexed in the provider system, so that when a third party calls , First check in the local system, whether it has been processed, and return the corresponding processing result; if it has not been processed, perform the corresponding processing and return the result.

Published 19 original articles · Likes2 · Visits 721

Guess you like

Origin blog.csdn.net/qq_40977118/article/details/103735229