Table of Contents

Software Design - Idempotence (Idempotent)

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.

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.

Example

Map reduce

The individual tasks in a Map Reduce job:

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.

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.

1) 2)