Skip to content

Worker queue for broadcasts #41

Closed
Closed

Description

@JuansesDev

Problem

Broadcast today does for sub := range subscribers inside a goroutine launched on the request thread. For 50k+ subscriber lists this means:

  • The goroutine holds resources for minutes
  • A backend restart in the middle drops the rest of the broadcast on the floor
  • No retry on transient SMTP failures
  • No visibility while it runs ('is it 30% done?' is unanswerable)
  • One slow recipient (DNS timeout, rate-limited host) blocks the next

Proposal

Replace the inline loop with a job queue. On broadcast, enqueue one job per recipient. Workers pull jobs, send, write results. State persists across restarts; visibility is a count of pending vs done.

Backend choice: stay simple. Use asynq (Redis-backed) — already a dep candidate since we use Redis for cache and rate limits. Or roll a lightweight Postgres-backed queue with SELECT FOR UPDATE SKIP LOCKED if we want to avoid Redis as a hard dep.

Schema:

CREATE TABLE broadcast_jobs (
    id, broadcast_id, recipient_email, status VARCHAR,
    attempts INT, last_error TEXT,
    scheduled_at TIMESTAMPTZ, completed_at TIMESTAMPTZ
);

The campaign worker process gains a 'send-job' consumer. Keep open/click pixel handling unchanged — they are stateless HTTP.

Acceptance criteria

  • A broadcast survives a backend restart and resumes where it left off
  • Per-recipient retries on transient SMTP errors with exponential backoff
  • The broadcast detail page shows live progress (X/Y sent)
  • Pro Analytics gains a 'broadcast in flight' status
  • Sustained throughput of ~50 emails/sec on default config

Activity

  1. added
    enhancement
    roadmap
    on Apr 28, 2026
  2. JuansesDev commented on May 28, 2026

    @JuansesDev
    MemberAuthor

    Delivered

    Worker queue infrastructure was already in place; this issue is now fully complete after fixing the last inconsistency around restart recovery.

    Acceptance criteria

    Criterion Status Where
    Broadcast survives backend restart and resumes ✅ broadcast_worker.go (ResetStuckSendingJobs on startup) + ReconcileStuckBroadcasts (62f93de)
    Per-recipient retries with exponential backoff (30s → 2m → 8m → 30m → 1h, max 5 attempts) ✅ broadcast_worker.go:158-185
    Broadcast detail page shows live progress (X/Y) ✅ BroadcastsSection.vue polls GET /projects/:id/broadcasts every 3s while any broadcast is sending
    Pro Analytics gains "broadcast in flight" status ✅ senddock-pro/internal/analytics/analytics.go:355-380 (broadcasts_in_flight field, filters status='sending') + AnalyticsSection.vue:535-549
    Sustained throughput ~50 emails/sec ⏳ Deferred to a separate benchmark issue — never measured in load conditions

    Architecture summary

    • Postgres-backed queue (no Redis dependency added — option B from the proposal)
    • broadcast_jobs table with FOR UPDATE SKIP LOCKED claim pattern
    • 5 worker goroutines per backend instance, panic recovery per goroutine
    • Outcomes are classified as Sent / Suppressed / Bounced / TransientError for correct counter updates and retry decisions
    • Inline for sub := range subscribers was replaced with a single bulk insert into broadcast_jobs; the request returns immediately with the broadcast ID

    Restart-recovery fix (62f93de)

    The previous MarkInProgressBroadcastsInterrupted flagged every 'sending' broadcast as 'interrupted' at startup. But jobs were also reset to 'retry' by ResetStuckSendingJobs, so workers in fact resumed sending. Side effects:

    • UI showed "this broadcast was interrupted, the remaining did not receive the email" while emails were still going out
    • Pro Analytics lost visibility (broadcasts_in_flight filters status='sending')
    • Polling stopped early on the broadcasts list page

    ReconcileStuckBroadcasts now only marks 'completed' the broadcasts whose jobs have all settled, leaving the rest as 'sending' so the worker pool keeps processing. Also reconciles any legacy 'interrupted' rows.

    Follow-ups (separate issues)

    • Throughput benchmark (the ~50 emails/sec claim is unverified)
    • Per-broadcast cancel action (no cancelled status today)
  3. added a commit that references this issue on May 28, 2026
    62f93de
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendenhancementinfraroadmap

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions