ChatGPT解决这个技术问题 Extra ChatGPT

What is an idempotent operation?

What is an idempotent operation?


G
Greg Hewgill

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.


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.
Is "stateless" synonymous with "idempotent"?
@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.
@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.
@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.
J
Jim Ferrans

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.


"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.
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.
@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.
@JimFerrans I see. I thought there might be some functionality-related reason (built into the HTTP itself) of why PUT can be resent without worries while POST cannot. Now it appears that we are simply required to conform to the HTTP standards and the behaviour is totally based on how the server is implemented
"In arithmetic, adding zero to a number is idempotent." This is a bad example because adding zero doesn't change anything. Being idempotent means that it (probably) changes things, but in the same way whether it is applied one or multiple times. The abs() function given further above is a better example.
n
nbro

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


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?
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.
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.
@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.
so if there is a row counter in database, which is used to check data integrity of the request input vs. database state, does that row counter need to be considered in the idempotent definition? that row counter will increase for every request but not being returned as part of result.
m
mikera

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.


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.
@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).
I
IVN

A good example of understanding an idempotent operation might be locking a car with remote key.

log(Car.state) // unlocked

Remote.lock();
log(Car.state) // locked

Remote.lock();
Remote.lock();
Remote.lock();
log(Car.state) // locked

lock is an idempotent operation. Even if there are some side effect each time you run lock, like blinking, the car is still in the same locked state, no matter how many times you run lock operation.


A good idea would be to contrast this with some remotes, which instead of two buttons lock() and unlock(), have one button toggleLock(). In that case, clicking the button is not idempotent - every click ends up changing the state, alternating between unlocked and locked.
n
nbro

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.


Doesn't sound logical at all. stackoverflow.com/questions/1077412/…
I think you may be confusing idempotent and deterministic.
n
nmit026

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

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

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.

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


L
Lance

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');

This answer doesn't seem right. Idempotence is about calling an operation that you expect to change some state, given the input parameters, which if you then call again with the same parameters has no further effect on state. But in your example, before even using caching, if we call model('user') twice, this is already idempotent, there is no change of state, just creation and return of a new distinct object to the caller (not stored). You are describing a object identity factory pattern that ensures 'the same' object is returned across calls, useful but not explaining idempotence.
M
Manish Basantani

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


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.
M
Mahmoud Abou-Eita

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


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.
O
Oorang

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?


it is pithy answer even after 10 years. +1
A
Arnkrishn

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


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

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


A
Aman Godara

An operation is said to be idempotent if executing it multiple times is equivalent to executing it once.

For eg: setting volume to 20. No matter how many times the volume of TV is set to 20, end result will be that volume is 20. Even if a process executes the operation 50/100 times or more, at the end of the process the volume will be 20.

Counter example: increasing the volume by 1. If a process executes this operation 50 times, at the end volume will be initial Volume + 50 and if a process executes the operation 100 times, at the end volume will be initial Volume + 100. As you can clearly see that the end result varies based upon how many times the operation was executed. Hence, we can conclude that this operation is NOT idempotent.

I have highlighted the end result in bold.

If you think in terms of programming, let's say that I have an operation in which a function f takes foo as the input and the output of f is set to foo back. If at the end of the process (that executes this operation 50/100 times or more), my foo variable holds the value that it did when the operation was executed only ONCE, then the operation is idempotent, otherwise NOT.

foo = <some random value here, let's say -2>

{ foo = f( foo ) }   curly brackets outline the operation

if f returns the square of the input then the operation is NOT idempotent. Because foo at the end will be (-2) raised to the power (number of times operation is executed)

if f returns the absolute of the input then the operation is idempotent because no matter how many multiple times the operation is executed foo will be abs(-2).
Here, end result is defined as the final value of variable foo.

In mathematical sense, idempotence has a slightly different meaning of:
f(f(....f(x))) = f(x)
here output of f(x) is passed as input to f again which doesn't need to be the case always with programming.


L
Leonid Ganeline

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.


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!
A
Abimael Domínguez

For a workflow manager (as Apache Airflow) if an idempotency operation fails in your pipeline the system can retry the task automatically without affecting the system. Even if the logs change, that is good because you can see the incident.

The most important in this case is that your system can retry the task that failed and doesn't mess up the pipeline (e.g. appending the same data in a table each retry)


but simply explain "idempotency operation" first before the above.