Notify Queue
Schedules email, SMS and push, and delivers each one exactly once — even with ten workers polling the same table at the same moment.
- NestJS
- TypeScript
- PostgreSQL
- TypeORM
- Docker
The problem
Scheduling a notification is the easy half. Delivering it exactly once is the half that goes wrong, and it goes wrong quietly: a duplicate SMS looks like a delivery, not a bug. Nobody files a ticket. The customer just gets told twice, and then charged twice for the privilege if the message was transactional.
The failure only appears under concurrency. One worker is always correct. Ten workers polling the same table for due rows is where two of them read the same job, both decide it is theirs, and both send it.
What I built
- Claims use
SELECT … FOR UPDATE SKIP LOCKED. Workers never contend for the same row, so adding workers adds throughput instead of adding duplicates. A worker that dies mid-claim has its jobs recovered rather than stranded. - Every request carries an idempotency key, so the same request sent twice creates one notification, not two.
- Retries back off exponentially and stop at six. After that the notification is set aside in a dead-letter queue rather than retried forever, and an operator can send it back one at a time or in batches.
- Rate limiting holds a recipient to ten an hour, and the eleventh waits rather than failing. A rate limit that drops messages is a different bug wearing a policy's clothes.
- Webhooks go through an outbox, so a crash between delivering and telling you about it cannot lose the notice.
The test that matters most is the one designed to fail
A concurrency guarantee nobody has watched break is a guarantee nobody has
tested. So alongside the real claim there is a control test that
implements the naive SELECT then UPDATE and
demonstrates it handing one job to many workers.
- 500 jobs drained across ten polling workers, every one delivered exactly once.
- One job, ten real worker applications polling simultaneously: one provider call, one delivery.
- 200 simultaneous cancel-versus-claim races, every one resolving one way only. A notification is either cancelled or sent, never both and never neither.
- Concurrent idempotency and rate-limit races, each with its own suite.
Decisions worth naming
- Recipients, payloads and idempotency keys never reach a log line. A notification service handles phone numbers and email addresses by definition, and logs are the easiest place to leak them.
- One error-logging path, one job-state conflict exception, one guarded update. State transitions are the thing most likely to drift into three slightly different versions, so there is one of each.
- The API reference is written from real responses, including the errors, rather than from the handler signatures.
- The design is documented twice — once in plain English for anyone, once in full for engineers.