By Will


2009-07-03 01:06:59 8 Comments

What is an idempotent operation?

14 comments

@Caleb Huitt - cjhuitt 2009-07-03 01:13:54

An idempotent operation produces the result in the same state even if you call it more than once, provided you pass in the same parameters.

@Green 2015-10-10 08:36:33

Doesn't sound logical at all. stackoverflow.com/questions/1077412/…

@Suncat2000 2018-04-12 17:33:22

I think you may be confusing idempotent and deterministic.

@Robert 2009-07-03 01:10:23

No matter how many times you call the operation, the result will be the same.

@Keith Bennett 2012-06-28 22:32:26

I've heard idempotent defined as either or both of the below: 1) For a given set of inputs it will always return the same output. 2) Does not produce any side effects. My question is, if a function conforms to #1, but not #2, because it results in a side effect unrelated to the computation (logs the request to a data store, for example), is it still considered idempotent?

@Robert 2012-07-17 21:45:36

The result of calling an operation must include the state of the system, so if the operation has some cumulative side effect it is not idempotent; however, if the side effect leaves the system in the same state no matter how many times the operation is called, then it may be idempotent.

@Prancer 2015-02-02 12:59:03

Short and sweet, I love that kind of answer. Not sure why I have to look this term up constantly, it's one that just doesn't stay with me.

@Pacerier 2015-03-10 23:11:28

@KeithBennett, The second definition is wrong. "No side effect" does not mean idempotent. Idempotent functions can have side effects. E.g. MySQL's truncate and delete.

@G. Steigert 2018-02-07 11:13:27

The result will be the same (that is, the system state), but the response may vary (ie, HTTP status codes on a REST service).

@teknopaul 2017-04-06 12:28:15

retry-safe.

Is usually the easiest way to understand its meaning in computer science.

@Lasse Vågsæther Karlsen 2017-04-06 12:37:23

Retry implies something that failed the first or previous time. Not quite the same.

@teknopaul 2017-04-09 10:17:01

Who edited my question and got me a down vote? That is not the text I posted??

@Lasse Vågsæther Karlsen 2017-04-09 19:17:17

You can check the edit log by clicking on the link below your answer that says "edited X hours ago" or similar.

@Marcus Thornton 2016-03-25 08:50:07

In short, Idempotent operations means that the operation will not result in different results no matter how many times you operate the idempotent operations.

For example, according to the definition of the spec of HTTP, GET, HEAD, PUT, and DELETE are idempotent operations; however POST and PATCH are not. That's why sometimes POST is replaced by PATCH.

@Manish Basantani 2015-08-13 10:59:41

Quite a detailed and technical answers. Just adding a simple definition.

Idempotent = Re-runnable

For example, Create operation in itself is not guaranteed to run without error if executed more than once. But if there is an operation CreateOrUpdate then it states re-runnability (Idempotency).

@Saeed Mohtasham 2018-04-16 14:38:40

This is a deceptive definition. re-runnability does not guarantee to be idempotent. An operation can be re-runnable and in each run it can add additional effects to the result so it would not be idempotent.

@nmit026 2015-08-03 22:46:59

An idempotent operation is an operation, action, or request that can be applied multiple times without changing the result, i.e. the state of the system, beyond the initial application.

EXAMPLES (WEB APP CONTEXT):

NULLIPOTENT: If an operation has no side effects, like purely displaying information on a web page without any change in a database (in other words you are only reading the database), we say the operation is NULLIPOTENT. All GETs should be nullipotent. Otherwise, use POST.

IDEMPOTENT: 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.

NON-IDEMPOTENT: If an operation always causes a change in state, like POSTing the same message to a user over and over, resulting in a new message sent and stored in the database every time, we say that the operation is NON-IDEMPOTENT.

When talking about the state of the system we are obviously ignoring hopefully harmless and inevitable effects like logging and diagnostics.

@Greg Hewgill 2009-07-03 01:10:53

In computing, an idempotent operation is one that has no additional effect if it is called more than once with the same input parameters. For example, removing an item from a set can be considered an idempotent operation on the set.

In mathematics, an idempotent operation is one where f(f(x)) = f(x). For example, the abs() function is idempotent because abs(abs(x)) = abs(x) for all x.

