Idempotent understanding and processing

What is idempotence

Idempotent (idempotent, idempotence) is a mathematical and computer science concept commonly found in abstract algebra.

In programming, the idempotent understanding is that when the data is unchanged, an operation, no matter how many times it is executed, the result is the same

Common scenarios

  • Repeated submission of front-end data
  • Order payment request
  • Whether it is a network timeout? System bug? Or other reasons, should only be deducted once
  • push push message
  • Push multiple same messages, users will go crazy
  • Send a text message to the user
  • User orders

Wait, many familiar application scenarios need to consider idempotence, and there are many situations that cause data inconsistency or problems.

For example: Repeated clicks? repeated submit? Network resend? Or repeated data submission caused by nginx retransmission, etc.

Idempotent technical solution

Query operation When
querying once and querying multiple times, the query result is the same when the data is unchanged. select is a natural idempotent operation

Delete operation
Delete operation is also idempotent. Delete one time and multiple deletes delete data. (Note that the returned results may be different, the deleted data does not exist, 0 is returned, there are multiple deleted data, and multiple results are returned)

Unique index The
database creates a unique index or a combination of unique indexes, which can avoid dirty data and ensure idempotence, but pay attention to handling exceptions

Token mechanism When
I was doing interface development before, I thought about several issues:

  • How to ensure the uniqueness of the response result of the request interface
  • How to prevent malicious requests or attacks
  • How to ensure the response result of the request interface in a distributed environment

The result considered at that time was achieved with token + redis, and the process is as follows:

  • Before requesting, ask the server for a token, the server caches the token to redis, plus the timeout period
  • When requesting, bring token
  • The server obtains the token verification from redis, and processes, responds, and deletes the token after passing the verification. If the verification fails, it prompts that the token is invalid

note:

  • The timeout of token in redis is reasonably controlled to prevent malicious requests
  • When the server verifies the token, the redis.delete operation is used, and the direct use of get+delete is prone to concurrency problems
  • Token can limit the flow to a certain extent

to sum up

In interface development, especially in projects related to bank payments, idempotence is the primary consideration, and it is also a problem that is more common in Chinese interviews. Therefore, it is very important to understand what idempotence is and how to ensure idempotence.


Copyright statement: This article is the original article of the CSDN blogger "xiaoxiaoyunlu". It follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/afsvsv/article/details/90767910

Guess you like

Origin blog.csdn.net/weixin_44679832/article/details/105760753