
What Is Idempotence? - tdurden
https://lispcast.com/what-is-idempotence/
======
didibus
I've been struggling lately with distinguishing between things that are
inherently idempotent and things that are made to be using deduplication.

For example, assignment is inherently idempotent. Assign the same thing twice,
and the world is still in the same state as before. Calling assign over and
over with the same arguments thus results in the same outcome. For example:

a = 10; a = 10;

But, incrementing is not:

a++; a++;

That said, you can dedupe the incrementing operation:

increment(a, id1); increment(a, id1);

Where increment keeps track of if it has already been called for a and id1,
and cached the result. Thus subsequent calls dedupe on the cache and return
the same result.

In my opinion, these are different. While increment is made idempotent, it
requires a deduplication strategy to achieve it. It is not an inherent
property of incrementing.

Deduplication is really hard to get right. And it prevents retrying an
operation. When someone says, this is safe to retry, its actually saying, you
can't retry this, subsequent calls are noop.

Is there a better vocabulary for these two things? I don't like how
idempotency is ambiguous and refers to both. I'd like to be able to know if
something is idempotent by its design, or if it is through the use of
deduplication.

~~~
betenoire
Is assignment even truly idempotent? Maybe for mathy systems, but not
necessarily for programming languages. It would depend on the implementation
of the language, and could, for example, introduce global side effects. It
kind of depends on what you consider the "value" of the operation. The return
value? The global state?

Even a classic GET request on a REST api, is only idempotent if you ignore the
fact that the data changes over time.

I guess what I'm saying is, outside of formal systems, I think it's all
deduplication.

~~~
mikekchar
Assuming you don't count side effects like running out of memory, assignment
is definitely idempotent. It is, of course, not immutable. Now, it is
completely possible to implement a non-idempotent assignment if you want. For
example, if you wrote an assignment function that returned true if the value
changed when you assigned it, or false if it didn't, then that would not be
idempotent.

And, of course, you could be nit-picky about side effects. For example, a
language might implement assignment such that the first time you assign a
value, it allocates the memory and the second time it simply overwrites the
memory. That's obviously not idempotent. However, you could make similar
arguments for a _lot_ of compiler optimisations: they can take an idempotent
function and make it not idempotent when viewed from outside the context of
the source code.

While there is some value in considering those issues, it's a bit self-
defeating as well. You take a useful abstraction and throw it away. I think it
makes sense to have conversations like: is this idempotent from the context of
the source code? Running out of memory when you assign to a variable that's
allocated on the stack is not something anyone normally prepares for (and if
you do, then God help you ;-) ). The concept still provides value to the
conversation, which is the reason for having words in the first place. You may
prefer a different word, but if it's going to be used in every single case I
would question the utility of the redefinition.

~~~
heavenlyblue
Assignment is not idempotent if you include ordering in the system.

These two operations have different results:

1\. A=5 2\. A=6

vs

1\. A=6 2\. A=5

In such scenarios, whenever an operation is performed, the assignment operator
is no longer binary. You also need to include the ordering information (e.g.
randomly generated) in the transaction, such as:

ASSIGN(0, A, 6) ASSIGN(1, A, 5)

Then operations are performed in the order depending on the first value in the
tuple.

As you can see, idempotency of assignment is only true if it's ordering value
is the same.

------
js2
At 13:45 in this interview from 2003, Sergey Brin and Larry Page try to
explain idempotence to Terry Gross (NPR's Fresh Air host):

[http://www.npr.org/2003/10/14/167643282/google-founders-
larr...](http://www.npr.org/2003/10/14/167643282/google-founders-larry-page-
and-sergey-brin-part-2)

(It does not go very well.)

Edit: I can't find a transcript, so I just transcribed that portion of the
interview myself, starting at 13:45:

TG: Now I'll tell you, in preparing for this, I decided, let me Google Google,
so I typed in "Google" into the Google search, and I came up with a lot of
Google things in the regular search, but in the "Are you feeling lucky?"
search, I got nothing.

LP: Well you just got Google itself.

TG: Yeah, I just got Google itself. Oh, I see, Google was giving me itself.

LP: Yeah.

TG: Oh.

LP: In computer science, we call that recursion. [laugh].

TG: Oh, you even have a name for it. [laugh]. I didn't quite get that. I kept
thinking it was just repeating itself. I didn't realize it was giving me
itself. [laugh].

LP: [laugh]

TG: And what's the name for it?

LP: Uh, recursion. It's... kind of... Sergey is giving me a dirty look.

TG: Why?

LP: It's a loose definition. [laugh]

TG: Lighten up Sergey. [laugh]

LP: It's a loose interpretation of... [laugh]... recursion.

TG: Sergey, what's the more literal interpretation?

SB: The technical term is you got itself back.

TG: Right?

SB: There's not really much beyond that. [laugh]

TG: Okay.

SB: Idempotence. How about that.

TG: Say it again.

SB: Idempotence.

TG: What is it?

SB: That's when you uh... [laugh]... Maybe I should stop while I'm ahead...

TG: ...You're just making this up, aren't you...

SB: ...Before I dig a deeper hole. Idempotence is when you do something and
you get the original thing back.

TG: Oh, so that's a real word?

LP: It's a mathematical term.

SB: Yeah, yeah, but it's also just as loose an interpretation as Larry's was
of recursion.

~~~
cgrand-net
Isn’t it a fix point rather?

~~~
Tomminn
By the way, one way to describe idempotence is a function whose range is the
fixed points of the function.

Fixed point is very close. Your idea here is that
_google_search_lucky(search_string)_ , where _search_string= "google"_ returns
itself. Which would be a fixed point.

The rub is that the function _google_search_lucky(search_string)_ would in all
likelihood return the URL of a website if it was to return a string (necessary
for even the possibility of a fix point), so the proper fix point would be
"[https://www.google.com"](https://www.google.com").

If I was trying to map this to mathematical ideas, I'd say the point here is
that the function _google_search(search_string, is_lucky)_ with the argument
_( "google", is_lucky= true)_ returns the _function_
_google_search(search_string, is_lucky)_ . So it's more like the argument _(
"google", is_lucky= true)_ is an identity for the function
_google_search(search_string, is_lucky)_.

