3

A fan who has worked in a traditional industry for 7 years sent me a private message.

He recently went to interviews with many Internet companies, and encountered many technologies and concepts that he had never heard of.

One of the questions is: "What is idempotency and how to solve the problem of idempotency"?

He said that he had never heard of this concept, so how could he answer it.

Ok, for this question, look at the answers of ordinary people and experts.

Ordinary people:

Um. . . . . . . . . . . . . .

Expert:

OK

The so-called idempotency is actually a mathematical concept. In the field of computer programming, idempotency means that when a method is repeatedly executed many times, the impact is the same as the impact of the first execution.

The reason for considering idempotency is because in network communication, there are two behaviors that may cause the interface to be executed repeatedly.

  1. The user's repeated submission or the user's malicious attack will cause the request to be repeated many times.
  2. In a distributed architecture, in order to avoid data loss caused by network communication, a timeout retry mechanism is designed when communicating between services, and this mechanism may cause the server interface to be called repeatedly.

Therefore, in the program design, for the interface of the data change class operation, it is necessary to ensure the idempotency of the interface.

The core idea of idempotency is to ensure that the execution result of this interface is only affected once, and subsequent calls will not affect the data. Therefore, based on such a demand, there are many common solutions.

  1. Use the unique constraint of the database to achieve idempotency. For example, for the scenario of data insertion, such as creating an order, because the order number is definitely unique, if it is called multiple times, it will trigger the unique constraint exception of the database, thereby avoiding the creation of multiple requests for one request. problem with an order.
  2. Use the setNX instruction provided in redis. For example, in the scenario of MQ consumption, in order to avoid the problem of repeated data modification caused by MQ consumption, you can write the message into redis through setNx when receiving the MQ message. Once This message has been consumed, and will not be consumed again.
  3. Use a state machine to achieve idempotency. The so-called state machine refers to the conversion process of the complete running state of a piece of data, such as the order state, because its state will only change forward, so when the same data is modified multiple times, once the state If a change occurs, the impact on this data modification will only occur once.

Of course, in addition to these methods, it can also be implemented based on the token mechanism, deduplication table and other methods, but no matter what the method is, there are nothing more than two types.

  • Either the interface is only allowed to be called once, such as unique constraints and redis-based locking mechanisms.
  • Either the impact on the data will only be triggered once, such as idempotency, optimistic locking

The above is my understanding of the problem.

Summarize

The development of the technology industry is rapid, if one's own technical capabilities and cognition cannot keep up with the changes.

That's basically obsolete, so it's very important to keep learning.

Friends who like my works remember to like and favorite.

If you encounter some questions that you don't understand during the interview, you can feel free to private message me at any time

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。