Deduplication in BullMQ delays and deduplicates job execution based on specific identifiers. Within a specified period, or until a job completes or fails, no new jobs with the same identifier will be added to the queue. Instead, these attempts trigger a deduplicated event.From src/classes/job.ts:160:
Simple Mode extends deduplication until the job’s completion or failure. While a job remains incomplete, subsequent jobs with the same deduplication ID are ignored.
import { Queue } from 'bullmq';const myQueue = new Queue('Paint');// Add a job that will be deduplicated until it completes or failsawait myQueue.add( 'house', { color: 'white' }, { deduplication: { id: 'customValue' } },);
While this job is not in completed or failed state, subsequent jobs with the same deduplication ID will be ignored, and a deduplicated event will be emitted.
Simple Mode is useful for long-running jobs or critical updates that must not be duplicated until resolved, such as processing a file upload.
Throttle Mode assigns a TTL (Time to Live) to a job. If a similar job is added during this TTL period, it’s ignored, preventing queue overload.
import { Queue } from 'bullmq';const myQueue = new Queue('Paint');// Add a job that will be deduplicated for 5 secondsawait myQueue.add( 'house', { color: 'white' }, { deduplication: { id: 'customValue', ttl: 5000 } },);
After adding this job, any subsequent job with the same deduplication ID customValue added within 5 seconds will be ignored.From src/types/deduplication-options.ts:
/** * Deduplication options */export type DeduplicationOptions = { /** * Identifier */ id: string;} & { /** * ttl in milliseconds */ ttl?: number; /** * Extend ttl value */ extend?: boolean; /** * replace job record while it's in delayed state */ replace?: boolean;};
Throttle Mode is useful for scenarios with rapid, repetitive requests, such as multiple users triggering the same job.
/** * Removes a deduplication key if job is still the cause of deduplication. * @returns true if the deduplication key was removed. */async removeDeduplicationKey(): Promise<boolean> { if (this.deduplicationId) { const result = await this.scripts.removeDeduplicationKey( this.deduplicationId, this.id, ); return result > 0; } return false;}