Background Jobs
How background job processing is handled using BullMQ and Redis.
❗ Important: Using Redis and the BullMQ background job system is entirely optional. The application is designed to function without it. Enable it only if you need to offload tasks to a background worker.
This document outlines how background job processing is handled using BullMQ and Redis.
Overview
If enabled, the application utilizes BullMQ, a robust Node.js library built on top of Redis, to manage background jobs. This allows deferring time-consuming or non-critical tasks (like sending emails, processing images, generating reports) to a separate process, preventing them from blocking the main application thread and improving responsiveness.
Getting Started with Redis
Setting up Redis is required if you enable background jobs:
- Local Development: Tools like DBngin provide a simple graphical interface to set up and manage a local Redis instance quickly.
- Hosted Platforms: Most modern hosting platforms (e.g., Railway, Render, Coolify, Vercel KV, Upstash) offer easy ways to provision a managed Redis database that you can connect your application to.
Initiation
The background job worker is started from apps/web/instrumentation.ts during server initialization (process.env.NEXT_RUNTIME === "nodejs").
- Enable Flag:
NEXT_PUBLIC_ENABLE_BACKGROUND_JOBSinpackages/envschemas must betrue. - Redis Configuration: Additionally, valid Redis connection details (
REDIS_HOST,REDIS_PORT) must be provided in the environment variables. - Conditional Start:
apps/web/instrumentation.tschecks the enable flag and Redis config, then callsstartWorkerfromapps/web/src/server/instrumentation/bull/worker.ts.
Implementation
- Worker Logic:
apps/web/src/server/instrumentation/bull/worker.tssets up the BullMQWorker. - Queue Management: Queues in
apps/web/src/server/instrumentation/bull/(e.g.queues.ts). - Adding Jobs: Other parts of the application (e.g., API routes, server actions) can add jobs to the queue (using BullMQ's
Queueclass) to be picked up and processed by the worker. - Usage Examples: Currently, there are no specific examples within this boilerplate demonstrating how to add jobs to the queue. You would typically import a queue instance and call its
.add()method.
Configuration
- Enable/Disable: Set
NEXT_PUBLIC_ENABLE_BACKGROUND_JOBStotrueorfalsein your.envfile (or environment variables) to enable or disable the background job system. - Redis Connection: If enabled (i.e.,
NEXT_PUBLIC_ENABLE_BACKGROUND_JOBSistrue), ensure Redis connection details (REDIS_HOST,REDIS_PORT, etc.) are correctly configured in your environment variables for BullMQ to connect.