Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

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:

  1. A cron that runs nightly, querying for jobs added to the nightly queue
  2. Another that runs any jobs that were added to the default queue 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.

1
# Basic usage - run jobs from default queue
2
pnpm payload jobs:run
3
4
# Run with custom queue and limit
5
pnpm payload jobs:run --queue myQueue --limit 15
6
7
# Run on a cron schedule (recommended for production)
8
pnpm payload jobs:run --cron "*/5 * * * *" --queue myQueue
9
10
# Run and also handle schedules (both queuing and running)
11
pnpm payload jobs:run --cron "*/5 * * * *" --queue myQueue --handle-schedules
12
13
# Run all jobs from all queues
14
pnpm payload jobs:run --all-queues

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:

1
# docker-compose.yml or similar
2
services:
3
nextjs:
4
# Your main Next.js app
5
command: pnpm start
6
7
worker-default:
8
# Worker for default queue
9
command: pnpm payload jobs:run --cron "*/5 * * * *" --queue default
10
11
worker-nightly:
12
# Worker for nightly queue
13
command: pnpm payload jobs:run --cron "* * * * *" --queue nightly --handle-schedules

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:

1
export default buildConfig({
2
// Other configurations...
3
jobs: {
4
tasks: [
5
{
6
slug: 'processPayment',
7
handler: async ({ input }) => {
8
// Payment processing logic
9
return { output: { success: true } }
10
},
11
},
12
{
13
slug: 'generateReport',
14
// This task auto-queues itself daily at midnight
15
schedule: [
16
{
17
cron: '0 0 * * *',
18
queue: 'nightly',
19
},
20
],
21
handler: async () => {
22
// Report generation logic
23
return { output: { reportId: '123' } }
24
},
25
},
26
],
27
28
// autoRun processes queued jobs from specified queues
29
// autoRun can optionally be a function that receives `payload` as an argument
30
autoRun: [
31
{
32
cron: '*/5 * * * *', // Check every 5 minutes
33
queue: 'default', // Process 'default' queue (for manually queued jobs)
34
limit: 50,
35
},
36
{
37
cron: '* * * * *', // Check every minute
38
queue: 'nightly', // Process 'nightly' queue (for scheduled jobs)
39
limit: 100,
40
},
41
],
42
43
shouldAutoRun: async (payload) => {
44
// Tell Payload if it should run jobs or not. This function is optional and will return true by default.
45
// This function will be invoked each time Payload goes to pick up and run jobs.
46
// If this function ever returns false, the cron schedule will be stopped.
47
return true
48
},
49
},
50
})

In this example:

  • processPayment jobs are queued manually (e.g., from API endpoints using payload.jobs.queue())
  • generateReport jobs are auto-queued at midnight via the schedule property
  • The first autoRun entry processes manually-queued jobs from the 'default' queue every 5 minutes
  • The second autoRun entry 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:

1
// Here, we're saying we want to run only 100 jobs for this invocation
2
// and we want to pull jobs from the `nightly` queue:
3
await fetch('/api/payload-jobs/run?limit=100&queue=nightly', {
4
method: 'GET',
5
headers: {
6
Authorization: `Bearer ${token}`,
7
},
8
})

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 the default queue.
  • allQueues: If set to true, all jobs from all queues will be run. This will ignore the queue parameter.

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:

1
{
2
"crons": [
3
{
4
"path": "/api/payload-jobs/run",
5
"schedule": "*/5 * * * *"
6
}
7
]
8
}

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.

1
export default buildConfig({
2
// Other configurations...
3
jobs: {
4
access: {
5
run: ({ req }: { req: PayloadRequest }): boolean => {
6
// Allow logged in users to execute this endpoint (default)
7
if (req.user) return true
8
9
const secret = process.env.CRON_SECRET
10
if (!secret) return false
11
12
// If there is no logged in user, then check
13
// for the Vercel Cron secret to be present as an
14
// Authorization header:
15
const authHeader = req.headers.get('authorization')
16
return authHeader === `Bearer ${secret}`
17
},
18
},
19
// Other job configurations...
20
},
21
})

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:

1
// Run all jobs from the `default` queue - default limit is 10
2
const results = await payload.jobs.run()
3
4
// You can customize the queue name and limit by passing them as arguments:
5
await payload.jobs.run({ queue: 'nightly', limit: 100 })
6
7
// Run all jobs from all queues:
8
await payload.jobs.run({ allQueues: true })
9
10
// You can provide a where clause to filter the jobs that should be run:
11
await payload.jobs.run({
12
where: { 'input.message': { equals: 'secret' } },
13
})

Run a single job:

1
const results = await payload.jobs.runByID({
2
id: myJobID,
3
})

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().

1
export default buildConfig({
2
// Other configurations...
3
jobs: {
4
tasks: [
5
// your tasks here
6
],
7
processingOrder: '-createdAt', // Process jobs in reverse order of creation = LIFO
8
},
9
})

You can also set this on a queue-by-queue basis:

1
export default buildConfig({
2
// Other configurations...
3
jobs: {
4
tasks: [
5
// your tasks here
6
],
7
processingOrder: {
8
default: 'createdAt', // FIFO
9
queues: {
10
nightly: '-createdAt', // LIFO
11
myQueue: '-createdAt', // LIFO
12
},
13
},
14
},
15
})

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.

1
export default buildConfig({
2
// Other configurations...
3
jobs: {
4
tasks: [
5
// your tasks here
6
],
7
processingOrder: ({ queue }) => {
8
if (queue === 'myQueue') {
9
return '-createdAt' // LIFO
10
}
11
return 'createdAt' // FIFO
12
},
13
},
14
})

Local API

