Software Design - Idempotence (Idempotent)

> Code - (Programming|Computer) Language > ( Information System | Software ) Design - Art of Programming

1 - About

Idempotence is the ability to apply multiple times an operation without changing the result beyond the initial application.

Given the same input, re-executing a task will always produce the same result and will not change other state.

Running an idempotent code (application, function,..) multiple times in a sequence should have the same effect as running it just once.

Advertising

3 - Implementation

One way to achieve idempotency is to have a check whether the desired final state has already been achieved, and if that's the case, to exit without performing any actions.

4 - Example

  • If you send an insert, the application will search to see if it has been already applied. If this is the case, the operation is not applied.
  • When you can overwrite a script with its output. The output of any script is executable, and the output of a correct script is itself.

4.1 - Map reduce

The individual tasks in a Map Reduce job:

  • are idempotent.
  • and have no side effects.

They can be restarted without changing the final result.

These two properties mean that given the same input, re-executing a task will always produce the same result and will not change other state. So, the results and end condition of the system are the same, whether a task is executed once or a thousand times.

4.2 - Database Migration Script

A migration script will not try to add a column if it’s already there. Therefore, a specific script order isn't necessary needed in the database migration script.

5 - Documentation / Reference

Advertising