These slightly different definitions can be reconciled by considering that x in the mathematical definition represents the state of an object, and f is an operation that may mutate that object. For example, consider the Python set and its discard method. The discard method removes an element from a set, and does nothing if the element does not exist. So:

my_set.discard(x)

has exactly the same effect as doing the same operation twice:

my_set.discard(x)
my_set.discard(x)

Idempotent operations are often used in the design of network protocols, where a request to perform an operation is guaranteed to happen at least once, but might also happen more than once. If the operation is idempotent, then there is no harm in performing the operation two or more times.

See the Wikipedia article on idempotence for more information.


The above answer previously had some incorrect and misleading examples. Comments below written before April 2014 refer to an older revision.

@KNU 2014-04-01 10:37:56

Example :since answer above states that Idempotent operations are often used in the design of network protocols here's a related example **GET is not suppose to change anything on the server, so GET is, idempotent. In HTTP/servlet context, it means the same request can be made twice with no negative consequences. **POST is NOT idempotent.

@Michael Osofsky 2014-12-05 17:09:54

Is "stateless" synonymous with "idempotent"?

@Greg Hewgill 2014-12-07 19:44:43

@MichaelOsofsky: No, in the Python set example in the answer, the set object clearly has state and also offers some idempotent operations such as discard.

@Michael Osofsky 2014-12-08 17:29:57

Wonderful, @GregHewgill, I understand now that "idempotent" and "stateless" are different because the discard operation operates on state yet returns the same result if called multiple times with the same input parameters; discard just does different work to get to that result. Thanks for your help.

@Pacerier 2015-03-10 22:59:13

@MichaelOsofsky, discard can also be implemented in a stateless way by encompassing the state in the return value: discard([my_set, x]) = [my_new_set, x]. So you can do discard(discard([my_set, x])). Note that [my_new_set, x] is just one argument and its type is 2-tuple.

@Pacerier 2015-03-10 22:59:52

@mikera, The state of the world is not an additional implicit argument, but the one and only argument. If you had additional arguments, the operation would be a binary operation and can only be considered idempotent if both arguments are identical. To remain as a unary idempotent operation, the state of the world must be the argument and the return value.

@Green 2015-10-10 08:10:51

Not removing an item from a set but removing the given number from a set. Because it you do, for example, array.pop() the operation definitely harms the set every time it is called.

@Green 2015-10-10 08:31:26

How can you say has exactly the same effect as doing the same operation twice? It doesn't have the same effect for sure. If I delete an item from a set on the first call and it is successfully deleted, first, I get the deleted item (in JS, not python), second, the state of the set gets modified. If I repeat the operation, I get undefined and the set is not modified. So, where is the same effect you're talking about? Absolutely different. But any subsequent calls really have the same effect: you get undefined and a set is not modified. I really don't understand computing idempotence.

@Andreas 2016-04-27 15:27:42

@Green When using the term same effect in the context of impotency, it means that the result is the same, not the action. Calling discard(x) a second time will have the same effect as calling it the first time: The set will no longer contain x. Computing idempotence is about the robustness of a system. Since things can fail (e.g. network outage), when a failure is detected, how do you recover? The easiest recovery is to just do it again, but that only works if doing it again is idempotent. E.g. discard(x) is idempotent, but pop() is not. It's all about error recovery.

@Andrew Shatnyy 2016-08-04 17:01:50

For those who speak Russian head to ru.wikipedia.org/wiki/…. Clearest definition, by far!

@ahnbizcad 2016-08-10 18:11:07

the whole point of asking elsewhere is that wikipedia explanations about mathematical stuff are usually harder to digest / comprehend, given the way it's explained in wikipedia.

@Franklin Yu 2017-12-19 22:15:06

@Andreas How about the second example on Wikipedia: suppose the initial value of a variable is 3 and there is a sequence that reads the variable, then changes it to 5, and then reads it again? Do you think this is idempotent?

@Andreas 2017-12-19 22:37:37

@FranklinYu Do I still think what is idempotent? That example is about composition of idempotent methods, and there is no composition in this question or in this answer. The Wikipedia example seems pretty clear to me, and specifically says that the example composite is not idempotent, even though each of the 3 methods/operations are. I don't understand your question.

