Queue for WhatsApp webhooks: reply, then process

Why to split receiving from processing: the receiver validates, stores and returns 2xx; a worker consumes the queue with deduplication and reprocessing.

The most expensive architecture mistake in WhatsApp integrations is doing everything inside the webhook request: validate, query the database, call a language model, compose the reply, send it. It works in the demo and breaks on the first burst of messages. The fix is old and simple: a queue between receiving and processing. This article explains why, how and where the pitfalls are.

Why separate receiving from processing

Separating is the only way to reply fast without losing the guarantee of processing. CRPRO Hub expects a 2xx within 10 seconds; a slower response counts as a failure and the delivery goes back to the retry queue. Meta, for its part, resends anything not answered with 200 for up to 7 days, with a risk of duplicates. A slow handler becomes a duplicate-event factory itself.

There is also the cascade effect. A slow receiver holds connections; busy connections make new deliveries time out; failures add up and, at the Hub, 20 consecutive failures pause the endpoint. The bottleneck of a single slow call, such as a language model that took too long, becomes a receiving outage for every event on the endpoint.

With a queue, the receiver does short, predictable work: verify the signature, store the raw body and reply. What is slow and unpredictable lives in a worker, which can fail, retry and scale without anyone outside noticing.

What the receiver should do, and only that

The receiver validates the signature, stores the raw body in a durable queue and returns 2xx. Nothing more. Validating first keeps junk out of the queue. Storing the raw body, not the processed object, preserves the option to reprocess with corrected code. Replying only after storing guarantees that 2xx means "it is safe with me".

If writing to the queue fails, return an error status such as 503 on purpose. The sender's retry will cover the failure. Acknowledging an event you could not store is the only way to truly lose it, because neither side will try again.

On size, Meta documents payloads of up to 3 MB and POSTs that can carry up to 1,000 batched updates, without guaranteeing the batching. Size your queue message for that, or write the body to object storage and enqueue only the reference.

A concrete receiver and worker example

The skeleton below shows both halves. The receiver keeps the receive time and the delivery id next to the body; the worker parses, deduplicates and handles. The message leaves the queue only when handling finishes without error.

// receiver: validate, store, reply app.post('/webhooks/crprohub', express.raw({ type: '*/*' }), async (req, res) => { if (!assinaturaValida(req)) return res.sendStatus(401) try { await fila.publicar('webhook-in', { recebidoEm: Date.now(), entregaId: req.headers['x-hub-delivery-id'], corpo: req.body.toString('utf8'), }) } catch { return res.sendStatus(503) // not stored: ask for a retry } res.sendStatus(200) // stored: acknowledge }) // worker: process outside the request fila.consumir('webhook-in', async (msg) => { const evento = JSON.parse(msg.corpo) if (!(await primeiraVez(evento.id))) return // duplicate await tratar(evento) // if it throws, the queue redelivers the msg })

Two points deserve attention. First, JSON parsing happens in the worker, after validation: the receiver works with bytes, which avoids the classic signature failure caused by re-serialization. Second, deduplication sits in the worker, not the receiver, because that is where the event is actually processed; the receiver can return 200 for a duplicate without even looking.

Guarantees the queue must provide

The queue must be durable, deliver at least once and let you reprocess failed messages. An in-memory queue loses everything if the process restarts, which defeats the reason for having one. At-least-once delivery means the worker will also see duplicates, so idempotency is not optional: it lives in the worker.

  • Durability: the message survives a process crash and a deploy.
  • Redelivery: if the worker throws, the message comes back to be tried again.
  • Attempt limit and a dead-letter queue: an event that always fails cannot block the others.
  • Visibility: queue length and the age of the oldest message are the two numbers to monitor.
  • Ordering: do not assume the queue preserves event order; see the article on out-of-order statuses.

The dead-letter queue deserves a comment. Without it, a "poison" event that causes a code error on every attempt is redelivered forever and eats the worker. With it, after N attempts the message steps aside and is kept for inspection, and the rest of the queue moves on.

When you still need to reply fast to the customer

Replying fast to the webhook is not the same as replying fast to the customer. The webhook gets its 200 in milliseconds; the reply to the user on WhatsApp leaves the worker when it is ready. Inside the 24-hour service window you can send free-form text, so the worker has room to call slow systems. What the user feels is the worker's latency, not the receiver's.

The same holds for flows built with visual automation. In tools like n8n, the webhook node can respond immediately and let the rest of the flow run afterwards. The idea is the same as the queue, with the tool itself playing the worker.

Reconciliation: the queue's safety net

Even with a queue, it helps to have a routine that compares what the Hub (or Meta) delivered with what you processed. If the Hub endpoint pauses, events in that interval generate no delivery and cannot be replayed; the way out is to query the channel's inbound messages through the API and process whatever is missing. A daily routine that does this usually costs a few lines and prevents the worst category of failure: the one nobody notices.

A practical way to decide how much machinery you need: count how many events per minute your busiest number can produce, then ask how long your slowest step takes. If the product of the two exceeds what a single process can hold in flight, you need the queue today, not later. If it is far below, you still want the queue for durability, but a simple table polled by one worker can be enough. The pattern matters more than the product you pick.

The endpoint configuration is in the CRPRO Hub webhooks reference, and the call authentication in authentication.

Frequently asked questions

Why not process everything inside the webhook POST?

Because the Hub expects a 2xx within 10 seconds and Meta retries anything that does not get a 200. Slow processing causes timeouts, retries and duplicates.

What should I store in the queue: the object or the raw body?

The raw body, after validating the signature. That way you can reprocess with corrected code without relying on the source to resend.

If the queue is down, what does the receiver reply?

An error status such as 503. Acknowledging with 2xx an event that was not stored is the only way to truly lose it.

Do I still need deduplication with a queue?

Yes. The queue delivers at least once and the source can also repeat events. Deduplicate in the worker on the event identifier.

Fontes

Documentação oficial da Meta, conferida em 31 de outubro de 2026.