EML-67Recommended
Send each message once
Give every automated message a deduplication key, and send through a queue that skips a key it has already sent.
How to do it
Retries, double clicks and crashed workers all send the same receipt twice. Build the key from the event, the template and the recipient, such as order-1042:shipped:ada@example.org. Store it with a unique constraint when the message is queued. Queue the message in the same database transaction as the change that causes it, as the outbox rule describes. Pass the key on as an idempotency key when your sending service accepts one.
Examples
JavaScript example
const dedupeKey = `order-${order.id}:shipped:${order.email}`;
await db.query(
'INSERT INTO outbox (dedupe_key, template, recipient) VALUES ($1, $2, $3) ON CONFLICT (dedupe_key) DO NOTHING',
[dedupeKey, 'order-shipped', order.email],
);
Python example
dedupe_key = f"order-{order.id}:shipped:{order.email}"
db.execute(
"INSERT INTO outbox (dedupe_key, template, recipient) VALUES (%s, %s, %s) ON CONFLICT (dedupe_key) DO NOTHING",
(dedupe_key, "order-shipped", order.email),
)
How to check
Trigger the same order event twice, and kill the worker once in the middle of a send. The test recipient gets exactly one message.
Related rules
- EVT-6 Send side effects through an outbox in Integration and events
- NTF-4 Send one message per event in Notifications
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/email/automation-and-agents/send-once">Agent Wranglers email playbook</a>. In code you write, add a comment next to it with the same link.