@Franklin Yu 2017-12-20 01:14:45

@Andreas But in that case, a second call doesn't change states, and returns different value, exactly the same case in HTTP DELETE. If that is not idempotent, how is DELETE idempotent?

@Andreas 2017-12-20 15:57:50

@FranklinYu If you are in a scenario where idempotency is important, then you also know that a second DELETE may return 404 (not found), which means that whether you get 404 or 200 (or 202, or 204), you know that you've got what you wanted. So, as far as you are concerned, 404 is a valid response saying "what you asked for has been done". As such, the method is idempotent. In this case, 404 and 200 both means OK.

@Leonid Ganeline 2013-11-07 16:11:59

my 5c: In integration and networking the idempotency is very important. Several examples from real-life: Imagine, we deliver data to the target system. Data delivered by a sequence of messages. 1. What would happen if the sequence is mixed in channel? (As network packages always do :) ). If the target system is idempotent, the result will not be different. If the target system depends of the right order in the sequence, we have to implement resequencer on the target site, which would restore the right order. 2. What would happen if there are the message duplicates? If the channel of target system does not acknowledge timely, the source system (or channel itself) usually sends another copy of the message. As a result we can have duplicate message on the target system side. If the target system is idempotent, it takes care of it and result will not be different. If the target system is not idempotent, we have to implement deduplicator on the target system side of the channel.

@Robin Green 2016-03-08 09:25:14

Idempotency of single requests sent in isolation from any other requests (or anything else happening that changes the state of the system), is not the same as reordering requests. A HTTP PUT request and a HTTP DELETE request should both be individually idempotent - but that does not mean that the order of calling PUT and DELETE on the same URL does not matter, because the PUT request might have side effects!

@Lance Pollard 2013-10-09 01:30:44

Just wanted to throw out a real use case that demonstrates idempotence. In JavaScript, say you are defining a bunch of model classes (as in MVC model). The way this is often implemented is functionally equivalent to something like this (basic example):

function model(name) {
  function Model() {
    this.name = name;
  }

  return Model;
}

You could then define new classes like this:

var User = model('user');
var Article = model('article');

But if you were to try to get the User class via model('user'), from somewhere else in the code, it would fail:

var User = model('user');
// ... then somewhere else in the code (in a different scope)
var User = model('user');

Those two User constructors would be different. That is,

model('user') !== model('user');

To make it idempotent, you would just add some sort of caching mechanism, like this:

var collection = {};

function model(name) {
  if (collection[name])
    return collection[name];

  function Model() {
    this.name = name;
  }

  collection[name] = Model;
  return Model;
}

By adding caching, every time you did model('user') it will be the same object, and so it's idempotent. So:

model('user') === model('user');

@Mahmoud Abou-Eita 2012-12-06 14:42:39

Idempotent Operations: Operations that have no side-effects if executed multiple times.
Example: An operation that retrieves values from a data resource and say, prints it

Non-Idempotent Operations: Operations that would cause some harm if executed multiple times. (As they change some values or states)
Example: An operation that withdraws from a bank account

@Saeed Mohtasham 2018-04-16 15:37:52

Actually a wrong answer! for the Idempotent operation saying "have no side-effects" is not right. for the non-idempotent operations saying " cause some harm" is a confusing answer.

@mikera 2012-03-05 04:49:12

Idempotence means that applying an operation once or applying it multiple times has the same effect.

Examples:

  • Multiplication by zero. No matter how many times you do it, the result is still zero.
  • Setting a boolean flag. No matter how many times you do it, the flag stays set.
  • Deleting a row from a database with a given ID. If you try it again, the row is still gone.

For pure functions (functions with no side effects) then idempotency implies that f(x) = f(f(x)) = f(f(f(x))) = f(f(f(f(x)))) = ...... for all values of x

For functions with side effects, idempotency furthermore implies that no additional side effects will be caused after the first application. You can consider the state of the world to be an additional "hidden" parameter to the function if you like.

Note that in a world where you have concurrent actions going on, you may find that operations you thought were idempotent cease to be so (for example, another thread could unset the value of the boolean flag in the example above). Basically whenever you have concurrency and mutable state, you need to think much more carefully about idempotency.

