Capture first, notify later: a contact form that never loses a lead
A contact form looks like one job: take the message, tell someone. In practice it is a chain of other people's services (your database, Slack, an email provider, a CRM) and any link can fail on any day. If the form only works when every link works, you will lose real leads, and you will not know which ones.
The fix is an ordering rule: durable first, everything else after. Six steps.
1. Store the lead before anything else
Validate the input, then write it to your database before you call a single outside service. From that moment the lead exists, whatever happens next.
const lead = await insertLead(clean); // durable: nothing below can lose it
res.status(200).json({ ok: true }); // the visitor is done
await runInBackground(dispatchLead(lead));
The only case where the visitor sees an error is when the database itself is down, because then there is genuinely nowhere safe to put the message. That is the one failure worth surfacing.
2. Give every channel its own status
Slack, email and the CRM fail independently. Track each one on the row, so a retry only repeats what actually failed:
slack_status text not null default 'pending'
check (slack_status in ('pending','sent','failed','skipped')),
email_status text not null default 'pending' ...,
notion_status text not null default 'pending' ...,
attempts integer not null default 0,
next_attempt_at timestamptz not null default now()
A channel with no credentials configured starts as skipped, so you can launch with only the database and add Slack and email later without a code change.
3. Answer first, dispatch after
Send the success response, then do the slow outbound calls. The visitor should not wait on three third-party APIs, and a slow Slack should never turn into a failed form. On serverless platforms, use the platform's "keep running after the response" hook so the work is not frozen when the response closes.
4. Retry with backoff, and stop eventually
After each attempt, record when the next one is allowed. Doubling the wait gives a struggling service room to recover:
const backoffMinutes = (attempts) =>
Math.min(5 * 2 ** Math.max(0, attempts - 1), 720); // 5, 10, 20, 40 ... capped at 12h
A scheduled job picks up rows where any channel is pending or failed, the wait has passed and attempts are under a cap (eight is plenty). A partial index on those statuses keeps that query cheap however large the table grows. Leads that hit the cap stay in the table with their last error, so a person can look at them.
5. Make every send idempotent
Retries mean a request might succeed twice: the first call worked but the response was lost. Give providers a key derived from the lead, so the second send is ignored:
headers: { 'Idempotency-Key': `lead-ack-${lead.id}` }
Combined with per-channel status, a visitor gets one confirmation email, not three.
6. Keep personal data out of your logs
A lead is personal data, and error messages love to quote it. Log the status code and the channel, never the response body or the submitted row. Redact anything that looks like a credential before it reaches a log line: pasted keys are the usual way secrets leak. And set a retention period, so old leads are deleted on a schedule instead of piling up forever.
The checklist
- Lead written to the database before any outside call.
- Success returned to the visitor, dispatch done afterwards.
- One status column per channel; retries touch only failures.
- Exponential backoff, an attempt cap and a scheduled sweep.
- Idempotency keys on every outbound send.
- No personal data or secrets in logs; a retention period.
This is how the form on this site works. The contact form is the live version, and if you want one built for your product, tell us what you need.