Asked about Writing data safely

A client retries a request after a timeout. How do you make sure it is not processed twice?

An answer that works

The client sends a unique request id. The server stores it with a unique constraint as part of the same transaction as the work, and on conflict returns the original result instead of repeating the work.

Then they push further

What if the work includes calling an external API that is not idempotent?

Record the intent with the request id before calling, and on retry check that record and ask the external system what happened rather than calling again.

What gets marked up

Marked up for enforcing it in the database with a constraint rather than only checking in application code, which races.

Reading an answer is not being able to give one. The section that teaches this is Writing data safely, 3 hours of SQL & data access.

Learn this properly