# Ensure exclusive execution of a function
Source: https://www.inngest.com/docs/reference/functions/singleton

Ensure that only a single run of a function (_or a set of specific functions, based on specific event properties_) is running at a time.

See the [Singleton Functions guide](/docs/guides/singleton) for more information about how this feature works.

```ts
export default inngest.createFunction(
  {
    id: "data-sync",
    singleton: {
      key: "event.data.user_id",
      mode: "skip",
    },
  },
  { event: "data-sync.start" },
  async ({ event }) => {
    // This function will be skipped if another run of the same function is already running for the same user
  }
);
```

## Configuration

<Properties>
  <Property name="singleton" type="object">
    Options to configure exclusive execution of a function.

    <Properties nested={true}>
      <Property name="key" type="string" required>
        A unique key expression to which the limit is applied. This expression is evaluated for each triggering event.

        Expressions are defined using the Common Expression Language (CEL) with the original event accessible using dot-notation. Read [our guide to writing expressions](/docs/guides/writing-expressions) for more info. Examples:

        * Ensure exclusive execution of a function per customer ID: `'event.data.customer_id'`
        * Ensure exclusive execution of a function per account and email address: `'event.data.account_id + "-" + event.user.email'`
      </Property>
      <Property name="mode" type="string" required>
        The mode to use for the singleton function:

        * `"skip"`: Skip the new run.
        * `"cancel"`: Cancel the existing run and start the new one.
      </Property>
    </Properties>
  </Property>
</Properties>

{/* ## Examples

### Ensure executing only upon the latest event

In this example, the active run of our `data-sync` function will be cancelled if another event with the same `user_id` is received:

```ts
// Example event payload:
// {
//   name: "data-sync.start",
//   data: {
//     user_id: "123456789",
//   }
// }
export default inngest.createFunction(
  {
    id: "data-sync",
    singleton: {
      key: "event.data.user_id",
      mode: "cancel",
    },
  },
  { event: "data-sync.start" },
  async ({ event, step }) => {
    await step.run(
      "fetch-latest-data-from-source",
      async () => {
        return await client.fetchData(event.data.user_id);
      }
    );

    await step.run("update-data-in-database", async () => {
      return await database.upsert({ id: company.id }, company);
    });
  }
);
```

While similar to [Debounce](/docs/guides/debounce), Singleton Functions are designed to ensure that only a single run of a function is happening at a time, whereas Debounce ensures that only a single event is processed within a given time window.

Refer to the [Singleton Functions guide](/docs/guides/singleton) for more information about how this feature works. */}