Webhook Endpoints - How we can handle concurrent requests?

Hello Team,

I have written an incomming Webhook services, where it is being called from the front-end.

The service is about incrementing a custom serial number and inserting a document into the collection. When users made the concurrent request the webhook is not working as expected and we see some lag between the generated custom serial number which are duplicated sometimes.

The concurrent requests are not working as a queue.

New suggestions are still welcome and appreciate

Hello @Pradeep_Raj

First of all, I want to welcome you to the MongoDB Community Forum! - My name is Josman and I happy to help you with your question.

The service is about incrementing a custom serial number and inserting a document into the collection. When users made the concurrent request the webhook is not working as expected and we see some lag between the generated custom serial number which are duplicated sometimes.

Webhooks linked functions do not work like Triggers in the sense that event ordering cannot be enabled to allow multiple executions to occur sequentially based on the timestamps of change events. In Webhooks, functions will run in parallel.

However, that said, if you do not want duplicate documents to be inserted into your collections, you could follow two complementary approaches:

  1. Create a unique index in the field you don’t want to have duplicates. Moreover, you can control this in the logic of your function.
  2. Control this logic in the body of your function, i.e, using idempotent update operators like $addToSet instead of $push

I hope you found my explanation useful. Please do not hesitate to contact me if you need any further clarification or if you would like to provide us with further details.

Best Regards,
Josman

1 Like