This is the 9th day of my participation in Gwen Challenge
What is idempotence
The concept of idempotence originally comes from the field of mathematics, indicating that the result of the NTH transformation is the same as that of the first transformation. This section discusses scenarios in which the client makes multiple calls to the service when it does not achieve the desired result. To avoid side effects of repeated calls to the service resources, the service provider promises to meet idempotent requirements.
The essence of idempotency is that when the same resource is requested once or more, the results obtained are consistent. For example, the query database operation, no add, delete or change, so there is no impact on the database.
Ii. When is idempotent necessary
Take payment for orders
- The front end calls an order-creating interface, and the first call times out, and then the caller tries again
- When the order is created, we need to deduct the inventory, then the network fluctuates, the call does not take effect, the caller tries again
- The server’s withholding operation timed out when the order was created and the caller tried again
- In an order status update operation, successively created and paid status requests are sent, first paid and then created
- SMS reminders after payment completion, network delay, and repeated SMS sending
These scenarios require idempotency to prevent repeated requests or calls from causing inconsistent data and results.
3. How to ensure idempotency
The only key
Idempotent is guaranteed by a unique business order number. The same service order number can be regarded as the same service. A unique service order number is used to ensure that the processing logic and execution results of this service order number are consistent.
To create an order as an example, in the case of not considering concurrency, to achieve idempotent: ① first query whether the order has been created; (2) If yes, the creation succeeds. If not, create a new order and update the status of the order.
The unique key can be the primary key of the database table or several key values can be used together to determine uniqueness.
lock
Optimistic locking
Optimistic locking is usually used to query data. When designing the table structure, optimistic locking is performed based on the version version, which ensures both execution efficiency and idempotent. When querying data, check whether the version version is consistent.
A distributed lock
The distributed lock, for example, Redis, takes the order payment as an example. The payment operation will first check whether there is a key corresponding to the order number in Redis. If there is no key corresponding to the order number, a new key will be inserted. Redis is used to do the distributed lock, the key in the cache, so that the query efficiency is higher. Only after a payment is completed, the next operation will be carried out, and the payment action will not be executed in parallel.
token
Tokens are similar to unique keys, except that tokens are updated and replaced, and resemble tokens.
The process is that when a user creates an order, the system will cache a token value in Redis according to the user information and set an expiration time.
After the payment request is initiated, the payment system will check the token in Redis and delete the token to start the payment operation if it exists. Return an error if it does not exist.
The token in this case is a token that allows the payment system to confirm whether the user’s request is legitimate.
Prevent repeat table
This is the same reason, according to the generated in to repeat the order number in the table to insert a index, when pay order to perform the operation, no matter success or failure to perform, will be to repeat the order number corresponding to the data in the table is updated for the failure or success, when has the order request to pay again, because there have been a unique index, cannot be repeated injection, So it’s going to fail. Knowing that only the first and only request completes can result in failure or success.