Designing a money movement that survives double clicks and interrupted responses.
Give the request an identity
A customer presses Send and the network drops before the confirmation arrives. Let the client retry the same operation with the same idempotency key. Store that key with the sender and a fingerprint of the request so a retry cannot accidentally represent a different transfer.
Commit the movement together
Represent amounts in a suitable exact numeric type and validate the currency. Within one database transaction, guard the available balance and record the debit and matching credit. A unique constraint on the request identity is essential when two requests arrive concurrently.
Return a recoverable result
After committing, return a stable transfer reference. A repeated request can retrieve its original result. Test the case where the database commits just before the connection closes, then reconcile both accounts against the transfer record.
