# Debounce

Debounce delays function execution until a series of events are no longer received. This is useful for preventing wasted work when a function might be triggered in quick succession. Use cases for debounce include:

- Preventing wasted work when handling events from user input that may change multiple times in a short time period.
- Delaying processing of noisy webhook events until they are no longer received.
- Ensuring that functions use the latest event within a series of updates (for example, synchronization).

## How to configure debounce

```ts {{ title: "TypeScript" }}
inngest.createFunction(
  {
    id: "handle-webhook",
    debounce: {
      key: "event.data.account_id",
      period: "5m",
      timeout: "10m",
    },
    triggers: { event: "intercom/company.updated" },
  },
  async ({ event, step }) => {
    // This function will only be scheduled 5 minutes after events are no longer received with the same
    // `event.data.account_id` field.
    //
    // `event` will be the last event in the series received.
  }
);
```

```go {{ title: "Go" }}
inngestgo.CreateFunction(
	client,
	inngestgo.FunctionOpts{
		ID: "handle-webhook",
		Debounce: &inngestgo.ConfigDebounce{
			Key:     "event.data.account_id",
			Period:  5 * time.Minute,
			Timeout: inngestgo.Ptr(10 * time.Minute),
		},
	},
	inngestgo.EventTrigger("intercom/company.updated", nil),
	func(ctx context.Context, input inngestgo.Input[map[string]any]) (any, error) {
		// This function will only be scheduled 5 minutes after events are
		// no longer received with the same `data.account_id` field.
		//
		// `ctx.event` will be the last event in the series received.
		return nil, nil
	},
)
```

```py {{ title: "Python" }}
@inngest_client.create_function(
    fn_id="handle-webhook",
    debounce=inngest.Debounce(
        key="event.data.account_id",
        period=datetime.timedelta(minutes=5),
    ),
    trigger=inngest.TriggerEvent(event="intercom/company.updated")
)
async def handle_webhook(ctx: inngest.Context):
    # This function will only be scheduled 5 minutes after events are no longer
    # received with the same `data.account_id` field.
    #
    # `ctx.event` will be the last event in the series received.
    pass
```

### Configuration reference

- `period` - The time delay to delay execution. The period begins when the first matching event is received.
- `key` - An optional [expression](/docs-markdown/guides/writing-expressions) using event data to apply each limit too. Each unique value of the `key` has its own limit, enabling you to rate limit function runs by any particular key, like a user ID.
- `timeout` - Optional. The maximum time that a debounce can be extended before running.

## How it works

When a function is triggered, the debounce `period` begins. If another event is received that matches the function's trigger, the debounce `period` is reset.  This continues until no events are received for the debounce `period`.  Once the `period` has passed without any new events, the function is executed using the last event received.

If a `timeout` is provided, the function will always run after the `timeout` has passed even if new events are received. This ensures that the function does not continue to be debounced indefinitely if events continue to debounce the function.

### Using a `key`

When a `key` is added, a separate debounce period is applied for each unique value of the `key` expression. For example, if your `key` is set to `event.data.customer_id`, each customer would have their individual debounce period applied to functions run. Read [our guide to writing expressions](/docs/guides/writing-expressions) for more information.

## Comparison to rate limiting

If you prefer to execute a function for the *first* event received, consider using [rate limiting](/docs-markdown/guides/rate-limiting) instead. Rate limiting ensures that a function runs once for each `key` the *first* time an event is received, while debounce uses the *last* event during a specified period.

## Combining with idempotency

Debounce can be combined with [idempotency](/docs-markdown/guides/handling-idempotency#at-the-function-level-the-consumer) to ensure that once the debounced function has run, it does not run again.

## Limitations

- The maximum debounce `period` is 7 days (168 hours).
- The minimum debounce `period` is 1 second.
- Debounce does not work with [batched functions](/docs-markdown/guides/batching).

## Further reference

- [TypeScript SDK Reference](/docs-markdown/reference/functions/debounce)
- [Python SDK Reference](/docs-markdown/reference/python/functions/create#configuration)