Background jobs patterns

From FerroxLabs/murage · View interactive version

Background jobs patterns is an AI agent skill from the GitHub repository FerroxLabs/murage. Guides expert-level background jobs patterns implementation: architecture and optimization decision frameworks, production-ready patterns, and concrete templates for background jobs patterns workflows. Use when the user asks about background jobs patterns, background jobs patterns configuration, or

How to use this skill

Skill file

Background Jobs Patterns

When to Use

Use this skill when:

Do NOT use this skill when:


Process

1. Classify the Job Type and Its Requirements

Before selecting any technology or pattern, identify the exact nature of the work:

2. Choose the Queue Backend and Worker Framework

Match the backend to the classification from Step 1:

3. Design the Job Interface and Payload Contract

A well-designed job interface prevents entire categories of production failures:

4. Implement Retry, Backoff, and Dead Letter Strategy

Retry logic is where most background job systems fail in production:

5. Design for Concurrency, Prioritization, and Rate Limiting

6. Implement Observability and Operational Visibility

Background jobs are invisible unless you instrument them explicitly:

7. Handle Scheduled and Recurring Jobs Correctly

Cron-style recurring jobs in distributed systems require specific care:

8. Validate and Harden the Implementation

Before marking background job infrastructure production-ready:


Output Format

When helping a user design or review background job infrastructure, structure the output as follows:

## Background Job Architecture Assessment

### Job Classification Summary

| Job Class | Type | Latency Class | Delivery Guarantee | Est. Volume/hr | Payload Size |
|---|---|---|---|---|---|
| [job name] | [CPU/IO] | [interactive/near-rt/background/batch] | [at-most/at-least/exactly-once] | [number] | [KB] |

### Queue Backend Recommendation

**Selected Backend:** [Redis/PostgreSQL/RabbitMQ/SQS/Temporal]

**Rationale:**
- [Specific reason tied to job classification above]
- [Operational constraints considered]
- [Trade-offs accepted]

**Rejected alternatives:**
- [Backend]: [Specific reason it was eliminated]

### Queue Topology

[queue_name] (concurrency: N, priority: high/medium/low)

├── JobClass1 (timeout: Xs, max_retries: N)

├── JobClass2 (timeout: Xs, max_retries: N)

└── JobClass3 (timeout: Xs, max_retries: N)

[queue_name]_dlq (alert_threshold: N jobs)


### Retry and Error Policy

| Job Class | Max Retries | Backoff Strategy | Non-Retryable Errors | DLQ |
|---|---|---|---|---|
| [job name] | [N] | [exponential 15s base / none] | [exception classes] | [yes/no] |

### Concurrency and Rate Limits

| Job Class | Max Concurrency | Rate Limit | Lock Strategy |
|---|---|---|---|
| [job name] | [N workers] | [N/sec or none] | [none/redis-nx/redlock] |

### Idempotency Design

| Job Class | Idempotency Key | Guard Implementation |
|---|---|---|
| [job name] | [key formula] | [DB unique index / Redis SETNX / check-then-act] |

### Implementation Template

Core job implementation pattern

class [JobName]

# Configuration block

queue_as :[queue_name]

retry_on [TransientError], wait: :exponentially_longer, attempts: [N]

discard_on [PermanentError]

# Payload: pass IDs only, fetch fresh inside perform

def perform([resource_id], correlation_id:)

return if already_processed?([resource_id]) # idempotency guard

# fetch fresh data

# execute work

# mark as processed (within same transaction if DB-backed)

end

private

def already_processed?(resource_id)

# implementation

end

end


### Observability Checklist

- [ ] Structured logging at enqueue, start, complete, and fail events
- [ ] Queue depth metric exported to monitoring system
- [ ] Queue latency (p50, p95, p99) tracked per queue
- [ ] DLQ depth alert configured at threshold: [N]
- [ ] Worker utilization dashboard created
- [ ] APM trace propagation configured
- [ ] Cron job missed-execution detection configured

### Operational Runbook References

- DLQ review process: [describe or link to runbook]
- Worker scaling procedure: [describe trigger conditions and steps]
- Queue drain emergency procedure: [describe steps for critical outages]

