Prompt
How do I process webhooks asynchronously without blocking the app?
Latest observation
To process webhooks asynchronously without blocking your app, the key idea is:
- Receive the webhook quickly
- Validate it
- Store or enqueue it
- Return a 2xx response immediately
- Process the event in a background worker
This avoids keeping the HTTP request open while your app does longer work.
Recommended pattern
1) Webhook endpoint does minimal work
Your webhook handler should only:
- verify the signature/authenticity
- parse the payload
- persist the event or push it to a queue
- respond
200 OKor202 Accepted
Example flow:
Webhook provider -> your HTTP endpoint -> queue/db -> background worker -> business logic
2) Use a queue or job system
Common options:
- Message queue: RabbitMQ, SQS, Kafka, Redis queues
- Job framework: Celery, Sidekiq, BullMQ, Hangfire, Resque
- Database-backed queue if you want simplicity at first
The webhook handler should enqueue a job, not do the heavy work itself.
3) Make processing idempotent
Webhooks are often retried, so your worker should safely handle duplicates:
- include a unique event ID
- store processed IDs
- ignore already-processed events
4) Return fast
Most providers expect a quick response, often within a few seconds. If you take too long:
- they may retry
- they may mark delivery as failed
- you can get duplicate events
Example architecture
Webhook endpoint
- Validate signature
- Save raw event to DB
- Add job to queue
- Respond immediately
Worker
- Pull job from queue
- Process event
- Update DB/state
- Retry on failure with backoff
Example pseudocode
@app.post("/webhook")
def webhook():
event = request.json
if not verify_signature(request):
return {"error": "invalid signature"}, 401
save_event_to_db(event) # or enqueue_job(event)
return {"status": "accepted"}, 202
Background worker:
def process_event(event_id):
event = load_event_from_db(event_id)
if already_processed(event_id):
return
handle_business_logic(event)
mark_processed(event_id)
Important practices
- Acknowledge quickly: don’t do slow API calls in the webhook request
- Retry safely: use exponential backoff
- Log failures: keep raw payloads for debugging
- Use dead-letter queues: for events that repeatedly fail
- Secure the endpoint: signature verification, IP allowlists if supported
- Monitor queue depth: so you notice backlog
If you don’t want a queue yet
You can still avoid blocking by spawning background work, but this is less reliable because the process can crash before work finishes. For production, a queue is much better.
If you want, I can show you how to do this in a specific stack like Node.js (Express/BullMQ), Python (FastAPI/Celery), Django, Ruby on Rails, or .NET.