GistGarden
Artificial Intelligence Difficulty 35/100

Idempotency

Stamp once, stamp forever

Idempotency monster
Stamp once, stamp forever
⚡ The 5-second answer

Idempotency means you can do the same operation over and over, but the result is always the same as doing it once.

Explain like I'm five

Imagine pressing the 'up' button on an elevator. You press it once, the elevator comes. If you press it ten more times while waiting, it doesn't make the elevator come faster or bring ten elevators — it just does the same thing as pressing once. Idempotency is like that: repeating the action doesn't change the outcome beyond the first time.

Why it matters

Idempotency is crucial in APIs and distributed systems because network failures can cause retries, and you need to be sure retrying a request won't accidentally double-charge a user or create duplicate orders. It's what makes systems safe to retry without side effects.

Common misconception

People often confuse idempotency with 'no side effects' (like a pure function), but an idempotent operation can have side effects — they just must be the same after the first call. For example, setting a user's name to 'Bob' is idempotent even though it changes state, because doing it twice leaves it the same as doing it once.

Formal definition

In computing, an operation is idempotent if performing it multiple times has the same effect as performing it once. Formally, for any operation f, f(f(x)) = f(x). This property is essential for safe retry mechanisms in REST APIs (e.g., PUT and DELETE methods are idempotent) and for ensuring consistency in distributed systems.