Rules

  1. NEVER enqueue a job inside a database transaction that has not yet committed. If the job executes before the transaction commits, it reads stale or missing data. Enqueue after after_commit hooks or outside the transaction block. This is one of the most common and silent failure modes in Rails and Django applications.
  1. NEVER use a global rescue-and-retry strategy that catches all exceptions without distinguishing retryable from permanent failures. Retrying a 400 Bad Request or a RecordNotFound is wasteful at best and can cause data corruption at worst. Explicitly map exception classes to retry policies.
  1. ALWAYS make job execution idempotent before relying on at-least-once delivery. For any queue backend that provides at-least-once guarantees (Redis-backed queues, SQS, most AMQP configurations), assume every job will be executed more than once at some point. Design accordingly.
  1. NEVER store mutable object state in job arguments. Serialize only IDs, primitive values, or immutable references. An ActiveRecord object, a Python model instance, or a JavaScript class embedded in a job argument will reflect state at enqueue time, not execution time, and will break across deployments that change class structure.
  1. ALWAYS set a per-job execution timeout. A job without a timeout can hang indefinitely due to an unresponsive database connection, external API hang, or infinite loop. Set timeout at 2x the expected p99 execution time and treat timeout as a permanent failure after max retries.
  1. NEVER allow unbounded queue depth growth without backpressure. If producers consistently enqueue faster than consumers process, queue depth grows unbounded, Redis memory or database disk is exhausted, and recovery requires complex drain procedures. Implement producer-side backpressure when queue depth exceeds a threshold.
  1. ALWAYS use separate queues for jobs of different latency classes. Mixing interactive jobs (target < 1s queue latency) with batch jobs (acceptable latency: minutes) in a single queue means batch jobs delay interactive jobs. Separate queues with separate concurrency pools are the correct isolation mechanism.
  1. NEVER deploy a change to a job's argument interface without a transition period. Jobs enqueued under the old interface will exist in the queue when the new worker code deploys. Maintain backward compatibility for argument shape for at least one full deployment cycle, covering the maximum job retention period.
  1. ALWAYS configure and monitor the Dead Letter Queue. A DLQ without monitoring is a data graveyard. Every item in the DLQ represents a failed business operation. DLQ depth must be a first-class operational metric with alerts and an assigned owner.
  1. NEVER use background jobs as a substitute for proper synchronous architecture when consistency is required. If an operation must be complete before the user receives a response, it belongs in the request cycle. Background jobs introduce eventual consistency -- if your use case cannot tolerate this, do not use async execution.

Edge Cases

Clock Skew in Distributed Schedulers

When multiple application instances run a cron scheduler simultaneously, clock differences of 1--2 seconds between hosts can cause the same scheduled job to be enqueued twice or skipped. The correct mitigation is a distributed lock with a deduplication window equal to the cron resolution (typically 60 seconds). Set the lock TTL to 55 seconds so it expires just before the next minute boundary. Do NOT rely on unique job constraints alone, because two instances can enqueue before either lock check completes. Sidekiq-Cron, GoodJob, and Solid Queue implement this with database-level locking. Custom schedulers must implement it explicitly with Redis SET [key] [instance_id] NX EX 55.

Thundering Herd After Queue Outage Recovery

When a queue broker (Redis, RabbitMQ) goes down and recovers, all enqueued jobs become visible simultaneously, and all retrying jobs retry at the same moment. This can overload downstream dependencies (databases, external APIs). Mitigate by: (1) using exponential backoff with jitter so retries are spread across a time window rather than aligned; (2) implementing circuit breakers in workers that open when downstream error rates exceed 50% in a 30-second window; (3) starting workers at reduced concurrency (25% of normal) after an outage and ramping up over 5--10 minutes. Monitor downstream error rates during queue drain and throttle if they exceed baseline.

Job Argument Deserialization After Code Deletion

If a job class is deleted or renamed in a deployment, jobs still in the queue for that class will fail to deserialize, generating a NameError or ImportError on every dequeue attempt. This can rapidly fill the DLQ and generate alert noise. The correct migration path: (1) stop enqueueing the job class; (2) wait for queue depth to reach zero; (3) then remove the class in a subsequent deployment. If the class must be removed urgently, create a tombstone class with the same name that simply logs and discards the job.

