I have a DB whose columns are:
| from_email | to_email | subject | body | processed(Y/N) |
Now I want to implement a system in Java that will access this DB for not processed, send the mail (I'm using fakeSMTP for testing) and set it as processed.
It works fine enough, although I have yet to add any threading.
But the problem is to speed up the procedure it might be deployed over more than one server, in that case how can I ensure no mail gets sent twice?
How can it be made faster?
The following should be the solution to your problem:
Each server should have a unique id
Add the server_id with default value of null to your mails table
When a server intends to send a mail, run a query, like:
update mails
set server_id = <your server_id>
where (server_id is null) and (<your other criteria>)
After you processed the update, select the row and if server_id has the expected value (no concurrency issue occurred), then process it
If a record's server_id is not null, then its processing has been already started. If its server_id is not null and its processed is Y, then it has been processed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With