Explained step by step
An operation is idempotent if doing it several times has the same effect as doing it 1 time. Setting a user's email to a@example.com is idempotent: doing it 3 times leaves the same email. On the other hand, «add $10 to the balance» is not, because every repeat changes the result: 3 times add $30. It's like the elevator button: pressing it 5 times doesn't bring 5 elevators.
In distributed systems, the network drops responses, clients hit timeouts and load balancers retry, so the same request often arrives more than 1 time. If a payment times out, the client doesn't know whether the server never got it, processed it and the response was lost, or is still processing it. The only safe thing is to retry, but without idempotency the retry can charge 2 times. It's like sending a registered letter: if the receipt doesn't arrive, you send another, and the recipient has to know it's the same one.
Some HTTP verbs are idempotent by definition: GET reads without changing anything, PUT replaces a resource with the same thing, DELETE of something already deleted does nothing. POST is not, because it usually creates a new resource on every call, and PATCH depends on what it does, for example if it increments. Careful: idempotent means the server state stays the same, not that the response is identical; a repeated DELETE may return 404 instead of 200. It's like switching off a light that's already off: the light stays the same, even if the switch doesn't click.
For operations that aren't idempotent, like POST /payments, you use an idempotency key. The client generates a unique value, usually a UUID, and sends it with the request, for example in the Idempotency-Key header. The server looks up the key: if it's not there, it stores it, processes the request and saves the response with the key; if it's already there, it doesn't process and returns the saved response. It's like a coat check ticket number: if you show it 2 times, you get the same coat, not 2.
There are 4 important details. Atomicity: looking up the key and storing it must be 1 single atomic operation, otherwise 2 simultaneous retries both pass; a unique constraint in the database or a SET NX in Redis works. Request fingerprint: store a hash of the content, and reject the same key with different content. In-flight requests: if a retry arrives while the original is still running, answer 409 Conflict or make it wait. Expiry: keys last from a few hours to a few days. It's like the «paid» stamp on an invoice: it has to go on in 1 single move, and it counts for that invoice, not another.
The same applies to asynchronous systems: since brokers usually guarantee at-least-once delivery, a consumer must withstand duplicates. There are 3 common ways. Deduplication table: store the ID of each processed message and skip repeats, writing the ID in the same transaction as the effect. Natural idempotency: «set status to shipped» instead of «advance status». Versioning: ignore any update that isn't newer than the current one. It's like crossing off guests who already came in: if someone comes back to the door, they're already crossed off.
Idempotency isn't free: it needs space and lookups for the keys, and extra logic for in-flight requests. Also, exactly-once processing generally can't be achieved across a network. But at-least-once delivery plus idempotent processing gives the same visible result: that's called effectively-once. It's like a mail carrier who sometimes delivers the same letter 2 times, but you throw away the repeat: in the end you read just 1.