title: Debounce Workflows to Prevent Wasted Work impact: MEDIUM impactDescription: Prevents redundant workflow executions during rapid triggers tags: pattern, debounce, delay, efficiency
Debounce Workflows to Prevent Wasted Work
Use Debouncer to delay workflow execution until some time has passed since the last trigger. This prevents wasted work when a workflow is triggered multiple times in quick succession.
Incorrect (executing on every trigger):
async function processInputFn(userInput: string) {
// Expensive processing
}
const processInput = DBOS.registerWorkflow(processInputFn);
// Every keystroke triggers a new workflow - wasteful!
async function onInputChange(userInput: string) {
await processInput(userInput);
}
Correct (using Debouncer):
import { DBOS, Debouncer } from "@dbos-inc/dbos-sdk";
async function processInputFn(userInput: string) {
// Expensive processing
}
const processInput = DBOS.registerWorkflow(processInputFn);
const debouncer = new Debouncer({
workflow: processInput,
debounceTimeoutMs: 120000, // Max wait: 2 minutes
});
async function onInputChange(userId: string, userInput: string) {
// Delays execution by 60 seconds from the last call
// Uses the LAST set of inputs when finally executing
await debouncer.debounce(userId, 60000, userInput);
}
Key behaviors:
debounceKeygroups executions that are debounced together (e.g., per user)debouncePeriodMsdelays execution by this amount from the last calldebounceTimeoutMssets a max wait time since the first trigger- When the workflow finally executes, it uses the last set of inputs
- Once the debounce period expires and the workflow is released for execution, the next
debouncecall starts a new cycle for a new workflow execution - Workflows from
ConfiguredInstanceclasses cannot be debounced
Reference: Debouncing Workflows