12

So "idempotence" can be defined as:

An action, that if performed N times has the same effect as performing the action only once.

Got it, easy enough.

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:

Suppose I have a PUT method that updates some resource, we'll call it f(x)

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)

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))?

Didaxis
  • 8,486
  • 7
  • 52
  • 89
  • 1
    Should this not be moved to the Mathematics forum? (For all clarity: I did not downvote) – Roy Dictus Dec 14 '11 at 14:16
  • Depends if we're talking about a definition in formal logic, or a definition in web programming. I suspect the downvoter (not me) just thought the question was ill-posed. – Ernest Friedman-Hill Dec 14 '11 at 14:17
  • 1
    @Roy -I don't think so, since it applies directly to RESTful web services, and that is the context I wrote the example in – Didaxis Dec 14 '11 at 14:18
  • I think it's valid to discuss here, as there are definite CS applications, discussed midway down the page here: http://en.wikipedia.org/wiki/Idempotence – Jonathan M Dec 14 '11 at 14:22
  • it may be useful for someone looking to understand the concept: http://pedrorijo.com/blog/fp-concepts/ – pedrorijo91 Feb 24 '17 at 11:10

4 Answers4

6

Suppose I have a PUT method that updates some resource, we'll call it f(x)

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).

This is only obvious is the server implementation is such that PUT respects this idempotent property. In the context of HTTP, RFC 2616 says:

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.

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.

Bruno
  • 119,590
  • 31
  • 270
  • 376
  • So from this we can conclude, in this example, PUT is idempotent in the context of a single request. But even when it is interleaved with other concurrent requests it is still idempotent as the first operation's result followed the idempotence property, isn't it. – Amit Dash Aug 13 '14 at 05:57
5

Idempotence requires that the action holds for all values over its domain, i.e., f(f(x)) = f(x) for all x. Another way to think about it is that an operation is idempotent if the composition of the operation with itself is just that operation.

Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88
  • @JonathanM Sorry, I don't see your point. `abs(abs(x)) = abs(x)` for all `x`, over the reals or the complex numbers. If you think `abs` doesn't match what I stated, please give a counterexample. – Michael J. Barber Dec 14 '11 at 14:21
3

You're assuming idempotence means that the state of the server will be changed at most once by a series of invocations. Most of the time, people use this term to mean that the state on the server won't be changed at all by any number of invocations. Under these circumstances, the distinction between your two cases is immaterial.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 3
    But state that "won't be changed at all" is "nullipotent", right? This would be GET, HEAD, OPTIONS, and TRACE – Didaxis Dec 14 '11 at 15:14
3

This is not quite the definition of idempotence. A function is idempotent if for any item x, f(f(x)) == f(x).

PUT is a side effect of your f() function here, not the result of it.

fge
  • 119,121
  • 33
  • 254
  • 329
  • Your definition goes against that given here: http://en.wikipedia.org/wiki/Idempotence. The page first sites `abs()` as an idempotent function, but it would not be by your definition. – Jonathan M Dec 14 '11 at 14:20
  • 1
    This is simply wrong. You've defined idempotence to be equivalent to the identity function. You're implicitly assuming that the operation has already been done to x. – Michael J. Barber Dec 14 '11 at 14:23
  • I suspect you could clarify your answer a bit without much trouble; relating idempotence specifically to the PUT operations would be useful. – Michael J. Barber Dec 14 '11 at 14:28
  • If I understand correctly, every identity function is idempotent, but the opposite isn't true. – Amit Dash Aug 13 '14 at 05:48