Queues
Queues are the final aspect of Payload's Jobs Queue and deal with how to run your jobs. Up to this point, all we've covered is how to queue up jobs to run, but so far, we aren't actually running any jobs.
When you go to run jobs, Payload will query for any jobs that are added to the queue and then run them. By default, all queued jobs are added to the default queue.
But, imagine if you wanted to have some jobs that run nightly, and other jobs which should run every five minutes.
By specifying the queue name when you queue a new job using payload.jobs.queue(), you can queue certain jobs with queue: 'nightly', and other jobs can be left as the default queue.
Then, you could configure two different runner strategies:
- A
cronthat runs nightly, querying for jobs added to thenightlyqueue - Another that runs any jobs that were added to the
defaultqueue every ~5 minutes or so
Executing jobs
As mentioned above, you can queue jobs, but the jobs won't run unless a worker picks up your jobs and runs them. This can be done in four ways:
Bin script (Recommended for Dedicated Servers)
For dedicated servers, the recommended approach is to use Payload's bin script to run jobs. This creates a completely separate process from your Next.js server, making it easier to deploy, scale, and manage workers independently.
Benefits of using bin scripts:
- Separate process: Runs completely independently from your Next.js server, preventing any impact on API response times
- Easy deployment: Can be deployed as a separate service/container, making it simple to scale workers independently
- Simple monitoring: Each worker process can be monitored, restarted, and scaled independently
- No Next.js overhead: Workers don't load the entire Next.js application, making them lighter and faster
Deployment example:
This makes it easy to run multiple workers for different queues, scale them independently, and deploy them to different servers if needed.
autoRun (Alternative for Dedicated Servers)
The jobs.autoRun property allows you to configure cron jobs that automatically execute jobs from your queue at specified intervals. This runs within your Next.js process.
Think of it this way:
- Queuing = Adding jobs to the database to be run later
- Running = Actually executing the job handler functions
autoRun only handles the running part.
Example:
In this example:
processPaymentjobs are queued manually (e.g., from API endpoints usingpayload.jobs.queue())generateReportjobs are auto-queued at midnight via thescheduleproperty- The first
autoRunentry processes manually-queued jobs from the 'default' queue every 5 minutes - The second
autoRunentry processes scheduled jobs from the 'nightly' queue every minute
Endpoint
You can execute jobs by making a fetch request to the /api/payload-jobs/run endpoint:
This endpoint is automatically mounted for you and is helpful in conjunction with serverless platforms like Vercel, where you might want to use Vercel Cron to invoke a serverless function that executes your jobs.
Query Parameters
limit: The maximum number of jobs to run in this invocation (default: 10).queue: The name of the queue to run jobs from. If not specified, jobs will be run from thedefaultqueue.allQueues: If set totrue, all jobs from all queues will be run. This will ignore thequeueparameter.
Vercel Cron Example
If you're deploying on Vercel, you can add a vercel.json file in the root of your project that configures Vercel Cron to invoke the run endpoint on a cron schedule.
Here's an example of what this file will look like:
The configuration above schedules the endpoint /api/payload-jobs/run to be invoked every 5 minutes.
The last step will be to secure your run endpoint so that only the proper users can invoke the runner.
To do this, you can set an environment variable on your Vercel project called CRON_SECRET, which should be a random string—ideally 16 characters or longer.
Then, you can modify the access function for running jobs by ensuring that only Vercel can invoke your runner.
This works because Vercel automatically makes the CRON_SECRET environment variable available to the endpoint as the Authorization header when triggered by the Vercel Cron, ensuring that the jobs can be run securely.
After the project is deployed to Vercel, the Vercel Cron job will automatically trigger the /api/payload-jobs/run endpoint in the specified schedule, running the queued jobs in the background.
Local API (For Programmatic Control)
If you want to process jobs programmatically from your server-side code, you can use the Local API:
Run all jobs:
Run a single job:
Processing Order
By default, jobs are processed first in, first out (FIFO). This means that the first job added to the queue will be the first one processed. However, you can also configure the order in which jobs are processed.
Jobs Configuration
You can configure the order in which jobs are processed in the jobs configuration by passing the processingOrder property. This mimics the Payload sort property that's used for functionality such as payload.find().
You can also set this on a queue-by-queue basis:
If you need even more control over the processing order, you can pass a function that returns the processing order - this function will be called every time a queue starts processing jobs.
Local API
You can configure the order in which jobs are processed in the payload.jobs.queue method by passing the processingOrder property.
Common Queue Strategies
Here are typical patterns for organizing your queues:
Priority-Based Queues
Separate jobs by priority to ensure critical tasks run quickly:
Then queue jobs to appropriate queues:
Environment-Based Execution
Only run jobs on specific servers:
Use cases:
- Dedicate specific servers to job processing
- Disable job processing during deployments
- Scale job workers independently from API servers
Feature-Based Queues
Group jobs by feature or domain:
This makes it easy to:
- Monitor specific features
- Scale individual features independently
- Pause/resume specific types of work
Choosing an Execution Method
Here's a quick guide to help you choose:
Method | Best For | Pros | Cons |
|---|---|---|---|
Bin script | Dedicated servers (Recommended) | Separate process, easy to deploy/scale | Requires dedicated server |
autoRun | Dedicated servers (Alternative) | Simple setup, automatic execution | Runs in Next.js process |
Endpoint | Serverless platforms (Vercel, Netlify) | Works with serverless, easy to trigger | Requires external cron (Vercel Cron, etc.) |
Local API | Custom scheduling, testing | Full control, good for tests | Must implement your own scheduling |
Recommendations:
- Production (Dedicated Server): Use Bin script with
--cronflag - Production (Serverless): Use Endpoint + Vercel Cron
- Development: Use Bin script or Local API
- Testing: Use Local API with
payload.jobs.runByID()
Troubleshooting
Jobs aren't running (Most Common Issues)
If you configured autoRun but jobs aren't executing, check these common causes in order:
1. You only configured autoRun without queuing any jobs
Symptom: autoRun is configured but no jobs ever execute.
Diagnosis: autoRun runs jobs that are already in the queue. If nothing is adding jobs to the queue, there's nothing to run.
Solution: Queue jobs either:
- Manually via
payload.jobs.queue()in your code (e.g., in hooks, endpoints), OR - Automatically via the
scheduleproperty on tasks/workflows
Example of the problem:
Solution:
2. Queue name mismatch
Symptom: Jobs appear in the payload-jobs collection but never execute.
Diagnosis: The queue name in your task's schedule doesn't match the queue name in autoRun.
Example of the problem:
Solution: Make sure queue names match exactly:
3. You're on a serverless platform (Vercel, Netlify, etc.)
Symptom: Jobs work locally but don't run in production on serverless platforms.
Diagnosis: autoRun requires a long-running server and won't work on serverless platforms where instances shut down between requests.
Solution: Use the endpoint method with Vercel Cron instead of autoRun.
4. Jobs are scheduled for the future
Symptom: Jobs exist in the payload-jobs collection with completedAt: null but aren't running.
Diagnosis: Jobs may have waitUntil set to a future date - they won't run until that time passes.
Solution: Check the payload-jobs collection:
5. Jobs stopped after hot module reload (development)
Symptom: Jobs work initially but stop running after you make code changes in development.
Diagnosis: Hot Module Reload (HMR) in Next.js disrupts cron schedules. This is expected behavior in development.
Solution: Restart your dev server after making changes to job configurations. This is not an issue in production.
Is shouldAutoRun returning true?
Is autoRun configured correctly?
Are jobs in the correct queue?
Check the jobs collection
Enable the jobs collection in admin:
Look for jobs with:
processing: truebut stuck → Worker may have crashedhasError: true→ Check thelogfield for errorscompletedAt: null→ Job hasn't run yet
Jobs running but failing
Check the job logs in the payload-jobs collection:
Jobs running too slowly
Increase limit
Run more frequently
Add more workers
Scale horizontally by running multiple servers with ENABLE_JOB_WORKERS=true.
Was this page helpful?