Review request

Donor thank-you mailer built in Bolt sent half the list twice

Solved · 12 views · asked by amara_v · edited

Repo or live app

github.com/amara-okoye/donor-thanks

Unsure about: DeploymentOther

After each campaign we send a thank-you to everyone who gave. 1,400 donors this time, and about 600 of them got the mail twice within a minute. The send is one Vercel route that loops over the donor list and awaits resend.emails.send per donor. It finished in the logs, or at least it said it did.

Comment

2 answers

Marked as helpful by the asker
jonas_k · edited

A loop of 1,400 awaited sends does not fit in a serverless function's execution limit. The platform kills it partway, sees a failed invocation, and retries from the top. Everyone the first run reached gets a second mail, which matches your 600.

Two changes. First, make the send resumable: write a sent_at on the donor row immediately after each successful send and select only where sent_at is null, so a retry picks up where it stopped instead of restarting. Second, pass an idempotency key per recipient so a duplicate call is a no-op at Resend's side:

await resend.emails.send(payload, { idempotencyKey: `thanks-${campaignId}-${donorId}` })

Then move the loop to a job that processes a batch per invocation. 1,400 mails is not a request, it is a queue.

Comment
dev_ana · edited

Worth adding a guard for the human side of this too. Put a campaign_sends row with a unique constraint on (campaign_id, donor_id) and insert before you send. It costs nothing and it also covers the case where someone clicks the send button twice because the first click showed no feedback, which from your screenshot it does not.

Comment