News for hackers. Stuff that matters.
Re:How to do distributed locking by amelius on Monday February 08, 2016 @05:52PM (#11061539)
Why not just use the database to handle the "locking" for you? For example, to ensure that an email with ID=123 gets sent only once, just check if "email 123 sent" is in the database, otherwise commit it to the database, wait for the transaction to be committed, and send the email.

Edit: Why is this downvoted? It is a serious question.

Re:How to do distributed locking by abraae on Monday February 08, 2016 @06:26PM (#11061779)
Perhaps because your example fails to describe what happens if there is a crash, after updating the database to say "email sent", but before the email has actually been sent.
Re:How to do distributed locking by amelius on Monday February 08, 2016 @06:31PM (#11061809)
Well, I didn't want to go into that much detail here, because that also presents a problem in the case of locks (if the locking process crashes, the lock is never released).
Re:How to do distributed locking by bryanh on Monday February 08, 2016 @06:50PM (#11061904)
Good observation! The article also covers that exact scenario - plus a solution called fencing which a proper locking system can help facilitate.
Re:How to do distributed locking by amelius on Monday February 08, 2016 @07:00PM (#11061938)
I like the fencing solution. Couldn't such a scheme be implemented over a database instead?

Why I'm thinking more in terms of a database-oriented solution is because there is still the problem of crashing. What happens if the system crashes just before the email is sent? The system somehow needs to remember to restart that task (sending the email) when it comes back up. And this is probably best done through a database anyway.

Re:How to do distributed locking by infinite8s on Monday February 08, 2016 @07:48PM (#11062184)
You need locking around the entire transaction, which includes sending the email.
Re:How to do distributed locking by tacos on Monday February 08, 2016 @06:41PM (#11061867)
You shouldn't be downvoted; people should help you get up to speed.

In the scenario you describe, the database commit works, you send the email but... you get an error back from the email send function. Now what?

This is a basic distributed computing problem. Choosing how to solve it can have a drastic effect on your code and your infrastructure. If the secondary function is "charge a credit card" obviously you can't do that twice. But if it's merely "send an email" then maybe it's okay if people get a duplicate.

Google lease/lock/deadlock/race condition and read up on why databases tend to make for bad implementations.