Make an operation safe to repeat
Networks retry. Users double-click. Both must be harmless.
This operation must not happen twice: <describe — a charge, an email, a record creation> Here's the current code: <paste> Make it idempotent: - The caller supplies a key, or I derive one from the request. Say which fits and why. - Store the key with the result. A repeat returns the original result rather than doing the work again. - Handle two identical requests arriving at the same moment, not just one after the other. - Say how long keys are kept and what happens after that. Then tell me what happens today if the user double-clicks, and if the network retries.
Two simultaneous requests is the case that catches people. Checking then inserting is not enough — you need the database to enforce it.