0

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.

1

1 Answer 1

7

"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.

5
  • A different definition of atomic, "appears to happen at the same time", might be more clear in this case. An atomic operation can be very slow as long as it still appears atomic to external observers. Commented May 25, 2017 at 18:16
  • 6
    Atomicity has nothing to with duration or appearance. Operations that participate in an atomic transaction must succeed or fail as a unit. That is the only requirement.
    – John Wu
    Commented May 25, 2017 at 18:35
  • @JohnWu My explanation needs work, but the crucial bit of atomic transactions is that its not enough that the transaction either completely succeeds or completely fails. E.g. a bank can guarantee that £100 be added to account A if and only if £100 is removed from B, but its not atomic if at any point you can see that £100 has been removed from B and not yet added to A.
    – Justin
    Commented May 25, 2017 at 20:38
  • 2
    What you're describing is consistency (the C in ACID) not atomicity. As for banks, they use a memo post and clearing house process-- sort of a 2PC that may span several days-- so you can indeed see a withdrawal long before you can see the corresponding deposit hard post to the account. But I get what you're saying.
    – John Wu
    Commented May 25, 2017 at 20:55
  • @JohnWu Okay, I follow - I was indeed confusing Atomicity with ACID
    – Justin
    Commented May 25, 2017 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.