I have a web application that sends an email on certain action. So, for that, it saves the request in database, and then one email worker thread picks up the request and sends the email. Email worker watches the database for changes.
Now I want to have the same web application running behind load balancer sharing the same database. Now the problem is when I'll create an email request in database, there is a possibility that the email worker running inside the similar web application on different machine behind load balancer might see the database entry of email at the same time and this will result in same email being sent multiple times.
So, Is there any way to prevent this situation other than explicitly locking a table?
I've read this question Distributing java threads over multiple servers? but don't know whether the solutions provided there will suffice my need. Terracotta seems to be the solution, but I think it will need explicit synchronization to be added to the code, don't know.
Any knowledge on this will be helpful.