Background jobs

This guide will walk you through creating background jobs with retries in a few minutes.

By running background tasks in Inngest:

  • You don't need to create queues, workers, or subscriptions.
  • You can run background jobs on serverless functions without setting up infrastructure.
  • You can enqueue jobs to run in the future, similar to a task queue, without any configuration.

How to create background jobs

Background jobs in Inngest are executed in response to a trigger (an event or cron).

The example below shows a background job that uses an event (here called app/user.created) to send an email to new signups. It consists of two parts: creating the function that runs in the background and triggering the function.

1. Create a function that runs in the background

Let's walk through the code step by step:

  1. We create a new Inngest function, which will run in the background any time the app/user.created event is sent to Inngest.
  2. We send an email reliably using the step.run() method. Every Inngest step is automatically retried upon failure.
  3. We pause the execution of the function until a specific date using step.sleepUntil(). The function will be resumed automatically, across server restarts or serverless functions. You don't have to worry about scale, memory leaks, connections, or restarts.
  4. We resume execution and perform other tasks.
import { Inngest } from "inngest";
const inngest = new Inngest({ id: "signup-flow" });

export const sendSignUpEmail = inngest.createFunction(
  { id: "send-signup-email" },
  { event: "app/user.created" },
  ({ event, step }) => {
    await step.run("send-the-user-a-signup-email", async () => {
      await sesclient.clientsendEmail({
        to: event.data.user_email,
        subject: "Welcome to Inngest!"
        message: "...",
      });
    });
    await step.sleepUntil("wait-for-the-future", "2023-02-01T16:30:00");

    await step.run("do-some-work-in-the-future", async () => {
      // Code here runs in the future automatically.
    });
  }
);

2. Trigger the function

Your sendSignUpEmail function will be triggered whenever Inngest receives an event called app/user.created. is received. You send this event to Inngest like so:

await inngest.send({
  name: "app/user.created", // This matches the event used in `createFunction`
  data: {
    email: "test@example.com",
    // any data you want to send
  },
});

When you send an event to Inngest, it automatically finds any functions that are triggered by the event ID and automatically runs those functions in the background. The entire JSON object you pass in to inngest.send() will be available to your functions.

💡 Tip: You can create many functions which listen to the same event, and all of them will run in the background. Learn more about this pattern in our "Fan out" guide.

Further reading

More information on background jobs: