Background Jobs, Queues and Scheduled Tasks

Move slow or unreliable work like emails, PDF generation and reminders out of the request into retryable background jobs.

Advanced · about 5 hours · difficulty 4/5

Learn first: Node.js Runtime Essentials, Relational Data Modelling in PostgreSQL

A request should return in well under a second. Anything slow or unreliable (sending emails, generating offer letter PDFs, calling a flaky third-party API) belongs in a background job.

The pattern

  1. The request handler validates input, writes to the database and enqueues a job.
  2. A separate worker process picks up the job and does the work.
  3. Failed jobs are retried with backoff, and permanently failed ones are kept for inspection.

Tools

  • BullMQ with Redis is the common choice for Node.js.
  • PostgreSQL-backed queues (pg-boss, Graphile Worker) avoid adding Redis.
  • Hosted options (Inngest, Trigger.dev, QStash) fit serverless apps.
  • Cron jobs or scheduled jobs handle periodic tasks like daily deadline reminders.

Making jobs safe

  • Jobs must be idempotent: running twice should not send two offer letters. Store a "sent" flag or use unique job ids.
  • Pass ids, not whole objects, and re-read fresh data inside the job.
  • Monitor queue length and failed jobs.

Pitfalls

  • Sending emails inline in the request, so a slow SMTP server slows every signup.
  • Fire-and-forget promises in serverless functions that get killed before finishing.
  • No retry limit, so a broken job loops forever.

You are ready to move on when your portal sends application emails and a daily deadline digest through a queue with retries, and a failed job is visible and replayable.

Key concepts

  • Why move work off the request
  • Queues and workers
  • Retries and backoff
  • Idempotent jobs
  • BullMQ with Redis
  • PostgreSQL-backed queues
  • Scheduled and cron jobs
  • Transactional email providers

Mini project

Add a job queue to your portal: send confirmation emails on application, generate a PDF summary for placement officers, and run a daily 8 am deadline reminder, all with retries and a simple failed-jobs view.

Check yourself

A worker crashes after sending an email but before marking the job complete. What happens on retry, and how do you prevent a duplicate email?

Free resources