定义幂等 [英] Defining Idempotence
问题描述
所以幂等可以定义为:
一个动作,如果执行N次,则只执行一次动作。
An action, that if performed N times has the same effect as performing the action only once.
知道了,很容易。
我的问题是关于这个定义的微妙之处 - 这个行为本身就是幂等的,或者你还必须考虑传递给行动的数据吗?
My question is about the subtlety of this definition -is an action considered idempotent by itself, or must you also consider the data being passed into the action?
让我用一个例子来澄清:
Let me clarify with an example:
假设我有一个更新某些资源的PUT方法,我们称之为 f(x)
Suppose I have a PUT method that updates some resource, we'll call it f(x)
显然, f(3)
是幂等的,只要我提供3作为输入。同样显而易见的是, f(5)
将改变资源的价值(即,它将不再是3或之前的任何值)
Obviously, f(3)
is idempotent, as long as I supply 3 as the input. And equally obvious, f(5)
will change the value of the resource (i.e., it will no longer be 3 or whatever value was there previously)
所以当我们谈论幂等性时,我们是指动作/函数的泛化(即 f(x)
),或者我们是指动作/功能+传递给它的数据(即 f(3)
)?
So when we talk about idempotence, are we referring to the generalization of the action/function like (i.e., f(x)
), or are we referring to action/function + the data being passed into it (i.e., f(3)
)?
推荐答案
假设我有一个更新某些资源的PUT方法,我们称之为
f(x)
Suppose I have a PUT method that updates some resource, we'll call it f(x)
显然,f(3)是幂等的,只要我提供3作为输入。并且
同样明显,f(5)将改变资源的价值(即,
将不再是3或之前的任何值)。
Obviously, f(3) is idempotent, as long as I supply 3 as the input. And equally obvious, f(5) will change the value of the resource (i.e., it will no longer be 3 or whatever value was there previously).
这是显而易见的,服务器实现是这样的 PUT
尊重这个幂等属性。在HTTP环境中, RFC 2616说:
This is only obvious is the server implementation is such that PUT
respects this idempotent property. In the context of HTTP, RFC 2616 says:
方法也可以具有idempotence属性(除了错误或到期问题的
)之外的副作用N> 0 相同
请求与单个请求相同。
Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request.
去咬掉主题...
在像网络这样的分布式系统中,您可能还需要考虑交换性和并发请求。例如,相同PUT(x1)请求的N + 1应具有相同的效果,但您不知道另一个客户端是否在您的之间发出了不同的PUT(x2)请求,因此当n PUT(x1)时= PUT(x1)和m PUT(x2)= PUT(x2),两组请求可以交错。
Going a bit off topic... In a distributed system like the web, you may also want to consider commutativity and concurrent requests. For example N+1 of the same PUT(x1) request should have the same effect, but you don't know if another client made a different PUT(x2) request in between yours, so while nPUT(x1)=PUT(x1) and mPUT(x2)=PUT(x2), the two sets of requests could be interleaved.
这篇关于定义幂等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!