Queuing APIs on serverless app

Hi Team,

So I am building a Freshdesk serverless app which links a custom object to a ticket (along side other functionalities) and a key thing I want to update on our third party tool is the amount of tickets linked to a certain custom object. I am using an SMI function to call an API to update the third party software

My idea was to have a counter field on our third party software and every time the Freshdesk app is used to link a custom object, the process would be something like:

  • Get counter
  • increase/decrease counter
  • update counter

This method works fine for some cases however, if two people use the app to link/unlink the same custom object at the same time, it records the whole process as one person linking/unlinking instead of 2.

So if the counter was originally 0, if 2 people link at the same time, it should be 2 however, it becomes one.

I am suspecting, the SMI function’s calls are not being queued.

I was hoping to get some eyes on this.

Thank you

Hello @Shairyl

A classic race condition :slight_smile:

The Freshworks Platform does not guarantee ordering, nor does it offer any kind of blocking semantics to help with this. A hark back to our OS (operating systems) classes would remind us that we need some kind of locking mechanism to help with this. Luckily, the platform offers an atomic operation called increment as part of its Key-Value Storage offering, that could help with this. You can start storing the counter with the platform’s key value (kv) store if that works for you. If the 3rd party system needs this, you can use the value in the kv store to update the 3rd party service via the app.

1 Like

It may not work perfectly, but you can try out this, @Shairyl

If the API calls are made to the 3rd party software, the 3rd party software would either respond with 200 or other.

Let’s have your app retry the request (there’s a retry request feature available) if it’s anything other than 200. In this case, all successful requests will anyway increase the counter.