Custom Job IDs

BullMQ automatically generate job ids for all the jobs added to a queue. The job id is an increasing integer number. This is the default and recommended behaviour for most use cases.

It is however possible to add jobs with a custom ID that will be used instead of the one auto-generated. The particularity is that there cannot be 2 jobs with the same job ID, so if you add a second job with the same ID as an existing job in the queue, the second job will just be ignored.

This is usually useful in order to implement "debouncing". For example, lets say that you have some system that generates a lot of messages, but you only care to process 1 job, at least until that job has been completely processed. By using a custom job ID, only 1 job will exist in the queue until this job has been processed. If you keep the job in the completed set (the default), or the job fails and is kept in the failed set (also the default), then no new jobs will be added to the queue if they use the same job id. Therefore you must choose a proper "removeOnComplete/Fail" strategy suitable for your particular use case. You can read more about these options in the "Workers" section.

In order to specify a custom job id, just set the jobId field in the job's options:

interface JobOpts {
  jobId: string;
  // ... More opts
}
curl --location 'http://mydomain.dev/queues/my-queue/jobs' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer my-secret-token' \
--data '[{
	  "name": "paint-black",
          "data": {},
          "opts": {
            "jobId": "my-custom-id"
           }
         } 
]'

This call will add a job with a custom id, note that the data returned by the call now includes the custom id in the id field instead of the autogenerated one.

[
  {
    "name": "paint-black",
    "data": {},
    "opts": { "attempts": 0, "delay": 0, "jobId": "my-custom-id" },
    "id": "my-custom-id",
    "progress": 0,
    "returnvalue": null,
    "stacktrace": null,
    "attemptsStarted": 0,
    "attemptsMade": 0,
    "delay": 0,
    "timestamp": 1708618768745,
    "queueQualifiedName": "bull:my-test-queue"
  }
]

Last updated