(Reprinted in this article)
What is idempotent?
Take a look at what Wikipedia says:
Idempotent: Calling a method or interface multiple times does not change the business state, ensuring that the result of repeated calls is the same as the result of a single call. 2. Use idempotent scenarios
1. Repeated front-end submissions
User registration, user creation of goods and other operations, the front end will submit some data to the background service, the background needs to create records in the database according to the data submitted by the user. If the user accidentally orders several times and the back end receives several commits, multiple records will be created repeatedly in the database. This is the bug of not having idempotent interfaces. 2. The interface times out and retry
An interface called to a third party may fail to be called due to network reasons. In this case, a retry mechanism will be added to the interface call. If a network exception occurs halfway through the first call. The next call will result in a call exception due to the presence of dirty data. 3. Repeated message consumption
When message-oriented middleware is used to process message queues and a manual ACK confirms that messages are properly consumed. If the consumer suddenly disconnects, the half-executed message is put back on the queue. When the message is re-consumed by other consumers, if there is no idempotency, it will lead to abnormal results when the message is re-consumed, such as database duplicate data, database data conflict, resource duplication, etc.
Iii. Solutions
1. Token mechanism implementation
The idempotency of the interface is realized by token mechanism, which is a relatively universal implementation method. The schematic diagram is as follows:
Specific process steps: The client will first send a request to obtain the token, and the server will generate a globally unique ID which is stored in redis as the token, and return this ID to the client. The client must carry this token when invoking the service request for the second time. The server will verify this token. If the verification is successful, the service is executed and the token in redis is deleted. If the verification fails, the redis has no corresponding token, and the operation is repeated. The specified result is directly returned to the client. It is recommended to use Lua script to realize the existence of token in Redis and the deleted code logic, ensuring that the atomic global unique ID can be generated by baidu’s UID-Generator and Meituan’s Leaf
2. Implementation based on mysql
This approach takes advantage of mysql’s unique index feature. The schematic diagram is as follows:
Specific process steps: To a table, one of the fields need to establish a unique index The client to request to the server, the server would insert the request some information to this heavy table Because of a field in the table with a unique index, if inserted successfully, prove that without the requested information in the table, execute the business logic of the follow-up If the insert fails, 3, based on redis implementation
SETNX key Value: Sets the value of the key to value if and only if the key does not exist. If the given key already exists, SETNX does nothing. This command returns 1 on success and 0 on failure. The schematic diagram is as follows:
Specific process steps: The client request to the server first, get a can represent the only request the business field The fields in the form of SETNX in the redis, and according to the business set up corresponding timeout If set success, proving that this is the first request, execute the business logic of the follow-up If set fails, represents has carried out the current request, Direct return
conclusion
In fact, these several idempotent ways are almost the same, similar to the use of state machines, pessimistic locks, optimistic locks to achieve, are relatively simple. In short, idempotence is the primary consideration when you design an interface at Lebyte, especially when you design an interface involving money such as transfer and payment.
PS: I would like to recommend a very good SELF-study course of SSM framework collection, B station: BV1BU4y1t7K1