Invental/Review lessons/Check, then act on stale state

Check, then act on stale state

Idempotency keys need an order of checks

An idempotency contract needs an order of checks, not just a list of rules. Compare the request fingerprint before looking at the in-flight status, so a changed request never waits behind, or replays, a different one.

01 · Before and afterThe pattern

TypeScript
// order matters: identity of the request first, then its progress
const row = await keys.find(userId, key);
if (!row) return claimAndRun();
if (row.fingerprint !== hash(method, path, body)) return reply(422); // different request, same key
if (row.status === 'processing') return reply(409);                  // same request, still running
return replay(row.responseStatus, decrypt(row.responseBody));        // same request, done

02 · Run it on your appThe check

If your API stores responses for replay, are they encrypted or redacted at rest, and is the response size bounded?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.