Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe: webhook events order

How should you handle the fact that events received via webhooks can be received in random order ?

For instance, given the following ordered event:

  • A: invoiceitem.created (with quantity of 1)
  • B: invoiceitem.updated (with quantity going from 1 to 3)
  • C: invoiceitem.updated (with quantity going from 3 to 2)

How do you make sure receiving C-A-B does not result in corrupted data (ie with a quantity of 2 instead of 3)?

You could reject the webhook if the previous_attributes in Event#data do not correspond to the current state, but then you are stuck if your local model was updated already, as you will never find yourself in the state expected by the webhook.

Or you can just use treat any webhook as a hint to retrieve and update an object. You just disregard the data sent by the webhook and always retrieve it. Even if you receive events ordered as update/delete/create it should work, as update would in fact create the object, delete would delete it, and create would fail to retrieve the object and do nothing. But it feels like a waste of resources to retrieve data each time when the webhook offers it as event data.

This question was asked before but the answers don't cover the above solutions.

Thanks

like image 340
Arnaud Avatar asked Jul 04 '18 11:07

Arnaud


1 Answers

If your application is sensitive to changes like this that can occur close in time, you really should just use the event as a signal to retrieve the object, as @koopajah noted in their comment. That's the only way to ensure you have the latest state.

like image 161
Nolan H Avatar answered Oct 02 '22 17:10

Nolan H