Recurring User Reminders in a SaaS Backend: Choose Queues Over Cron When Latency Matters
Use cron as a bounded coordinator and a durable queue as the delivery boundary. For recurring user reminders about shipment status, this is the simplest design to implement in a SaaS backend when a late notification can cause a support or settlement problem: cron claims due work in batches, creates idempotent jobs, and workers fan out one shipment update to many subscribers. A direct timer scan is cheaper to operate at small volume, but it couples discovery, retry, and notification latency into one process.
That boundary matters.
The invariant is more important than the scheduler: each recurrence window gets a stable identity, each subscriber delivery can be retried without duplicating its business effect, and the audit trail records the transition from due to attempted to acknowledged or permanently failed. Exactly-once delivery is not a property I assume from a queue. I design an exactly-once effect at the consumer boundary.
How should I implement recurring user reminders using cron and queues?
The short answer is to let cron discover due occurrences and let queues carry delivery work. This separation keeps the implementation small while giving a SaaS backend a place to absorb bursts.
What should cron own, and what should the queue own?
Cron should answer one narrow question: which recurrence windows became due? It should not make network calls to every subscriber. A worker should answer a different question: can this particular delivery be processed now, and can it be retried safely? Keeping those questions separate makes the latency-versus-cost decision visible instead of hiding it in a large scheduler process.
For a reminder with schedule R, occurrence time T, shipment S, and subscriber U, derive a deterministic key such as R:T:S:U. Store it with a uniqueness constraint. The cron run claims rows whose next_run_at is at or before the current time, advances the schedule transactionally, and enqueues delivery records. If the enqueue operation is not part of the same transaction, use an outbox row in that transaction and publish the row from a separate relay. A crash between database commit and queue publication must leave something recoverable. I initially treated the outbox as optional; the failure boundary made that assumption untenable.
A useful decision table is deliberately plain:
| Design | Latency behavior | Operational cost | Failure boundary | Use it when |
|---|---|---|---|---|
| One cron process scans and sends | Bounded by scan duration and request fan-out | Lowest initial component count | Scheduler owns retries and network failures | Subscriber count is small and delivery is non-critical |
| Cron claims due rows, queue workers fan out | Workers can consume in parallel; queue depth exposes delay | More moving parts and observability | Claiming, publication, and delivery can fail independently | Shipment updates need predictable recovery or many subscribers |
| Distributed timer per reminder | Can be precise, but timer state is distributed | Highest coordination burden | Timer ownership and cancellation become the hard parts | A platform already provides durable, inspectable timers |
The second row is my default for the stated fintech scenario. It spends a little more operational attention to buy a smaller failure domain around external notification calls. That is a trade, not a universal rule.
Why does fan-out need an audit trail?
A shipment status event can arrive twice, arrive out of order, or be replayed after a consumer timeout. A recurring reminder can also cross a daylight-saving transition or be edited while an occurrence is being claimed. These are ordinary state transitions, not exceptional cases.
I keep a recurrence record, an occurrence record, and a delivery record. The occurrence records the intended scheduled instant and the schedule version used to calculate it. The delivery record includes the subscriber, channel, attempt count, last error class, and a provider-independent status. A unique key on occurrence plus subscriber prevents duplicate insertion; a consumer-side idempotency key prevents a retry from repeating the external side effect when the worker did not receive the first response.
The words “exactly once” need discipline here. A database transaction can make the state change and its audit entry atomic. It cannot make an arbitrary remote notification endpoint participate in that transaction. The honest contract is at-least-once attempts, idempotent effects, and a durable record of what the system believes happened. If the endpoint supports idempotency keys, pass the stable delivery key. If it does not, record the uncertainty and require reconciliation rather than silently claiming success.
The critical path can stay small. This Go example shows the boundary, not a vendor SDK or a complete queue implementation:
type Delivery struct {
\tID string
\tOccurrenceID string
\tSubscriberID string
\tShipmentID string
\tIdempotencyKey string
}
func processDelivery(ctx context.Context, d Delivery, sender Sender, audit AuditLog) error {
\tif audit.AlreadyApplied(ctx, d.IdempotencyKey) {
\t\treturn nil
\t}
\terr := sender.Send(ctx, d.SubscriberID, d.ShipmentID, d.IdempotencyKey)
\tif err != nil {
\t\t_ = audit.RecordAttempt(ctx, d, classify(err))
\t\treturn err
\t}
\treturn audit.RecordApplied(ctx, d)
}
The real implementation must make AlreadyApplied and RecordApplied safe under concurrent consumers, usually with a conditional insert or update. The important point is that the queue message is a trigger to consult durable state; it is not the ledger.
How should the scheduler behave under load?
Use a short, fixed claim window rather than loading every overdue reminder into memory. Claiming should be bounded by a batch size, use row-level coordination appropriate to the database, and include a lease or claim timestamp that can expire. A worker that dies after claiming work must not create a permanent hole. A second scheduler run should be able to recover expired claims without making two successful effects, because the delivery key remains stable.
The queue needs separate measurements for queue age, claim lag, processing duration, retry count, and terminal failures. “Cron succeeded” is not a delivery metric. I would alert on the age of the oldest due occurrence and on the ratio of deliveries that require reconciliation. Those signals distinguish a scheduler that is not running from a worker pool that is saturated.
Cost enters through batch size, polling frequency, retained audit data, and worker concurrency. It should not decide whether a duplicate financial effect is acceptable. If latency is soft, a five-minute scan interval and modest workers may be adequate. If a shipment update drives a user action or a regulated notification window, reduce the allowed queue age and scale workers based on measured backlog. Keep the policy explicit.
There is a real limitation here: queue fan-out is the wrong choice for a tiny, low-consequence reminder service whose team cannot operate leases, backlog alerts, dead-letter handling, and reconciliation. In that case, inline cron scanning is the better boundary. The extra queue is justified by latency and recovery requirements, not by fashion.
External destinations also deserve a trust boundary. A user-controlled callback URL can turn a notification worker into a server-side request forgery primitive; validate destination schemes and network ranges according to the deployment model, and do not treat a successful TCP connection as authorization. OWASP’s SSRF guidance is a useful baseline, but application-specific allowlists and egress controls still belong in the design.
When is the simpler timer scan the right answer?
The rejected option is not wrong. A single cron process that selects due reminders and sends them inline is appropriate when the subscriber population is bounded, lateness is acceptable, and the notification action is already idempotent. It has fewer operational surfaces, fewer dashboards, and a shorter path from database row to test assertion. For an internal prototype, that simplicity can outweigh the queue’s recovery benefits. That is the trade-off: fewer components, but a larger blast radius when one destination stalls.
I reject it for a growing shipment fan-out because one slow destination extends the scan, one process now owns retry policy, and a process restart entangles schedule recovery with in-flight network calls. The queue design is justified only if the team will operate its visibility: backlog age, poison messages, leases, dead-letter handling, and reconciliation. A queue without those controls is ceremony with a new place to lose work.
The practical rule is therefore conditional: choose inline cron scanning for bounded, low-consequence reminders; choose cron plus an idempotent queue fan-out when latency has to remain predictable while subscriber count and failure diversity grow. In both designs, the durable occurrence and delivery records are the source of truth.
Top comments (0)