Live data from Hacker News

Idempotence: What is it and why should I care?

cloudingmine.com

11–20 of 77 posts

Re: Idempotence: What is it and why should I care?

#11

A function f is idempotent if: f(f(x)) = f(x) The "absolute value" function is idempotent: abs(abs(-42)) = abs(-42) The "squared" function is not: sq(sq(7)) != sq(7)

This is about idempotent operations , which are basically state changes that have the same affect if executed multiple times as if executed once: $ chmod a+x file $ chmod a+x file This is linked to mathematical idempotence in the sense that an operation is a function which takes some inputs i and the state of the world S and produces a new state S': S' = f(S, i). So then if f(f(S, i), i) = f(S, i) then f is idemponen…

Posting this to drive people crazy :) (only kidding)

How about state changes as a side effect, with a near meaningless result.

LockAccount('user')

The account is now locked.

LockAccount('user')

Error: The account was already locked!

However, the internal state of the user after the second operation remains the same. We also cannot use any of the information in the result as something meaningful as shown in these math equations because the API has a wall over an opaque data structure. Which is often not the case in Math. Similar to chmod, there might be a low-level file API that throws an internal error because the file is already executable, but the chmod wrapper hides that possible error. (It probably doesn't have an error internally, but for argument sake).

Re: Idempotence: What is it and why should I care?

#12
post #11

Earlier quoted context omitted.

This is about idempotent operations , which are basically state changes that have the same affect if executed multiple times as if executed once: $ chmod a+x file $ chmod a+x file This is linked to mathematical idempotence in the sense that an operation is a function which takes some inputs i and the state of the world S and produces a new state S': S' = f(S, i). So then if f(f(S, i), i) = f(S, i) then f is idemponen…

Posting this to drive people crazy :) (only kidding) How about state changes as a side effect, with a near meaningless result. LockAccount('user') The account is now locked. LockAccount('user') Error: The account was already locked! However, the internal state of the user after the second operation remains the same. We also cannot use any of the information in the result as something meaningful as shown in these math…

Your error changed the state S of the world.

Re: Idempotence: What is it and why should I care?

#13

Earlier quoted context omitted.

This is about idempotent operations , which are basically state changes that have the same affect if executed multiple times as if executed once: $ chmod a+x file $ chmod a+x file This is linked to mathematical idempotence in the sense that an operation is a function which takes some inputs i and the state of the world S and produces a new state S': S' = f(S, i). So then if f(f(S, i), i) = f(S, i) then f is idemponen…

You're right, and the technical distinction is warranted, but the definition you give is still not equivalent to that of being mathematically idempotent, since the domain and range of f are not isomorphic.

That's only because the domain is multi-dimensional, while the range is the domain of just the first argument.

We can fold the input into S. That is to say, the input is just an aspect of the state of the word, and then we can reduce f(S, i) to just f(S), so then we have f(f(S)) = f(S).

Re: Idempotence: What is it and why should I care?

#14

A function f is idempotent if: f(f(x)) = f(x) The "absolute value" function is idempotent: abs(abs(-42)) = abs(-42) The "squared" function is not: sq(sq(7)) != sq(7)

This differs from the definition in the article. I think the article's definition is the significantly more common one for software engineering.

Re: Idempotence: What is it and why should I care?

#15
post #11

Earlier quoted context omitted.

This is about idempotent operations , which are basically state changes that have the same affect if executed multiple times as if executed once: $ chmod a+x file $ chmod a+x file This is linked to mathematical idempotence in the sense that an operation is a function which takes some inputs i and the state of the world S and produces a new state S': S' = f(S, i). So then if f(f(S, i), i) = f(S, i) then f is idemponen…

Posting this to drive people crazy :) (only kidding) How about state changes as a side effect, with a near meaningless result. LockAccount('user') The account is now locked. LockAccount('user') Error: The account was already locked! However, the internal state of the user after the second operation remains the same. We also cannot use any of the information in the result as something meaningful as shown in these math…

A more idempotency-friendly API would be:

    setAccountLockStatus('user', LockStatus.LOCKED)

Re: Idempotence: What is it and why should I care?

#16
post #5

One of the practical applications of this concept I've found is that I try to write idempotent database migrations (so rerunning the migration, which is a common necessity while you're developing it, but is also useful if problems occur, won't error). So in essence both the "up" and the "down" migrations are idempotent and warn if they are not (and why).

How do you do it without throwing an error?

My philosophy has been to ignore the error if it's a re-application of the migration because the migration _is_ idempotent. So ALTER TABLE foo ADD COLUMN bar INT either succeeds (because there is no bar column) or it fails (because there is a bar column) with no harm done.

Re: Idempotence: What is it and why should I care?

#17

A function f is idempotent if: f(f(x)) = f(x) The "absolute value" function is idempotent: abs(abs(-42)) = abs(-42) The "squared" function is not: sq(sq(7)) != sq(7)

In category theory we also say h is idempotent iff h^2 = h whereas h is an endomorphism.

Re: Idempotence: What is it and why should I care?

#18
This is vital if you are designing apis or clients that deal with charging a user money. It should be literally impossible for a user to accidentally get charged twice due to a flakey connection if you design correctly.

The trick is to have the client generate a random 'idempotency key' (a uuid) to start each logical transaction and have the server use that id to prevent double charges of the same transaction. By always passing that key, client can request that the payment be processed a 100 times with no fear of it being processed more than once.

This stripe blog post has as good a description as any: https://stripe.com/blog/idempotency

Post reply on HN