How to avoid a webhook race condition

How to avoid a webhook race condition

jstaab
Shopify Partner
20 1 11

I recently added a subscription to the `products/update` webhook to my application. We already subscribe to the `orders/create` webhook, which we use to modify available quantity on an item based on the line item's sold quantity. Unfortunately, a `products/update` gets triggered at the exact same time as an `orders/create`, and if it ends up getting handled first, our system records first a modification of quantity because of the update, then a sale — double decrementing product quantity in our system.

 

Currently we are handling this by delaying `products/update` webhooks by 10 seconds to make sure the order gets processed first, but this feels extremely brittle. Another alternative would be to check whether an order for that item has been processed in the last x seconds, but that feels equally brittle, and more complex.

 

My ideal solution would be to have a `reason` attribute in the `products/update` webhook so that I could ignore the update when it's the result of a sale we'll be processing anyway. Is there a blessed way to handle this?

Replies 0 (0)