I’m building an app which integrates with a 3rd party software. This 3rd party software sends out a “change notification” via a callback url to notify my app when to search for new data. This is great as it prevents unnecessary polling for data but unfortunately they seem to spam the callback URL multiple times within a few milliseconds.
I can’t wrap my mind around how to prevent duplicating the actions taken by my app when it receives the callback.
The callback is something like this:
https://your_callback_url?connectionId=5fecbc200f0e4a7cbf41040e11047e56&ap
iKey=2de51c4fd0f04b9fabeb95225e87da70&type=Contact&id=3DC13A80-6EB1-4D3D-
B784-1B42BE325B7E&operation=Update
Specifically I’m looking for an “invoiced” type from the parameters and ignoring all others.
When my app receives a callback I do the following:
- confirm parameters are received and type equals “invoiced”
- make api request to 3rd party app to get the invoice info and corresponding customer info
- Check if the invoice ID received by the callback already exists and, if not, create the invoice in my own database
- create or update if exists the customer info in my own database
- send an SMS message if a certain setting is enabled.
I was expecting the callback to be received, which would allow me to keep both customers and invoices in sync with the 3rd party app at all times without having to poll for data constantly. This does work however i’m receiving errors from the database because it’s attempting to create the same record multiple times.
I’ve tried the following:
-
check if the invoice exists first before performing any other actions then ending the function if it does. This doesn’t work because the callbacks are too close together (4ms in some cases) and so the first callback can’t complete the post to the database (50ms) before the second checks for the record (4ms)
-
Stupidly, I tried adding a time out in various places in the function to try and allow time for any previous callbacks to finish but all this did was push the whole process out by the duration of the timeout. Didn’t help at all.
-
Tried creating a callback monitoring database in which every callback is tracked by posting a record of it to a database. Then later in the function checking that there’s only 1 and ignoring any duplicates. This seemed really clunky and unnecessary, doesn’t work 100% as I found the odd invoice would get skipped completely which isn’t acceptable.
There has to be some simple way to deal with this?