Idempotence (, ) is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application. The concept of idempotence arises in a number of places in abstract algebra (in particular, in the theory of projectors and ) and functional programming (in which it is connected to the property of referential transparency).
The term was introduced by American mathematician Benjamin Peirce in 1870Original manuscript of 1870 lecture before National Academy of Sciences (Washington, DC, USA): Peirce, Benjamin (1870) "Linear associative algebra" From pages 16–17: "When an expression which is raised to the square or any higher power vanishes, it may be called nilpotent; but when raised to a square or higher power it gives itself as the result, it may be called idempotent.
The defining equation of nilpotent and idempotent expressions are respectively An = 0 and An = A; but with reference to idempotent expressions, it will always be assumed that they are of the form An = A unless it be otherwise distinctly stated."
in the context of elements of algebras that remain invariant when raised to a positive integer power, and literally means "(the quality of having) the same power", from + (same + power).
If the set has elements, we can partition it into chosen fixed points and non-fixed points under , and then is the number of different idempotent functions. Hence, taking into account all possible partitions,
Neither the property of being idempotent nor that of being not is preserved under function composition. As an example for the former, mod 3 and are both idempotent, but is not, although happens to be. As an example for the latter, the negation function on the Boolean domain is not idempotent, but is. Similarly, unary negation of real numbers is not idempotent, but is. In both cases, the composition is simply the identity function, which is idempotent.
This is a very useful property in many situations, as it means that an operation can be repeated or retried as often as necessary without causing unintended effects. With non-idempotent operations, the algorithm may have to keep track of whether the operation was already performed or not.
A sequence of idempotent subroutines where at least one subroutine is different from the others, however, is not necessarily idempotent if a later subroutine in the sequence changes a value that an earlier subroutine depends on— idempotence is not closed under sequential composition. For example, suppose the initial value of a variable is 3 and there is a subroutine sequence that reads the variable, then changes it to 5, and then reads it again. Each step in the sequence is idempotent: both steps reading the variable have no side effects and the step changing the variable to 5 will always have the same effect no matter how many times it is executed. Nonetheless, executing the entire sequence once produces the output (3, 5), but executing it a second time produces the output (5, 5), so the sequence is not idempotent.
int main() {
sequence(); // prints "3\n5\n"
sequence(); // prints "5\n5\n"
return 0;
}
In the Hypertext Transfer Protocol (HTTP), idempotence and safety are the major attributes that separate HTTP methods. Of the major HTTP methods, GET, PUT, and DELETE should be implemented in an idempotent manner according to the standard, but POST doesn't need to be.IETF, Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content . See also HyperText Transfer Protocol. GET retrieves the state of a resource; PUT updates the state of a resource; and DELETE deletes a resource. As in the example above, reading data usually has no side effects, so it is idempotent (in fact ). Updating and deleting a given data are each usually idempotent as long as the request uniquely identifies the resource and only that resource again in the future. PUT and DELETE with unique identifiers reduce to the simple case of assignment to a variable of either a value or the null-value, respectively, and are idempotent for the same reason; the end result is always the same as the result of the initial execution, even if the response differs.
Violation of the unique identification requirement in storage or deletion typically causes violation of idempotence. For example, storing or deleting a given set of content without specifying a unique identifier: POST requests, which do not need to be idempotent, often do not contain unique identifiers, so the creation of the identifier is delegated to the receiving system which then creates a corresponding new record. Similarly, PUT and DELETE requests with nonspecific criteria may result in different outcomes depending on the state of the system – for example, a request to delete the most recent record. In each case, subsequent executions will further modify the state of the system, so they are not idempotent.
In event stream processing, idempotence refers to the ability of a system to produce the same outcome, even if the same file, event or message is received more than once.
In a load–store architecture, instructions that might possibly cause a page fault are idempotent. So if a page fault occurs, the operating system can load the page from disk and then simply re-execute the faulted instruction. In a processor where such instructions are not idempotent, dealing with page faults is much more complex. John Ousterhout. "Demand Paging". Marc A. de Kruijf. "Compiler construction of idempotent regions and applications in architecture design". 2012. p. 10.
When reformatting output, prettyprint is expected to be idempotent. In other words, if the output is already "pretty", there should be nothing to do for the pretty-printer.
In service-oriented architecture (SOA), a multiple-step orchestration process composed entirely of idempotent steps can be replayed without side-effects if any part of that process fails.
Many operations that are idempotent often have ways to "resume" a process if it is interrupted ways that finish much faster than starting all over from the beginning. For example, resuming a file transfer, rsync, creating a software build, installing an application and all of its dependencies with a package manager, etc.
Similarly, the elevator "close" button may be pressed many times to the same effect as once, since the doors close on a fixed schedule – unless the "open" button is pressed. Which is not idempotent, because each press adds further delay.
|
|