You can configure the order in which jobs are processed in the payload.jobs.queue method by passing the processingOrder property.

1
const createdJob = await payload.jobs.queue({
2
workflow: 'createPostAndUpdate',
3
input: {
4
title: 'my title',
5
},
6
processingOrder: '-createdAt', // Process jobs in reverse order of creation = LIFO
7
})

Common Queue Strategies

Here are typical patterns for organizing your queues:

Priority-Based Queues

Separate jobs by priority to ensure critical tasks run quickly:

1
export default buildConfig({
2
jobs: {
3
tasks: [
4
/* ... */
5
],
6
autoRun: [
7
{
8
cron: '* * * * *', // Every minute
9
limit: 100,
10
queue: 'critical',
11
},
12
{
13
cron: '*/5 * * * *', // Every 5 minutes
14
limit: 50,
15
queue: 'default',
16
},
17
{
18
cron: '0 2 * * *', // Daily at 2 AM
19
limit: 1000,
20
queue: 'batch',
21
},
22
],
23
},
24
})

Then queue jobs to appropriate queues:

1
// Critical: Password resets, payment confirmations
2
await payload.jobs.queue({
3
task: 'sendPasswordReset',
4
input: { userId: '123' },
5
queue: 'critical',
6
})
7
8
// Default: Welcome emails, notifications
9
await payload.jobs.queue({
10
task: 'sendWelcomeEmail',
11
input: { userId: '123' },
12
queue: 'default',
13
})
14
15
// Batch: Analytics, reports, cleanups
16
await payload.jobs.queue({
17
task: 'generateAnalytics',
18
input: { date: new Date() },
19
queue: 'batch',
20
})

Environment-Based Execution

Only run jobs on specific servers:

1
export default buildConfig({
2
jobs: {
3
tasks: [
4
/* ... */
5
],
6
shouldAutoRun: async (payload) => {
7
// Only run jobs if this env var is set
8
return process.env.ENABLE_JOB_WORKERS === 'true'
9
},
10
autoRun: [
11
{
12
cron: '*/5 * * * *',
13
limit: 50,
14
queue: 'default',
15
},
16
],
17
},
18
})

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:

1
autoRun: [
2
{ cron: '*/2 * * * *', queue: 'emails', limit: 100 },
3
{ cron: '*/10 * * * *', queue: 'images', limit: 50 },
4
{ cron: '0 * * * *', queue: 'analytics', limit: 1000 },
5
]

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 --cron flag
  • 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 schedule property on tasks/workflows

Example of the problem:

1
// This alone won't do anything - no jobs are being queued!
2
jobs: {
3
tasks: [{ slug: 'myTask', handler: async () => {} }],
4
autoRun: [{ cron: '* * * * *', queue: 'default' }],
5
}

Solution:

1
// Option A: Queue manually in your code
2
await payload.jobs.queue({ task: 'myTask' })
3
4
// Option B: Add schedule property to auto-queue
5
jobs: {
6
tasks: [
7
{
8
slug: 'myTask',
9
schedule: [{ cron: '0 8 * * *', queue: 'default' }], // Now jobs will be queued
10
handler: async () => {},
11
},
12
]
13
}

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:

1
// Task queues to 'reports'
2
schedule: [{ cron: '0 8 * * *', queue: 'reports' }]
3
4
// But autoRun processes 'default'
5
autoRun: [{ queue: 'default' }] // Won't pick up the job!

Solution: Make sure queue names match exactly:

1
// Task queues to 'reports'
2
schedule: [{ cron: '0 8 * * *', queue: 'reports' }]
3
4
// autoRun also processes 'reports'
5
autoRun: [{ cron: '* * * * *', queue: 'reports' }] // Now they match!

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:

1
const jobs = await payload.find({
2
collection: 'payload-jobs',
3
where: {
4
completedAt: { exists: false },
5
},
6
})
7
8
console.log(jobs.docs[0].waitUntil) // Check if this is in the future

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?

1
jobs: {
2
shouldAutoRun: async (payload) => {
3
console.log('shouldAutoRun called') // Add logging
4
return true
5
},
6
}

Is autoRun configured correctly?

1
// invalid cron syntax
2
autoRun: [{ cron: 'every 5 minutes' }]
3
4
// valid cron syntax
5
autoRun: [{ cron: '*/5 * * * *' }]

Are jobs in the correct queue?

1
// Queuing to 'critical' queue
2
await payload.jobs.queue({ task: 'myTask', queue: 'critical' })
3
4
// But autoRun only processes 'default' queue
5
autoRun: [{ queue: 'default' }] // won't pick up the job

Check the jobs collection

Enable the jobs collection in admin:

1
jobsCollectionOverrides: ({ defaultJobsCollection }) => ({
2
...defaultJobsCollection,
3
admin: {
4
...defaultJobsCollection.admin,
5
hidden: false,
6
},
7
})

Look for jobs with:

  • processing: true but stuck → Worker may have crashed
  • hasError: true → Check the log field for errors
  • completedAt: null → Job hasn't run yet

Jobs running but failing

Check the job logs in the payload-jobs collection:

1
const job = await payload.findByID({
2
collection: 'payload-jobs',
3
id: jobId,
4
})
5
6
console.log(job.log) // View execution log
7
console.log(job.processingErrors) // View errors

Jobs running too slowly

Increase limit

1
autoRun: [
2
{ cron: '*/5 * * * *', limit: 100 }, // Process more jobs per run
3
]

Run more frequently

1
autoRun: [
2
{ cron: '* * * * *', limit: 50 }, // Run every minute instead of every 5
3
]

Add more workers

Scale horizontally by running multiple servers with ENABLE_JOB_WORKERS=true.

Was this page helpful?

Next

Job Schedules