Memory Leaks in Long-Running Workers

Workers that process thousands of jobs without restarting accumulate memory from object retention, third-party library leaks, and connection pool fragmentation. Implement maximum job count per worker process (Sidekiq's max_jobs option, Celery's --max-tasks-per-child) to periodically recycle worker processes. Set this threshold based on memory profiling -- typically 500--2000 jobs depending on job complexity. Monitor worker memory RSS over time; if RSS grows linearly with jobs processed, there is a leak that recycling masks rather than fixes. Use a memory profiler (ruby-prof, memory_profiler for Python) to identify the root cause.

Transactional Outbox Pattern for Reliable Enqueue

When a job must be enqueued reliably only when a database transaction commits (and not if it rolls back), the standard after_commit hook is fragile -- if the application crashes between commit and enqueue, the job is silently lost. The transactional outbox pattern solves this: write the job payload to an outbox table within the same transaction as the business data. A separate polling process reads unprocessed outbox rows, enqueues them, and marks them processed. This guarantees at-least-once enqueue with no silent loss. PostgreSQL-backed queue solutions (pg-boss, GoodJob) can implement this natively because the queue table is in the same database. For Redis-backed queues, implement an explicit outbox table with a polling interval of 1--5 seconds.

High-Cardinality Job Classes Causing Metric Explosion

Instrumenting background jobs with a job_class label in Prometheus or Datadog is standard practice, but if job classes are dynamically named or parameterized (e.g., Import::Sheet123Job), the label cardinality explodes, causing metric storage to grow unbounded. This has crashed monitoring systems in production. Normalize job class names to a fixed set of labels. Use a mapping table that translates dynamic class names to canonical categories before emitting metrics. Alert if a new unmapped job class label appears, rather than allowing it to propagate to metrics.

Exactly-Once Semantics Across External Side Effects

