Prompt
How do I queue notifications so they don't get dropped?
Latest observation
To prevent notifications from being dropped, you generally want to buffer them in a durable queue and have a worker deliver them asynchronously rather than sending directly from the request thread.
Basic pattern
-
Producer receives an event
- Example: user signup, payment failed, password reset requested.
-
Write a notification job to a queue
- Store enough data to recreate the notification.
- Prefer a persistent queue or database-backed job table if you can’t afford loss.
-
Worker consumes the queue
- A background process sends the notification.
- If sending fails, it retries.
-
Use retry + dead-letter handling
- Retry transient failures with backoff.
- After repeated failures, move the job to a dead-letter queue/table for inspection.
Good queue options
- Message brokers: RabbitMQ, Redis queues, Kafka, SQS, Pub/Sub
- Database queue: a
notificationsorjobstable with status fields - Job frameworks: Sidekiq, Celery, BullMQ, Hangfire, Resque, etc.
Important reliability practices
1. Make enqueueing durable
If the process crashes after generating the event but before delivery, the queued job should still exist.
2. Acknowledge only after success
The worker should only mark a message as done after the notification provider confirms delivery.
3. Use retries with exponential backoff
For temporary issues like timeouts or rate limits:
- retry after 1s, 5s, 30s, etc.
- cap max attempts
4. Use idempotency
Prevent duplicate notifications when retries happen:
- include a unique notification ID
- store delivered IDs
- make downstream calls idempotent when possible
5. Separate queues by priority/type
For example:
- critical alerts
- email notifications
- marketing notifications
This keeps low-priority traffic from blocking high-priority messages.
6. Add monitoring
Track:
- queue depth
- age of oldest job
- failure rate
- retry count
- dead-letter count
Simple database-backed queue example
A table like:
notifications (
id UUID PRIMARY KEY,
type TEXT,
payload JSONB,
status TEXT, -- pending, processing, sent, failed
attempts INT DEFAULT 0,
next_attempt_at TIMESTAMP,
created_at TIMESTAMP,
updated_at TIMESTAMP
)
Worker logic:
- select pending jobs where
next_attempt_at <= now() - lock row
- process
- on success: mark
sent - on failure: increment attempts, set
next_attempt_atinto the future - after max attempts: mark
failed
If you want guaranteed delivery semantics
You typically aim for at-least-once delivery:
- the notification may be delivered more than once
- but it won’t be silently lost
Exactly-once is much harder and usually not necessary; idempotency is the usual fix.
If you’re using a specific stack
I can give you a concrete implementation for:
- Node.js / BullMQ
- Python / Celery
- Ruby / Sidekiq
- Java / Kafka or RabbitMQ
- .NET / Hangfire
- Postgres-only queue
If you tell me your stack and whether these are email, SMS, push, or in-app notifications, I can sketch the exact queue design and code.