← Lessons

quiz vs the machine

Silver1050

Concurrency

Idempotency in Distributed Systems

Make an operation safe to retry by ensuring repeats produce the same effect as one call.

4 min read · intro · beat Silver to climb

What idempotency means

An operation is idempotent when applying it many times has the same effect as applying it once. In a distributed system, networks drop responses, so a client cannot always tell whether its request succeeded. The safe answer is to retry, and idempotency makes retries harmless.

Natural versus engineered

  • Some operations are naturally idempotent, like setting a field to a fixed value or deleting a known record.
  • Others are not, like adding to a balance or appending to a list. Each replay changes state again.

To make a non idempotent action safe, attach an idempotency key, a unique token the client generates per logical request. The server records the key and its result. If the same key arrives again, it returns the stored result instead of repeating the work.

Why it matters

Without idempotency, a retried payment can charge a card twice, or a retried order can ship two parcels. Idempotency turns an unreliable network into a reliable one from the caller view.

Key idea

Idempotency lets a client retry safely after an uncertain outcome, usually by recording an idempotency key and returning the saved result for repeats.

Check yourself

Answer to earn rating on the learn ladder.

1. Why is idempotency important when networks are unreliable?

2. How is a non idempotent action like incrementing a balance made safe to retry?