A website enquiry looks like one click, but it crosses a form, a server, a CRM, and a messaging service, and any of them can fail. Before anything I build touches a real customer, I run ten failure tests on sample data. Below are the tests, then the five rules that make them pass: save through an outbox before you confirm, make every step idempotent, retry with a per-step budget, track state per lead, and stop on reply or STOP.
One click, four systems
When a visitor sends an enquiry form, the business sees one event. Underneath, it is a chain:
- The browser posts the form to a server.
- The server writes the enquiry somewhere.
- Something creates or updates a record in the CRM.
- Something sends a first reply by email or WhatsApp, and schedules follow-ups.
Each hop can time out, fail halfway or run twice. The browser can lose its connection after the server saved the data but before the "thank you" arrived. A webhook can be delivered twice. The CRM can be down for ten minutes. If you design for the happy path only, these failures turn into the worst kind of bug: a lead that silently disappears.
So I treat the enquiry form as what it is, a distributed system, and test it like one.
The ten failure tests I run before launch
I run these on sample data, with test names and test numbers:
- A normal enquiry, end to end.
- The same form sent twice within a second.
- The same webhook delivered twice.
- The CRM unavailable for the first two retries.
- The CRM unavailable for longer than all retries, to check the alert and the dead-letter entry.
- A message send that fails, and one that times out with no answer.
- A follow-up due after the 24-hour window, to check it uses a template.
- A customer reply arriving one minute before a follow-up is due.
- A customer sending STOP, then a follow-up coming due.
- A required field missing, to check the form keeps what was typed.
Each test has an expected final state from the state table in Rule 4. If the state is wrong, the build is not ready. The five rules below are what make these tests pass.
Rule 1: save before you say thank you, through an outbox
The confirmation the visitor sees must be a consequence of a durable write, not of the click. Write the enquiry to your own store first. Only then respond with success. Everything after that, the CRM, the reply, the follow-up, runs from the stored record.
There is a trap here. If your store and your queue are separate systems, writing to both is two writes, not one. If the process dies after the lead is saved and before the jobs are queued, the lead exists and nothing ever runs for it. So I write the jobs into the same database transaction as the lead, an outbox, and a worker picks them up.
on POST /enquiry (form): validate(form) # show errors, keep what was typed with db.transaction(): lead = leads.insert_or_get(key=form.submission_id, data=form) outbox.insert_once(lead.id, "sync_crm") outbox.insert_once(lead.id, "first_reply") return 200 "Thanks, we have your enquiry" worker, every few seconds: for job in outbox.pending(): run(job) # retries and alerts as in Rule 3 If the transaction fails, the visitor sees an error and keeps their typed text. They can try again. What they never see is a "thank you" for an enquiry that does not exist, and no saved lead is left without its jobs.
Rule 2: make every step idempotent
RFC 9110 defines a method as idempotent when sending the same request several times has the same intended effect as sending it once. PUT and DELETE are; POST is not. A form submit is a POST, so retrying it is not safe by default.
The fix is a key. When the form renders, generate a unique submission_id and put it in a hidden field. The server stores it with a unique constraint, so a double click or a browser retry returns the existing record instead of creating a second one. There is an IETF Internet-Draft, "The Idempotency-Key HTTP Header Field", that describes the same idea for HTTP APIs as a request header. It is still a draft, but the pattern is sound either way.
Apply the same thinking to every later step:
- Webhooks in: store the provider's event ID and ignore one you have already processed.
- CRM writes: upsert on a stable key such as the normalised phone number or email, not "create new".
- Messages out: record the key, such as
lead_id + step_name, with statuspending, send, then mark itsentwith the provider's message ID. On retry, a key that is alreadysentis skipped.
The last one is where the real decision sits. A key stuck at pending is uncertain: the send may or may not have reached the customer. There is no exactly-once delivery to a messaging API, so you have to choose a failure mode. For follow-ups I choose at-most-once: I would rather alert on an uncertain send than message a customer twice. A duplicate CRM record is untidy. A duplicate WhatsApp message is something the customer sees.
Rule 3: retry with a budget per step, then stop and alert
Most failures are temporary, so retry. But retry with spacing, some randomness and a limit, and do not give every step the same budget. A first reply is worth little after an hour, so it gets short retries and then an alert. A CRM sync can wait hours.
retry_delays = { "first_reply": [30s, 2m, 10m], # then alert: a late first reply is worth little "sync_crm": [1m, 5m, 30m, 2h], # this one can wait } # each delay plus random jitter on job failure (job): delays = retry_delays[job.step] if job.attempts < len(delays): schedule(job, after=delays[job.attempts] + jitter()) else: dead_letter.add(job, last_error) alert(owner, "Lead " + job.lead_id + " stuck at " + job.step) lead.mark(job.step, "failed") The dead-letter list is the important part. A job that has used up its retries goes to a list someone is alerted about, with the lead ID and the step it failed on. Without that list, the automation fails quietly, which is the one thing it must never do.
Rule 4: track state per lead
"We received it" and "we followed it up" are different facts, and I keep them as separate states on each lead.
| State | Set when | What runs next | Alert if stuck for |
|---|---|---|---|
| received | The enquiry and its outbox jobs are stored | CRM sync and first reply | 15 minutes |
| in_crm | The CRM record exists | Set the next step | 1 hour |
| next_step_set | A follow-up time is saved | Wait, then send | Past its due time |
| followed_up | A message was actually sent | Wait for a reply | Not needed |
| replied | The customer answered | Follow-ups stop | Not needed |
| stopped | The customer sent STOP | Nothing, ever | Not needed |
| failed | Retries are used up, or a send is uncertain | Dead-letter list | At once |
With this table you can answer the question a business really asks: which enquiries have not had a message yet? A notification that "a lead came in" does not answer it. A state does.
Rule 5: stop on reply, honour STOP, respect the window
Two rules sit above every scheduled follow-up: if the customer replied, stop; if the customer asked to stop, stop for good. WhatsApp's own policy says a business must honour any request to stop or opt out, whether it comes on WhatsApp or elsewhere.
WhatsApp adds a timing constraint. When a customer messages a business, a 24-hour customer service window opens, and it resets with each new customer message. Inside it, you can send free-form replies. Outside it, you can send only message templates that Meta has accepted, and only to people who agreed to hear from you. So the scheduler has to check the window before it picks what to send, and it should leave a margin, because a message queued at 23 hours 59 minutes can reach the API after the window has closed.
before sending follow-up (lead): if lead.state in ["replied", "stopped"]: return if not lead.opted_in: return if now - lead.last_customer_message < 23h: # leave room for queue delay send_freeform(lead) else: send_template(lead, accepted_template) There is a cost side too. Meta charges per delivered message, and from 1 October 2026 it charges for service messages inside the window after a free tier of 1,000 per business phone number per month. Duplicate sends are now a billing bug as well as a customer-experience one, which is one more reason for Rule 2.
If you only run one test
Run number 8: a customer reply arriving one minute before a follow-up is due. It is the one that shows whether your states and your scheduler agree.