Tips on how to handle polling


I’m using an external API for a service that takes longer than 10 seconds to run. It calls a webhook once it’s done processing, which adds an object to my database. I’m currently polling the database every x seconds with a serverless function to see if a new object is found there.

However, this ends up being very expensive, because I have to invoke a lot of function calls. For example, if I poll every 5 seconds and it might take a minute to fulfill, I might be using 12 function calls to handle this one event.

Any tips on how to do this more efficiently?

Thanks :slight_smile:

@of-dev If it tends to take 60 seconds to resolve there would be no need to poll every 5 seconds between 0 and “the minimum time it takes to complete”.

If 55 seconds was the fastest you ever saw it complete, you could wait 55 seconds for the first call then poll every 3 seconds. Which would both use less calls and have a chance of reacting faster once the value does change.

Alternatively, you could write the value to a realtime database, (or even just any value that represents that “it has changed”), and listen to the value in the realtime database, you’ll know exactly when it changes.

Thanks for the reply! I’ve currently chosen the approach to waiting a while before start the polling. However, sometimes the external api is very fast and in those cases waiting for 50 seconds is not optimal.

Realtime database was an interesting one. I’ll have to look into that, thank you!