What is idempotency?

Generally, if the read service fails to be invoked during service invocation, the read service will be automatically transferred to other services according to the configured times. However, the write service cannot repeat the request. If the invoked service does not return a successful response due to timeout or network failure, the service caller will consider it as a failure, but it is likely that the service has succeeded. If repeated requests for write services, such as transfer services, may cause serious consequences. Therefore, write service failure should not be designed to continue to make repeated requests. The called service should also be designed to be idempotent, even if repeated requests are made, there is no impact.

Knowing the background above, idempotence means that the same parameters, repeated requests for the same service, must get the same result.

Idempotent design

Take a payment scenario, request a third-party payment interface to initiate the payment function, the same order number, the same amount of information, return is successful. The same order number, different amount of information, return is the order number repeat. This is idempotent design. If the request parameters are consistent with the information in the existing database, the third-party payment directly returns the existing success data. If the data is inconsistent and the order number is repeated, the third-party payment directly returns the same order number. If the idempotent design is not done, the same order number, the same amount of information, repeated payment, may cause the amount of accumulation. For service friendliness, it is not friendly to return the same order number with the same amount of information repeatedly.

Some services are inherently idempotent, such as changing a user’s email address, gender, etc., so that no matter how many times you repeat the request, the result will be the same.

Therefore, the key point of service idempotent design is to verify the validity of request parameters and the comparison of existing data. If the same request parameter has already been processed, do not process it again and return it directly. This is the idempotent core point.

Recommended reading

Dry goods: Free 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.