DEV Community

ZebedeeHolloway9023
ZebedeeHolloway9023

Posted on

Node.js Customer Support Reports: Recovering Failed Email Sends

Short answer: for a customer-support system that sends a daily report email and also runs periodic cleanup, let cron create a run, let a queue retry each delivery, and let a DLQ hold messages that need a human decision; do not rerun the whole cron batch as the default recovery action.

The deciding constraint is latency versus cost. A second cron run is cheap to understand, but it makes recovery as large as the original batch. Per-recipient queue work costs more operational machinery, yet it reduces the latency of the useful retry and avoids reopening successful sends.

This is an architecture decision record, not a vendor shopping list. The important question is where the system records what it intended, what it attempted, and what it finally accepted.

Should a daily report email retry failed sends through a queue or a cron rerun?

Use cron as the clock, not as the delivery ledger. The scheduled handler should create one immutable report-run record, enqueue one delivery intent per recipient, and return. A worker should claim one intent, send the message, record the result, and acknowledge the queue message only after the result is known. A temporary failure can be retried; a terminal failure can be moved to a dead-letter queue (DLQ) for review and deliberate redrive.

The same split works for periodic cleanup. The schedule says that stale support artifacts are due for examination. It should not perform an unbounded cleanup inside a web request or make a later operator guess which rows were already processed.

The business key is the guardrail: report-run-id + recipient-id for mail, or cleanup-run-id + object-id for cleanup. A queue message ID is transport metadata, so it is the wrong key when a message is republished. A durable ledger should enforce uniqueness on the business key and retain the state transitions needed for reconciliation.

That is exactly-once effect built on at-least-once delivery. The transport may deliver an instruction again. The business operation must still produce one accepted effect.

Keep the recovery unit small.

Record the invariants before choosing the mechanism

The first invariant is idempotency: handling the same delivery intent twice must not send two emails. The second is isolation: one bad address or temporary provider response must not reopen successful recipients. The third is auditability: an operator must be able to answer which report was intended for which recipient, how many attempts occurred, and why the final state is sent, retryable, or dead_lettered.

The failure boundaries follow from those invariants. A scheduler timeout proves only that the invocation crossed its execution boundary; it does not prove that no delivery intents were created. A worker timeout proves only that the consumer lost certainty about that attempt; it does not authorize an unguarded second send. The ledger decides what a redelivery may do. It won't make the network transactional, but it can make the application's decision explicit.

A retry policy needs a classification step. Temporary transport failure, throttling, or a downstream timeout may be retryable. An invalid destination or a rejected payload is usually terminal until data is corrected. The precise classification belongs to the mail provider contract and the application's compliance policy, so your mileage may vary; the queue should preserve the classification rather than flattening every failure into “try again.” In a payment or ledger system, I would also require the audit event to name the policy version that made that decision, because a later replay must be explainable even after the retry rules change.

Do not confuse a DLQ with an archive. A DLQ is an operational holding area for work that exceeded an automated policy. The durable ledger is the audit record. Queue retention can expire, and an acknowledged message is no longer a reliable source of historical truth.

Compare recovery boundaries, not feature counts

The useful comparison is the unit that can be retried without duplicating completed work.

Design Recovery unit Where idempotency lives Latency and cost trade-off Valid boundary
Cron-only batch rerun The whole candidate batch Application code and a durable ledger Low setup cost; a small failure waits for a coarse replay and scans completed work Small, naturally idempotent jobs where duplicate effects are acceptable
Cron plus queue One delivery or cleanup intent Consumer plus a unique business key More moving parts; failed work can retry without waiting for the next full run Daily reports and cleanup with partial failures or distinct recipient state
Queue plus DLQ One intent after retry exhaustion Ledger plus an operator workflow Review work is delayed by design; it avoids infinite automatic retries Invalid data, repeated downstream rejection, or policy-controlled manual recovery
Workflow engine A durable workflow step or activity Workflow state plus business records Higher operational cost; useful when many dependent steps must be resumed Long-lived orchestration, compensation, and explicit step dependencies

The table also explains why a DLQ cannot replace a retry queue. A DLQ answers “what needs attention after automation stopped?” It does not answer “when should the next transient attempt happen?” Likewise, a cron rerun is not a substitute for a delivery ledger: it can schedule work, but it cannot by itself prove that a previous attempt was accepted.

Make the worker's critical path idempotent

The following Go example shows the contract at the worker boundary. It uses interfaces for the queue and mail provider, so the recovery rule is independent of a particular service. The map is an executable teaching model; production code should put the claim behind a database uniqueness constraint and append an audit event for every state transition.

package main

import (
    "context"
    "errors"
    "fmt"
    "sync"
)

type Delivery struct {
    MessageID string
    RunID     string
    Recipient string
}

type Ledger struct {
    mu     sync.Mutex
    status map[string]string
}

func (l *Ledger) Claim(key string) bool {
    l.mu.Lock()
    defer l.mu.Unlock()
    if l.status[key] == "processing" || l.status[key] == "sent" {
        return false
    }
    l.status[key] = "processing"
    return true
}

func (l *Ledger) Set(key, state string) {
    l.mu.Lock()
    defer l.mu.Unlock()
    l.status[key] = state
}

type Mailer interface {
    Send(context.Context, Delivery) error
}

type Queue interface {
    Ack(context.Context, string) error
    Retry(context.Context, string) error
    DeadLetter(context.Context, string) error
}

type Worker struct {
    ledger *Ledger
    mailer Mailer
    queue  Queue
}

func (w Worker) Handle(ctx context.Context, d Delivery) error {
    key := d.RunID + ":" + d.Recipient
    if !w.ledger.Claim(key) {
        return w.queue.Ack(ctx, d.MessageID)
    }

    if err := w.mailer.Send(ctx, d); err != nil {
        w.ledger.Set(key, "retryable")
        return errors.Join(err, w.queue.Retry(ctx, d.MessageID))
    }

    w.ledger.Set(key, "sent")
    return w.queue.Ack(ctx, d.MessageID)
}

func main() {
    ledger := &Ledger{status: make(map[string]string)}
    _ = Worker{ledger: ledger}
    fmt.Println("the claim belongs to the business key, not the message ID")
}
Enter fullscreen mode Exit fullscreen mode

There is an uncomfortable boundary here. The send and the ledger update are usually different systems, so they cannot be assumed to commit as one transaction. If the provider accepts the email and the worker disappears before recording sent, the queue may redeliver. That is why the business key, provider idempotency facility where available, and reconciliation process must be designed together. A naked Ack is not proof of delivery.

Reject the full rerun, but keep its valid use case

The rejected option is a cron handler that rebuilds the entire daily report and sends every candidate again whenever one recipient fails. Its appeal is real: one deployment, one visible trigger, and little queue administration. It is suitable when the operation is inherently idempotent, the batch is small, and duplicate effects have negligible consequences.

It is not suitable when support agents depend on a report arriving quickly, recipient failures are independent, or a cleanup must avoid deleting an object twice. In those cases, stick with cron-only execution only if the ledger can cheaply isolate completed work; otherwise use a queue and make the DLQ a deliberate operator boundary. The extra component is a cost, but it buys a smaller recovery unit and a clearer audit trail.

No magic retry count fixes a missing invariant.

For a Node.js application, the same arrangement can be expressed with any scheduler and queue adapter, while the critical contract stays stable: create a run, persist intent, process by business key, classify failures, and reconcile accepted effects. The implementation language changes the API shape; it should not change the recovery semantics.

References

Top comments (0)