Background jobs

This guide will walk you through creating background jobs with retries in a few minutes. Background tasks in Inngest are easy because:

  • 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 config.

How to create background jobs

There are two parts to background jobs:

  1. Creating the function that runs in the background
  2. Triggering the function

Let's get started on a background job that sends an email to new signups. This code creates a new Inngest function as outlined in the function reference:

import { Inngest } from "inngest";

const inngest = new Inngest({ id: "signup-flow" });

export const fn = inngest.createFunction(
  { id: "send-signup-email" },
  { event: "user/created" }, // Trigger
  ({ event, step }) => {

    // This runs in the background any time the `user/created` event is
    // sent to Inngest.
    //
    // Whatever event data you send is accessible within `event`.

    await step.run("send-the-user-a-signup-email", async () => {
      // Reliably send an email.  Anything in `step.run` automatically retries
      await sesclient.clientsendEmail({
        to: event.data.user_email,
        subject: "Welcome to Inngest!"
        message: "...",
      });
    });

    // You can schedule work for the future by sleeping within functions.
    // NOTE: This actually *****stops***** functions and continues execution automatically,
    // across server restarts or serverless functions.  You don't have to worry about
    // scale, memory leaks, connections, or restarts.
    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.
    });
  }
);

You can trigger this background function by sending an event to Inngest:

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

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

Further reading

Now that you've created a background function you might want to learn how to: