Chapter 14: Data and failure

If silence means you don't know, the only safe response is to try again, and make trying again harmless. This is the third part of the sentence, and the one that holds the other two together.

Idempotency

Also called Idempotency key

An operation is idempotent if doing it twice has the same effect as doing it once. A client makes that true of a payment by sending a unique key with the request. The server saves the first result against that key, and a repeat gets that result back and charges nothing.

Agents add retries readily, since retries look like robustness, and a retry on a charge is how a customer pays twice. It doesn't appear in tests, because the test network never loses a reply. Done properly, this is the whole sentence: in one transaction, insert the key under a unique constraint, and only the winner does the work.

Say it to your agent

“Use a transaction with a unique constraint and an idempotency key. Require an Idempotency-Key header on POST /charges. In one transaction, INSERT it into idempotency_keys with ON CONFLICT DO NOTHING, and if no row went in, return the saved response. Test it with 20 concurrent requests using one key: one charge, 20 identical replies.”

The workshop

Name a word, or describe what you mean in everyday language.