什么是幂等运算?


当前回答

对于工作流管理器(如Apache workflow),如果管道中的幂等操作失败,系统可以自动重试该任务而不影响系统。即使日志发生了变化,这也很好,因为您可以看到事件。

在这种情况下,最重要的是您的系统可以重试失败的任务,并且不会弄乱管道(例如,每次重试都在表中添加相同的数据)

其他回答

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

幂等运算可以重复任意次数,结果与只做一次是一样的。在算术中,一个数加零是幂等的。

幂等性在“RESTful”web服务的上下文中被讨论了很多。REST寻求最大限度地利用HTTP来为程序提供对web内容的访问,并且通常与基于soap的web服务进行设置,后者只是在HTTP请求和响应中隐藏远程过程调用样式的服务。

REST将web应用程序组织成“资源”(如Twitter用户或Flickr图像),然后使用POST、PUT、GET和DELETE这些HTTP动词来创建、更新、读取和删除这些资源。

Idempotence plays an important role in REST. If you GET a representation of a REST resource (eg, GET a jpeg image from Flickr), and the operation fails, you can just repeat the GET again and again until the operation succeeds. To the web service, it doesn't matter how many times the image is gotten. Likewise, if you use a RESTful web service to update your Twitter account information, you can PUT the new information as many times as it takes in order to get confirmation from the web service. PUT-ing it a thousand times is the same as PUT-ing it once. Similarly DELETE-ing a REST resource a thousand times is the same as deleting it once. Idempotence thus makes it a lot easier to construct a web service that's resilient to communication errors.

进一步阅读:Richardson和Ruby的RESTful Web服务(幂等性在103-104页讨论),以及Roy Fielding关于REST的博士论文。Fielding是HTTP 1.1 RFC-2616的作者之一,RFC-2616在9.1.2节中讨论了幂等性。

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

示例(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的所有值

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

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

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

假设客户端向“IstanceA”服务发出请求,该服务处理请求,将其传递给DB,并在发送响应之前关闭。因为客户端没有看到它被处理,它将重试相同的请求。负载均衡器将请求转发到另一个服务实例“InstanceB”,该服务实例将对相同的DB项进行相同的更改。

我们应该使用幂等符号。当客户端向服务发送请求时,它应该有某种类型的请求id,可以保存在DB中,以显示我们已经执行了请求。如果客户端重试请求,“InstanceB”将检查requestId。由于特定的请求已经被执行,因此它不会对DB项进行任何更改。这种请求叫做幂等请求。因此,我们多次发送相同的请求,但不会做任何更改