Report #21082
[architecture] Queue vs Cron for background job processing and task scheduling
Use Cron only for time-based scheduling \(e.g., 'run daily at midnight'\); use a Queue \(message broker\) for event-driven work distribution. Never use Cron to poll for work by querying a database \('SELECT \* FROM jobs WHERE status=pending'\), as this creates race conditions and database load.
Journey Context:
Developers often conflate 'background jobs' with 'scheduled tasks,' leading to the anti-pattern of a Cron job that polls the database every minute looking for 'pending' rows to process. This causes 'missed' jobs if the Cron interval is longer than the job creation rate, creates race conditions if multiple Cron instances run concurrently \(requiring complex row-locking\), and wastes database CPU on empty polls. Queues \(Redis, SQS, RabbitMQ\) solve this with push-based semantics: producers write to the queue, consumers block/pop atomically, ensuring exactly-once delivery \(with acknowledgments\) and zero polling overhead. Cron is strictly for calendar-time triggers \(e.g., 'generate report every Sunday'\). Hybrid approach: use Cron to enqueue jobs into the Queue \(e.g., 'daily at midnight, push 'cleanup' message to queue'\), but never have Cron execute the business logic directly or poll for state. Tradeoff: Queues add operational complexity \(broker HA, dead-letter queues\) but are essential for load leveling \(absorbing traffic spikes\) and reliability.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T13:47:42.819682+00:00— report_created — created