------
phamilton
Lately I've been trying hard to not just get idempotence but to get a full
CRDT. While established CRDTs like an OR-set are great, simply framing a
problem in terms of a max value has been really useful. Also useful are fuse
values, where you record the first non zero value. Getting commutativity in
addition to idempotence is great.

Examples where this is useful tend to be pretty obvious in hindsight (things
like accepting partially reported data, or recording a high water mark), and
most people have built systems using these properties but taking a more formal
approach has made it easier to identify and confirm that the order of
operations doesn't need to be strict and at least once delivery is fine.

------
mbushey
I wrote a blog with this exact title from the standpoint of a SysAdmin/DevOps
Engineer: [https://www.sendthemtomir.com/blog/what-is-
idempotence](https://www.sendthemtomir.com/blog/what-is-idempotence)

------
fibo
Actually idempotent comes from algebra, in particular from ring theory. An
ideal is idempotent if its generator, say it a, multiplied by itself n times,
equals the ring identity.

See also
[https://en.wikipedia.org/wiki/Idempotent_(ring_theory)](https://en.wikipedia.org/wiki/Idempotent_\(ring_theory\))

The definition of idempotent used in IT, assumes that n=2.

Another concept stolen from Mathematics and misused in IT is Topology.

~~~
jhanschoo
> Actually idempotent comes from algebra, in particular from ring theory. An
> ideal is idempotent if its generator, say it a, multiplied by itself n
> times, equals the ring identity.

The Wikipedia article you linked to defined it for ring elements rather than
for ideals of rings and for n=2. I agree that it is sometimes useful to define
it more generally where it is in respect to some integer n, analogous to
nilpotence.

You may have encountered this notion first in ring theory which may have led
to this impression, but it is by no means an idea originating from this
specific context.

For example, another comment points out that in linear algebra, a projection
operator is said to have the idempotence property.

------
j7ake
An analogy from linear algebra might help with the intuition: a projection is
idempotent. Projecting a projection still gives you the same output.

------
matt-attack
I would have thought that pressing an elevator call button is non-idempotent,
since it affected something on the backend. It changed state. An elevator
button is a POST. I thought POSTs like that that _perform_ an action are non-
idempotent.

Where as asking for the current floor is idempotent (i.e. a GET). Do I have
this all wrong?

~~~
GhostVII
I think you have it wrong. As far as I understand, an operation is idempotent
if you can perform it any number of times consecutively (without another
operation taking place), and have the resulting state be the same. So for the
elevator, you can press it 1 time, or 100 times, and it will have the same
result of calling the elevator to your floor, so long as no operation (ex. The
elevator arriving at your floor) takes place in between button presses.

~~~
rswail
I don't agree. Idempotency in terms of REST (or equivalent) is about the
effect on the receiver of the call. Pressing the button each time is
definitely not idempotent, because it is a change of state of the button.

The fact that the server may choose to ignore this change in terms of the
travel of the elevator is not relevant to the change of state of the button
transmitted by it being pressed.

~~~
GhostVII
So what is an example of a call that is idempotent? Are only requests that
don't modify state considered idempotent, by your definition?

~~~
Izkata
...which is weird because in the elevator example, the state _isn 't_ being
modified. The effect on the receiver, to use their terminology, is always to
set the button to "on" or "called" or whatever you want to name it. Hence, the
elevator button example is idempotent.

Quick edit: Someone else below makes a good point - in at least one part of
the world, a second button press does act as "cancel". Maybe that's the
confusion here?

------
Animats
There's a similar property of interest - order independence. That is, doing A,
then B has the same final state as doing B, then A. Does that have a cool
name?

~~~
Veedrac
The standard term seems to be ‘permutable’, for A(B(x)) = B(A(x)).

One can also say these functions commute with respect to composition. This is
slightly different from the classical notion of commutativity, since the
operator (composition) isn't generally commutative.

~~~
Animats
Ah. That's the right term. Thanks.

This is an important property for database updates.

------
latchkey
The elevator analogy doesn't work in Vietnam, pressing the button again
sometimes cancels the elevator.

------
lxe
After reading this I realized I’ve been conflating indempotence with purity.

~~~
phamilton
Referential integrity is the ~synonym for purity that is often used.

~~~
lkuty
Referential transparency ;-)

~~~
phamilton
Ha. You're right. Man there are too many terms to keep straight. "Replacing a
function call with the result of that call does not change the program" is the
one I meant.

------
xiaodai
It's impotence for people living in Deutschland

