# Is there a way to provide Enqueue a unique identifier

**URL:** <https://discuss.hangfire.io/t/is-there-a-way-to-provide-enqueue-a-unique-identifier/8964>\
**Category:** question\
**Tags:** queues\
**Created:** [February 13, 2022, 11:48pm UTC](https://discuss.hangfire.io/t/is-there-a-way-to-provide-enqueue-a-unique-identifier/8964 "2022-02-13T23:48:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![haacked](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/haacked/32/2818_2.png) [@haacked](https://discuss.hangfire.io/u/haacked)\
**Post date:** [February 13, 2022, 11:48pm UTC](https://discuss.hangfire.io/t/is-there-a-way-to-provide-enqueue-a-unique-identifier/8964/1 "2022-02-13T23:48:29Z")

</div>

I have a web app that uses Hangfire to enqueue background jobs and set up recurring jobs. It works great.

However, I need to start processing incoming events from an external service. This service sends me events with unique identifiers. If my app doesn’t respond in time, the external service might retry the event.

In order to handle these events, I’d like to do something like this:

```auto
BackgroundJob.Enqueue(() => HandleEvent(event), event.UniqueIdentifier);

```

And let Hangfire handle ensuring that my `HandleEvent` call is not called twice for the same event. Is there a way to do that?

Right now, it looks like the only way to do that is for my app to keep track of the event IDs and only enqueue a task when the event id is unique.

---

<div class="post-metadata">

**Author:** ![odinserj](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/odinserj/32/648_2.png) [@odinserj](https://discuss.hangfire.io/u/odinserj)\
**Post date:** [February 15, 2022, 4:52am UTC](https://discuss.hangfire.io/t/is-there-a-way-to-provide-enqueue-a-unique-identifier/8964/2 "2022-02-15T04:52:11Z")

</div>

Unfortunately idrmpotent calls aren’t directly supported as for now. But I’m sure it’s possible to write a client filter that acquires a distributed lock, checks existence of some key and:

1. Exits without doing anything if exists, filter context has the Canceled property to exit without creating a job.
2. Creates a key with some time to live set, and creates a background job (preferable atomically with key creation, but atomicity will make it more difficult to write this filter).

And then releases the distributed lock. Sorry, this pseudo-code is the only I can give to you currently. And please use slightly different keys for distributed lock and idempotence key (different prefix or suffix) to avoid collisions in storages where common key space is used.

---

<div class="post-metadata">

**Author:** ![szilardd](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/szilardd/32/3794_2.png) [@szilardd](https://discuss.hangfire.io/u/szilardd)\
**Post date:** [August 29, 2024, 6:33am UTC](https://discuss.hangfire.io/t/is-there-a-way-to-provide-enqueue-a-unique-identifier/8964/3 "2024-08-29T06:33:51Z")

</div>

@haacked have you found a solution for this?

---

<div class="post-metadata">

**Author:** ![szilardd](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/szilardd/32/3794_2.png) [@szilardd](https://discuss.hangfire.io/u/szilardd)\
**Post date:** [September 3, 2024, 11:29am UTC](https://discuss.hangfire.io/t/is-there-a-way-to-provide-enqueue-a-unique-identifier/8964/4 "2024-09-03T11:29:32Z")

</div>

Possible solution: [Allow creating any job with a specific job id · Issue #181 · HangfireIO/Hangfire · GitHub](https://github.com/HangfireIO/Hangfire/issues/181#issuecomment-2321273014)