Idempotency is often a useful property in building robust systems. For example, if there is a risk that you may receive a duplicate message from a third party, it is helpful to have the message handler act as an idempotent operation so that the message effect only happens once.

@Pacerier 2015-03-10 22:08:51

If for pure functions f(x) = f(f(x)), Do you mean that f(x){return x+1;} is not a pure function? because f(x) != f(f(x)): f(1) gives 2 while f(2) gives 3.

@Justin J Stark 2016-10-17 16:14:09

@Pacerier No, @mikera is saying pure and idempotent implies f(x) = f(f(x)). But as @GregHewgill mentioned, in order for this definition to make sense, you have to consider x as an object and f as an operation that mutates the state of the object (ie: the output of f is a mutated x).

@Jim Ferrans 2009-07-03 01:47:45

An idempotent operation can be repeated an arbitrary number of times and the result will be the same as if it had been done only once. In arithmetic, adding zero to a number is idempotent.

Idempotence is talked about a lot in the context of "RESTful" web services. REST seeks to maximally leverage HTTP to give programs access to web content, and is usually set in contrast to SOAP-based web services, which just tunnel remote procedure call style services inside HTTP requests and responses.

REST organizes a web application into "resources" (like a Twitter user, or a Flickr image) and then uses the HTTP verbs of POST, PUT, GET, and DELETE to create, update, read, and delete those resources.

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.

Further reading: RESTful Web Services, by Richardson and Ruby (idempotence is discussed on page 103-104), and Roy Fielding's PhD dissertation on REST. Fielding was one of the authors of HTTP 1.1, RFC-2616, which talks about idempotence in section 9.1.2.

@Pacerier 2015-03-10 21:45:23

Clear and straightforward. Yet this is but only one interpretation of idempotent.

@Jim Ferrans 2015-03-11 03:37:09

@Pacerier: Very true, idempotence has applications in many other areas, like functional programming and message queue processing.

@Pacerier 2015-03-11 11:15:42

"idempotence" is a heavily overloaded word because it sounds grandiloquent and has enough characters to pass the sesquipedalian check. If Benjamin Peirce had chosen a simpler sounding word, we wouldn't even have this question today.

@Green 2015-10-10 08:14:49

How to understand it: Similarly DELETE-ing a REST resource a thousand times is the same as deleting it once? You cannot delete the resource again if it is already deleted.

@Caleth 2017-04-06 12:54:33

@Green but you don't delete it the first time. You send a delete request. The important point is that you can send as many requests as you like.

@Oorang 2009-07-03 01:15:25

It is any operation that every nth result will result in an output matching the value of the 1st result. For instance the absolute value of -1 is 1. The absolute value of the absolute value of -1 is 1. The absolute value of the absolute value of absolute value of -1 is 1. And so on.

See also: When would be a really silly time to use recursion?

@Arnkrishn 2009-07-03 01:26:46

An idempotent operation over a set leaves its members unchanged when applied one or more times.

It can be a unary operation like absolute(x) where x belongs to a set of positive integers. Here absolute(absolute(x)) = x.

It can be a binary operation like union of a set with itself would always return the same set.

cheers

@Saeed Mohtasham 2018-04-16 15:44:59

An idempotent operation is one where f(f(x)) = f(x). "leaves its members unchanged" is not a right answer.

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] What is tail recursion?

31 Answered Questions

[SOLVED] What is the difference between a deep copy and a shallow copy?

28 Answered Questions

32 Answered Questions

[SOLVED] What exactly is RESTful programming?

  • 2009-03-22 14:45:39
  • hasen
  • 1599656 View
  • 3839 Score
  • 32 Answer
  • Tags:   http rest definition

25 Answered Questions

[SOLVED] What and where are the stack and heap?

32 Answered Questions

[SOLVED] What does it mean to "program to an interface"?

34 Answered Questions

[SOLVED] What is dependency injection?

33 Answered Questions

[SOLVED] Prefer composition over inheritance?

32 Answered Questions

[SOLVED] What is the difference between concurrency and parallelism?

36 Answered Questions

[SOLVED] How to pair socks from a pile efficiently?

Sponsored Content