Building Reliable Background Tasks in Next.js with DBOS

It’s hard to build a Next.js app that reliably handles tasks that run longer than HTTP request timeouts. Use cases like these include:

  • Processing large files in the background
  • Handling webhook events in order
  • Sending batches of emails on a schedule

The standard solution is to set up a separate service running a queueing system like BullMQ. This works, but it means deploying and maintaining additional infrastructure alongside your Next.js app.

DBOS offers a different approach. It’s an open-source TypeScript library that helps you durably execute long-running tasks directly in your Next.js app. It works by storing execution state in your database and if necessary, retrieving that data to resume execution after a failure. This ensures your tasks complete reliably, even if users disconnect, servers restart, or other interruptions occur.

How to Build a Durable Next.js App

Let’s build a simple Next.js app that runs a reliable background task. The app has two buttons: one that launches a background task and one that crashes the app. We’ll show that no matter how many times you crash the app, upon restart the background task will continue from where it left off.

Building durable Next.js workflows with DBOS

Here’s the code for the background task. Each task runs ten steps, and each step sleeps for two seconds. The magic here is in the decorators: @DBOS.workflow() and @DBOS.step(). 

Durable Next.js workflow execution code

The decorators store the state of your program–what background tasks are running and how many steps they’ve completed–in a database:

Durable Next.js workflow execution diagram

This acts as a “save point” in your app. If it fails at any point, the background task can be restarted and picks up from the last stored checkpoint.

‍

Video game screenshot of a character at a Save Point, with a message to access the menu and select SAVE to save the game

Because the background task is just a normal TypeScript function, you can start it from a Next.js server action. Here’s the code for the app’s server actions. The first action starts a background task, the second looks up the progress of a background task to display on the screen, and the third crashes the app–for demonstration purposes only 🙂.

Durable Next.js execution code example

You can call these server actions from buttons in your frontend code:

Front-end code for durable Next.js example

Run the Durable Next.js Example App Yourself

To try out this app yourself, initialize it from a template:

npx @dbos-inc/create -t dbos-nextjs-starter

Launch the development server:

npm run dev

Open localhost:3000 to see the app in action. Try launching a background task and crashing the app – you'll see the task resume exactly where it left off when you restart.

This example shows how to handle long-running tasks, but DBOS can help with other reliability challenges too:

‍

Insights

Recent articles

The latest in durable execution, AI workflows & more.

Product news
Oct 29, 2026

What's New in DBOS - October 2026

Workflow rewind, safer distributed execution, DBOS Transact Enterprise, and scaling to 1M+ workflows per second on sharded Postgres!
Qian Li
How To
Oct 9, 2026

‍Applications are Dead. Long Live Workflows

Applications should be replaced by workflows as the unit of middleware management
Mike Stonebraker
Product news
Sep 29, 2026

What's New in DBOS - September 2026

DBOS for Rust, autoscaling control, faster data retention, new integrations, and more!
Qian Li