On this page 4 sections
This one is for developers. If you build a follow-up or reminder system in PHP, sooner or later one worker can't keep up and you start a second. That's when duplicate sends appear: two workers read the same "pending" row at the same moment, and the customer gets the same follow-up twice. Here is a pattern that avoids it, using CodeIgniter 4 and MySQL 8.0 (or MariaDB 10.6 and later).
Claim jobs with SKIP LOCKED
The problem with a plain SELECT ... WHERE status = 'pending' is that nothing stops another worker reading the same rows. FOR UPDATE SKIP LOCKED locks the rows you select and makes other workers skip them instead of waiting:
$db->transStart();
$jobs = $db->query(
"SELECT id FROM follow_up_queue
WHERE state = 'queued' AND due_at <= NOW()
ORDER BY due_at
LIMIT 25
FOR UPDATE SKIP LOCKED"
)->getResultArray();
$ids = array_column($jobs, 'id');
if ($ids) {
$db->table('follow_up_queue')->whereIn('id', $ids)
->update(['state' => 'sending', 'claimed_at' => date('Y-m-d H:i:s')]);
}
$db->transComplete();
Keep the transaction short. Mark the rows as claimed and commit, then do the slow part (talking to the mail server) outside the transaction. You need an index on (state, due_at) for this to stay fast, and a separate job that resets rows stuck in sending for too long, in case a worker dies halfway.
Retry with backoff
Mail servers fail temporarily all the time. Store an attempt count on each row, and when a send fails with a temporary error, set due_at further into the future each time, for example 1, 5, 30 and 120 minutes. After a fixed number of attempts, mark the job failed and tell a human. Permanent errors (5xx responses) shouldn't be retried at all.
Run workers under systemd
Write the worker as a spark command (or use the official codeigniter4/queue package) and run it under a systemd unit with Restart=always. Long-running PHP processes slowly grow, so have the worker exit on its own after a set number of jobs or minutes and let systemd start a fresh one. MemoryMax= in the unit file gives you a hard ceiling as a safety net.
Check for a reply right before sending
For a follow-up system, the most important check happens last. A job might have been queued hours ago, and the contact may have replied since. Just before sending, search the mailbox for a message in the thread, for example with an IMAP search on the References or In-Reply-To header containing your original Message-ID. If there's a reply, cancel the job and any later steps for that contact. IMAP IDLE can tell you about new mail as it arrives, but the final check before sending is the one that prevents embarrassing follow-ups.
This is the same approach Sequenzo takes: the thread is checked in the background and again a few seconds before each send. There's a plain-English version in how stop-on-reply works.
Frequently asked questions
Q01Which databases support SKIP LOCKED?
MySQL 8.0 and later, MariaDB 10.6 and later, and PostgreSQL 9.5 and later. Older MySQL versions do not support it.
Q02Why do email workers send duplicates?
Usually because two workers read the same pending rows before either marks them as taken. Locking the rows with FOR UPDATE SKIP LOCKED inside a transaction prevents it.
Q03Should I keep the transaction open while sending?
No. Claim the rows, commit, and send afterwards. Holding a transaction open during a slow SMTP conversation blocks other workers and can cause lock timeouts.
Q04How do I stop a follow-up if the person already replied?
Check the mailbox for a reply in the same thread just before sending, and cancel the job and any later steps if one exists.