Decorative background gradient
Back to Blog
Trigger.dev TutorialNextjs Background JobsServerless Task Queue Nodejs

Background Jobs & Workflows in Next.js with Trigger.dev v3

Run long tasks, retries, scheduled CRON jobs, and webhook processing in Next.js using Trigger.dev v3 — without timing out serverless functions.

Background Jobs & Workflows in Next.js with Trigger.dev v3

Serverless functions are great for request-response work and terrible for anything that takes more than a few seconds — video processing, bulk imports, multi-step AI pipelines, or scheduled reports. Trigger.dev v3 gives Next.js apps a proper background job runtime: long-running tasks, automatic retries, CRON scheduling, and webhook-safe processing, without you managing a queue or worker infrastructure yourself.

Step 1: Install and Configure Trigger.dev

bash

This scaffolds a trigger/ directory and a trigger.config.ts file, and links your project to a Trigger.dev instance.

ts

Step 2: Define a Long-Running Task

ts

This task can run for minutes without hitting any serverless timeout, because it executes on Trigger.dev's infrastructure, not inside your Next.js deployment.

Step 3: Trigger the Task from a Server Action

tsx

The Server Action returns immediately with a runId — the user's request isn't blocked waiting for transcoding to finish. You can poll or subscribe to that run's status separately if you need to show progress in the UI.

Step 4: Add Scheduled CRON Jobs

ts

No separate cron service, no always-on server, and no reliance on a serverless platform's own (often unreliable) scheduled function trigger — Trigger.dev's infrastructure fires this task on the defined schedule.

Step 5: Process Webhooks Safely

Most webhook providers retry aggressively if your endpoint doesn't respond within a few seconds — so validate and hand off immediately, then do the real work in a task.

ts
ts

The webhook handler's only job is signature verification and enqueueing — it responds in milliseconds regardless of how long fulfillOrder actually takes.

Step 6: Chain Multi-Step Workflows

ts

Each step runs sequentially with the same retry semantics as any other task — if sendWelcomeEmail fails transiently, only that step retries, not the whole onboarding flow from scratch (when using triggerAndWait for sub-tasks instead of inline calls).

Key Takeaways

Trigger.dev tasks run outside your Next.js deployment's execution limits, so long tasks like transcoding or bulk sync never risk a serverless timeout. Trigger tasks from Server Actions with tasks.trigger() and return immediately, lean on built-in retry.maxAttempts instead of hand-rolled retry loops, use schedules.task() for CRON work, and always acknowledge webhooks fast by enqueueing a task rather than processing inline.

Frequently Asked Questions

Why not just use a Vercel serverless function for background jobs?

Serverless functions have hard execution time limits (10-60s on most plans) and are meant for request-response work, not long tasks like video processing or bulk data sync. Trigger.dev runs tasks on dedicated infrastructure with no such limit, and gives you retries, observability, and queuing that a raw serverless function doesn't.

How do retries work in Trigger.dev?

Set retry.maxAttempts (and optionally minTimeoutInMs/maxTimeoutInMs) on a task definition. If the task throws, Trigger.dev automatically re-runs it with exponential backoff up to the configured attempt count, without you writing retry-loop code.

Can Trigger.dev replace a traditional CRON job?

Yes. schedules.task() lets you attach a CRON expression directly to a task definition, and Trigger.dev's infrastructure triggers it on that schedule — no separate cron service, serverless cron trigger, or always-on server process required.

How should I handle incoming webhooks with Trigger.dev?

Validate the webhook signature in your Route Handler, then immediately call tasks.trigger() to hand the payload off to a background task and return a 200 response right away. Do the actual processing inside the task — most webhook providers retry aggressively if your endpoint doesn't respond within a few seconds.

Working on something similar? Take a look at my services and case studies, or book a free call to talk about your idea.

Related Articles

Caching Strategies and Cache Invalidation — The Complete Guide
CachingSystem DesignPerformance

Caching Strategies and Cache Invalidation — The Complete Guide

A practical guide to caching strategies (cache-aside, write-through, write-behind) and the cache invalidation techniques that keep them from serving stale data.

September 8, 2026Read more →
Database Indexing and Read Replicas — A Practical Guide
DatabasePostgreSQLSystem Design

Database Indexing and Read Replicas — A Practical Guide

How to choose the right database indexes, avoid the ones that quietly hurt write performance, and scale reads with replicas without introducing replication lag bugs.

September 8, 2026Read more →
Load Balancing and Stateless Service Design — A Practical Guide
System DesignLoad BalancingScalability

Load Balancing and Stateless Service Design — A Practical Guide

How load balancers distribute traffic, why stateless services are what actually makes horizontal scaling work, and how to fix the sticky-session traps that quietly reintroduce state.

September 8, 2026Read more →
Session Stores and Database Connection Pooling Explained
BackendRedisDatabase

Session Stores and Database Connection Pooling Explained

Why in-memory sessions break horizontally scaled apps, how to move session state to Redis correctly, and how connection pooling keeps your database from falling over under concurrent load.

September 8, 2026Read more →
Vertical vs Horizontal Scaling: How to Choose and Implement Each
System DesignScalabilityArchitecture

Vertical vs Horizontal Scaling: How to Choose and Implement Each

A practical comparison of vertical and horizontal scaling — what each actually fixes, where each breaks down, and the architecture changes horizontal scaling requires that most guides skip.

September 8, 2026Read more →
AI Agent Guardrails and Safety — Preventing Prompt Injection and Runaway Actions
Ai Agent SafetyPrompt Injection DefenseAgent Guardrails

AI Agent Guardrails and Safety — Preventing Prompt Injection and Runaway Actions

Build practical guardrails for AI agents — prompt injection defenses, destructive-action confirmation, iteration caps, and permission scoping.

September 7, 2026Read more →

Trending Topics