Skip to main content

Background work

Choose between post-response work and durable background jobs.

Some work doesn't need to block the HTTP response. Easel supports framework APIs, such as after() and waitUntil, that keep work running inside the same invocation after the response is sent. Post-response work isn't a durable job queue.

Post-response work

In Next.js, use after():

import { after } from "next/server";

export async function POST(request: Request) {
  const event = await request.json();

  after(async () => {
    await recordAnalyticsEvent(event);
  });

  return Response.json({ accepted: true });
}

The client can receive the response before the callback completes, but the function stays active and the callback counts toward the route's maximum duration.

Appropriate uses

Post-response work is appropriate for short, best-effort tasks such as:

  • Recording non-critical analytics
  • Updating a secondary cache
  • Sending a lightweight notification
  • Completing a bounded cleanup step

Use a durable job system when work

  • Must survive the invocation ending
  • Requires automatic retries
  • May take longer than the route's maximum duration
  • Must run independently of the originating request
  • Requires scheduling, fan-out, or concurrency control
  • Must guarantee eventual completion

A durable pattern is:

HTTP request
  ↓
Validate and persist job
  ↓
Return 202 Accepted
  ↓
Worker processes job with retries

Failure behavior

A successful HTTP response doesn't prove that post-response work completed. Post-response work shares the function's duration, memory, CPU, and shutdown boundaries, so it can be cut off when the invocation ends; see Function limits. Log failures, and make non-idempotent operations safe to retry.