什么是幂等运算?


当前回答

任何操作,每n个结果都会产生与第1个结果值匹配的输出。例如,-1的绝对值是1。-1的绝对值的绝对值是1。-1绝对值的绝对值的绝对值等于1。等等。请参见:什么时候使用递归是非常愚蠢的?

其他回答

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.

幂等操作是一种可以应用多次而不改变结果(即系统状态)的操作、动作或请求,超出初始应用。

示例(web应用上下文):

IDEMPOTENT: Making multiple identical requests has the same effect as making a single request. A message in an email messaging system is opened and marked as "opened" in the database. One can open the message many times but this repeated action will only ever result in that message being in the "opened" state. This is an idempotent operation. The first time one PUTs an update to a resource using information that does not match the resource (the state of the system), the state of the system will change as the resource is updated. If one PUTs the same update to a resource repeatedly then the information in the update will match the information already in the system upon every PUT, and no change to the state of the system will occur. Repeated PUTs with the same information are idempotent: the first PUT may change the state of the system, subsequent PUTs should not.

非幂等性: 如果一个操作总是导致状态的变化,比如反复向用户发送相同的消息,导致每次都发送新消息并存储在数据库中,我们称该操作为NON-IDEMPOTENT。

NULLIPOTENT: 如果一个操作没有副作用,就像仅仅在网页上显示信息而没有对数据库进行任何更改(换句话说,你只是在读取数据库),我们说这个操作是NULLIPOTENT。所有get都应该是无效的。

当谈论系统的状态时,我们显然忽略了无害的和不可避免的影响,如日志和诊断。

幂等性意味着应用一次操作或应用多次操作具有相同的效果。

例子:

乘以0。无论你做多少次,结果仍然是零。 设置布尔标志。不管你做了多少次,旗帜都不会动摇。 从数据库中删除具有给定ID的行。如果你再试一次,排还是消失了。

对于纯函数(没有副作用的函数),幂等性意味着f(x) = f(f(x)) = f(f(f(x)))) = ......)) = f(f(f(f(x)))) = ......))对于x的所有值

对于有副作用的函数,幂等性进一步意味着在第一次应用后不会引起额外的副作用。如果愿意,可以将世界的状态视为函数的附加“隐藏”参数。

请注意,在有并发操作的情况下,您可能会发现您认为是幂等的操作不再是幂等的(例如,在上面的示例中,另一个线程可以取消布尔标志的值)。基本上,当你有并发性和可变状态时,你需要更仔细地考虑幂等性。

在构建健壮系统时,幂等性通常是一个有用的性质。例如,如果存在从第三方接收重复消息的风险,则将消息处理程序用作幂等操作,以便消息效果只发生一次,这是很有帮助的。

如果一个操作执行多次等同于执行一次,那么它就是幂等的。

例如:将音量设置为20。 不管把电视的音量设置多少次为20,最终的结果都是20。即使一个进程执行该操作50/100次或更多,在进程结束时,卷也将为20。

反例:将音量增加1。如果一个进程执行该操作50次,则最终卷将为初始卷+ 50;如果一个进程执行该操作100次,则最终卷将为初始卷+ 100。正如您可以清楚地看到的,最终结果根据执行操作的次数而变化。因此,我们可以得出结论,这个运算不是幂等的。

我用粗体突出显示了最终结果。


如果你从编程的角度考虑,假设我有一个操作,其中一个函数f以foo作为输入,f的输出被设为foo。如果在进程结束时(执行此操作50/100次或更多次),我的foo变量保存的值是该操作只执行一次时的值,则该操作是幂等的,否则为NOT。

Foo = <某个随机值,比如-2>

{foo = f(foo)}花括号概括了该操作

如果f返回输入的平方,则运算不是幂等的。因为foo在最后会被(-2)提升到(执行操作次数)的次方

如果f返回输入的绝对值,则操作是幂等的,因为无论执行多少次操作,foo都将是abs(-2)。 这里,最终结果被定义为变量foo的最终值。


在数学意义上,幂等的含义略有不同: F (F (.... F (x))) = F (x) 这里f(x)的输出再次作为输入传递给f,这在编程中并不需要总是这样。

无论调用该操作多少次,结果都是相同的。