Prompt
How do I send order confirmations from my backend reliably?
Latest observation
A reliable backend flow for order confirmations usually needs two things:
- Don’t tie email sending to the user’s request
- Make the send process durable and retryable
A good pattern is:
Recommended approach
1) Persist the order first
When an order is created, write it to your database in a transaction.
2) Write an “email to send” record
In the same transaction, create an outbox/job row like:
event_type = order_confirmationorder_id = ...status = pendingattempt_count = 0
This is often called the outbox pattern.
3) Return success to the client
Don’t wait for the email to be sent before responding.
4) A background worker sends the email
A separate worker process reads pending rows/jobs and sends the confirmation email.
5) Mark success or retry
- If send succeeds: mark job as
sent - If it fails: increment attempts and retry later with backoff
- If it keeps failing: move it to a dead-letter state / manual review
Why this is more reliable
If you send the email directly in the HTTP request:
- the request can time out
- the email provider can fail temporarily
- your app can crash after the order is saved but before email send
- retries can accidentally send duplicate emails
With the outbox + worker approach, you can recover from failures and avoid losing the notification.
Key reliability rules
Use idempotency
Ensure the same order confirmation isn’t sent twice.
Examples:
- store a unique
order_confirmation_sent_at - enforce uniqueness on
(order_id, notification_type) - use provider idempotency keys if supported
Use retries with backoff
For transient failures:
- retry after 1 min, 5 min, 15 min, etc.
- cap the number of retries
Make the worker safe to restart
The worker should be able to stop and restart without losing pending jobs.
Log and monitor failures
Track:
- failed send count
- dead-letter queue size
- provider errors
- delivery latency
Simple architecture
API
- create order
- create outbox event in DB
- commit transaction
Worker
- poll outbox table or consume queue
- send email
- update status
Email provider
- SMTP / SendGrid / SES / Mailgun, etc.
Minimal pseudo-flow
POST /checkout
begin transaction
save order
save outbox record (order_confirmation)
commit
return 200
worker loop:
fetch pending outbox records
for each record:
send email
if success -> mark sent
if fail -> mark retry / increment attempts
If you want the simplest reliable option
Use a managed queue + worker:
- AWS SQS + Lambda/ECS worker + SES
- RabbitMQ + worker
- Sidekiq / Celery / BullMQ depending on stack
- Transactional email service with retry support
Common mistake to avoid
Don’t do this:
save order
send email immediately
if email fails, rollback order
The order should not depend on email success. The email is a side effect, not the source of truth.
If you want, I can show:
- a database schema for the outbox pattern
- an example in Node.js / Python / Ruby / Java
- or a queue-based design for your specific stack