wiki/content/20201126095525-idempotent.md

799 B

id title
48609306-f2e9-4d01-b774-7d3ec34703aa idempotent

Origin

Idem is latin for 'same'.

Definition

An operation or function, that when called multiple times will always produce the same result.

Examples

Idempotent

console.log(1 * 1 * 1 * 1)
const set = new Set()

set.add('blaat')
set.add('blaat')
set.add('blaat')

console.log(set) // { 'blaat' }

Practial examples

  • Get, Put & Delete endpoints on a REST api
  • Payment operations. Reposting the same form should not result in multiple charges to the customer.

Non idempotent

console.log(2 * 2 * 2 * 2)
const arr = []

arr.push('blaat')
arr.push('blaat')
arr.push('blaat')

console.log(arr) // [ 'blaat', 'blaat', 'blaat' ]