When a job sends an email, charges a payment, or calls a webhook, idempotency at the job level requires coordination with the external system. Re-running a job due to a retry must not send a second email or double-charge a payment. The correct pattern: (1) generate a stable idempotency key before the side effect (email_confirmation_#{user_id}_#{signup_date}); (2) check a local record of completed side effects before executing; (3) pass the idempotency key to the external system (Stripe, SendGrid, and most payment processors support this natively); (4) record the completed side effect atomically with the business data write if possible. Do not rely on the external system's idempotency alone -- if the response is lost in transit, you will not know whether the operation succeeded and will be unable to determine whether to retry.

Worker Pool Exhaustion Under Slow Downstream Dependencies

When a downstream dependency (database, external API) slows from 50ms to 2000ms response time, workers that normally handle 20 jobs/second each now handle only 0.5 jobs/second, meaning 40x more workers are needed to maintain throughput. This causes worker pool exhaustion, and new jobs queue indefinitely. Mitigate with: (1) per-job execution timeouts that free workers when dependencies are slow; (2) circuit breakers that fast-fail when a dependency exceeds its error threshold, freeing workers to process jobs that do not use that dependency; (3) separate worker pools per dependency so a slow email API does not exhaust the worker pool for database jobs. Set circuit breaker thresholds at 20--30% error rate over a 30-second window, with a half-open probe every 60 seconds.


Example

Input: "We're building a SaaS B2B invoicing platform in Ruby on Rails with PostgreSQL and Redis. We need to handle: monthly invoice generation for all accounts (runs on the 1st of each month, ~50,000 accounts), real-time invoice PDF generation when a user clicks 'Download' (must complete in under 3 seconds from user perspective), sending invoice emails after generation, and syncing invoice data to QuickBooks via their API (rate-limited to 100 req/min). What background job architecture do you recommend?"


Background Job Architecture Assessment

Job Classification Summary

| Job Class | Type | Latency Class | Delivery Guarantee | Est. Volume/hr | Payload Size |

|---|---|---|---|---|---|

| Billing::MonthlyInvoiceGenerationJob | CPU + I/O | Batch (< 2hr total drain) | At-least-once + idempotent | 50,000 (burst, 1st of month) | < 1KB |

| Billing::InvoicePdfRenderJob | CPU | Interactive (< 3s p99) | At-least-once + idempotent | ~500 during business hours | < 2KB |

| Notifications::InvoiceEmailJob | I/O | Near-real-time (< 30s) | At-least-once + idempotent | ~50,000 (burst) + 500/hr steady | < 1KB |

| Integrations::QuickBooksSyncJob | I/O | Background (< 5min) | At-least-once + idempotent | 100/min maximum (rate-limited) | < 2KB |

Queue Backend Recommendation

Selected Backend: Redis (Sidekiq Pro) + PostgreSQL (GoodJob for scheduled coordination)

Rationale:

Rejected alternatives:

Queue Topology
critical (Sidekiq, concurrency: 10)
  └── Billing::InvoicePdfRenderJob (timeout: 30s, max_retries: 3)

default (Sidekiq, concurrency: 20)
  ├── Notifications::InvoiceEmailJob (timeout: 60s, max_retries: 15)
  └── Billing::MonthlyInvoiceGenerationJob (timeout: 120s, max_retries: 5)

integrations (Sidekiq, concurrency: 2)
  └── Integrations::QuickBooksSyncJob (timeout: 90s, max_retries: 25)

[each queue]_dlq (Sidekiq dead set, alert_threshold: 5 jobs)

Note: integrations queue is intentionally capped at concurrency 2 -- the rate limit on QuickBooks (100 req/min) means 2 workers each processing 1 job/~1.2s saturates the limit safely with headroom.

Retry and Error Policy

| Job Class | Max Retries | Backoff Strategy | Non-Retryable Errors | DLQ |

|---|---|---|---|---|

| InvoicePdfRenderJob | 3 | Exponential, 15s base | Invoice::NotFoundError, PDF::InvalidTemplateError | Yes |

| InvoiceEmailJob | 15 | Exponential, 15s base (max ~4hr spread) | Email::InvalidAddressError, Account::SuspendedError | Yes |

| MonthlyInvoiceGenerationJob | 5 | Exponential, 30s base | Account::ClosedError | Yes |

| QuickBooksSyncJob | 25 | Exponential, 30s base (max ~48hr spread) | QB::AuthRevoked, Invoice::SyncDisabledError | Yes |

Concurrency and Rate Limits

| Job Class | Max Concurrency | Rate Limit | Lock Strategy |

|---|---|---|---|

| InvoicePdfRenderJob | 10 (critical queue) | None | Redis NX per invoice_id, TTL: 35s |

| InvoiceEmailJob | 20 (default queue share) | None | Redis NX per invoice_id, TTL: 65s |

| MonthlyInvoiceGenerationJob | 20 (default queue share) | Batch: 1000 enqueues/sec max | Redis NX per account_id_billing_period, TTL: 125s |

| QuickBooksSyncJob | 2 (integrations queue) | 90 req/min (90% of limit) | Redis NX per invoice_id_sync_type, TTL: 95s |

Idempotency Design

| Job Class | Idempotency Key | Guard Implementation |

|---|---|---|

| InvoicePdfRenderJob | pdf_#{invoice_id} | Check invoices.pdf_generated_at IS NOT NULL before render |

| InvoiceEmailJob | email_invoice_#{invoice_id}_#{recipient_email} | DB unique index on invoice_notifications(invoice_id, email) |

| MonthlyInvoiceGenerationJob | monthly_invoice_#{account_id}_#{year}_#{month} | DB unique index on invoices(account_id, billing_period) |

| QuickBooksSyncJob | qb_sync_#{invoice_id} | QuickBooks idempotency key header + local qb_synced_at column |

Implementation Template
# app/jobs/billing/invoice_pdf_render_job.rb
class Billing::InvoicePdfRenderJob < ApplicationJob
  queue_as :critical

  # Retryable transient failures only
  retry_on PDF::RenderTimeoutError, Net::ReadTimeout, Redis::TimeoutError,
           wait: :exponentially_longer, attempts: 3

  # Permanent failures -- move to DLQ immediately
  discard_on Invoice::NotFoundError, PDF::InvalidTemplateError

  # Hard execution ceiling: 2x p99 render time (p99 observed: 12s)
  sidekiq_options timeout: 30

  def perform(invoice_id, correlation_id: nil)
    # Propagate trace context for APM
    Datadog::Tracing.trace('invoice.pdf_render', tags: { correlation_id: }) do
      
      # Idempotency guard: check before acquiring lock
      invoice = Invoice.find(invoice_id)  # raises Invoice::NotFoundError if missing
      return if invoice.pdf_generated_at.present?

      # Distributed lock: prevents duplicate renders under at-least-once delivery
      lock_key = "lock:pdf_render:#{invoice_id}"
      acquired = $redis.set(lock_key, 1, nx: true, ex: 35)
      return unless acquired

      begin
        # Fetch all data fresh -- never rely on serialized state
        account = invoice.account
        line_items = invoice.line_items.includes(:product)

        pdf_bytes = PdfRenderer.render(
          template: account.invoice_template,
          invoice:,
          line_items:
        )

        # Atomic: store PDF + mark generated in same transaction
        ActiveRecord::Base.transaction do
          invoice.update!(
            pdf_url: StorageService.upload(pdf_bytes, "invoices/#{invoice_id}.pdf"),
            pdf_generated_at: Time.current
          )
        end

        # Enqueue email AFTER commit (not inside the transaction above)
        Notifications::InvoiceEmailJob.perform_later(
          invoice_id,
          correlation_id:
        )

      ensure
        $redis.del(lock_key)
      end
    end
  end
end
# app/jobs/billing/monthly_invoice_fan_out_job.rb
# This is the cron-triggered entry point -- it fans out one job per account
class Billing::MonthlyInvoiceFanOutJob < ApplicationJob
  queue_as :default
  sidekiq_options timeout: 300  # Fan-out of 50k enqueues may take 2-3 minutes

  def perform(year:, month:)
    billing_period = "#{year}-#{month.to_s.rjust(2, '0')}"
    
    # Cursor-based batching: avoid loading 50k records into memory
    Account.active.in_batches(of: 500) do |batch|
      jobs = batch.map do |account|
        [
          'Billing::MonthlyInvoiceGenerationJob',
          [account.id, { billing_period:, correlation_id: SecureRandom.uuid }]
        ]
      end
      # Sidekiq bulk enqueue: one Redis round-trip per 500 jobs
      Sidekiq::Client.push_bulk('class' => jobs.first[0],
                                 'args'  => jobs.map(&:last),
                                 'queue' => 'default')
    end
  end
end
# config/schedule.yml (Sidekiq-Cron)
monthly_invoice_fan_out:
  cron: "0 2 1 * *"           # 2:00 AM on 1st of every month
  class: "Billing::MonthlyInvoiceFanOutJob"
  # Args passed as keyword arguments resolved at runtime
  args:
    year: <%= Date.today.year %>
    month: <%= Date.today.month %>
  description: >
    Fans out monthly invoice generation to all active accounts.
    Runs at 2AM to minimize overlap with peak PDF download traffic.
    Delayed by 2 months max before the accounting SLA is breached.
    Idempotent: duplicate enqueue within the same billing_period is a no-op.
Observability Checklist
Operational Runbook Notes

DLQ review process: All DLQ jobs are reviewed within 24 hours. MonthlyInvoiceGenerationJob DLQ items require manual verification that the invoice was not partially created before re-enqueueing. QuickBooksSyncJob DLQ items with QB::AuthRevoked require the account owner to re-authenticate QuickBooks before re-enqueueing -- do not retry automatically.

Worker scaling: Add Sidekiq processes (not threads) for CPU-bound PDF rendering. Threads are appropriate for I/O-bound email and sync jobs. Target integrations queue at exactly 2 concurrency permanently -- do not scale this up, as it will exceed the QuickBooks rate limit.

Monthly fan-out window: The 50,000-job fan-out drains in approximately 45--60 minutes at current concurrency. If the queue is not empty by 4:00 AM on the 1st, page on-call to investigate. The accounting cutoff requires all invoices generated by 6:00 AM.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.