什么是幂等运算?
当前回答
相当详细和专业的回答。只是添加了一个简单的定义。
幂等=可重复运行
例如, 如果多次执行Create操作,则不能保证运行时没有错误。 但是如果有一个CreateOrUpdate操作,那么它声明了可重运行性(等幂)。
其他回答
my 5c: In integration and networking the idempotency is very important. Several examples from real-life: Imagine, we deliver data to the target system. Data delivered by a sequence of messages. 1. What would happen if the sequence is mixed in channel? (As network packages always do :) ). If the target system is idempotent, the result will not be different. If the target system depends of the right order in the sequence, we have to implement resequencer on the target site, which would restore the right order. 2. What would happen if there are the message duplicates? If the channel of target system does not acknowledge timely, the source system (or channel itself) usually sends another copy of the message. As a result we can have duplicate message on the target system side. If the target system is idempotent, it takes care of it and result will not be different. If the target system is not idempotent, we have to implement deduplicator on the target system side of the channel.
幂等操作:多次执行没有副作用的操作。 示例:从数据资源检索值并打印值的操作 非幂等操作:多次执行会造成伤害的操作。(当它们改变某些值或状态时) 示例:从银行账户提款的操作
无论调用该操作多少次,结果都是相同的。
对集合的幂等运算在应用一次或多次时,其成员保持不变。
它可以是像absolute(x)这样的一元运算,其中x属于一组正整数。这里absolute(absolute(x)) = x。
它可以是一个二进制操作,比如集合与自身的并集总是返回相同的集合。
干杯
简而言之,幂等运算意味着无论你做多少次幂等运算都不会得到不同的结果。
例如,根据HTTP规范的定义,GET、HEAD、PUT、DELETE是幂等操作;但是POST和PATCH不是。这就是为什么有时POST被PUT取代的原因。