How to prevent non-atomic actions in Rails transactions

A taste of what could happen if you put network requests and jobs in your db transactions: "...oh no, these methods don’t simply update ActiveRecords! They also queue up background jobs and send emails. If we fail to update Alice’s balance, then all db changes in the transaction are rolled back as expected. However, our BankWithdrawJob is already queued up and the email to Bob cannot be unsent. In this example, the job will fail because it won’t be able to find the transfer, which is a better outcome than actually withdrawing money from Bob’s account. It’s still not a pleasant experience for Bob who received a notification that didn’t match his account records." shoutout @mquan
johnsontanner3 about 1 year ago