Question

I have a lot of cases where I need to persist some information into a database and complete a secondary action using third party system.

For example I have a trivial case, to save user information and to send an email to the user about this action. How can this be an atomic process ie. how can I be sure that user information is saved and that an email has been send successfully?

If I persist information I still have to send email and will not be sure if an error will occur on email server.

This particular working environment is IIS, asp.net mvc, sql server and smtp server.

Was it helpful?

Solution

"Atomic" means that the process appears to have happened instantaneously, or at least it cannot be interrupted. Its possible to have atomic distributed processes, but its not easy, e.g. you could do the following:

  • Start a SQL transaction
  • Add the user to the database
  • Send the email
    • Rollback the transaction if the email failed to send
    • Commit the transaction if the email was sent successfully

However even this is not foolproof, e.g. suppose your commit fails, or suppose you get an "email bounced" response back 30 seconds after sending your email?

Instead I would try to achieve Eventual Consistency, i.e. you want to guarantee that an email is sent to a user after the user is saved but it doesn't necessarily need to happen immediately - its okay if the email gets sent a few seconds (or even a few minutes) after the user record has saved, after all it will take a few seconds for the email to be delivered to the users inbox.

Licensed under: CC-BY-SA with attribution
scroll top