C
C#3d ago
Vernon

How to Improve Long API Call Response Times?

Hey everyone, I have an API call that takes a significant amount of time to compute the result. During the computation, users have to wait until the API responds, which leads to a poor user experience. The calculation uses a parallel approach:
parallel for loop [1 ~ 100]
parallel for loop [1 ~ 1000]
parallel for loop [1 ~ 100]
parallel for loop [1 ~ 1000]
Is there a way to reduce the waiting time? Would using something like RabbitMQ or a similar message queue allow the task to run in the background and update the database once it's completed? Thanks for any suggestions!
2 Replies
becquerel
becquerel3d ago
there are many ways to deal with this either you speed up your actual calculation, or you make the API call asynchronous for the user speeding up your actual calculation is difficult to give advice for without knowing more for an asynchronous API, when the user issues their GET or POST request (or whatever), the endpoint doesn't return them the result, but instead a ticket or token representing the operation which they can then use to check back in later at another endpoint so GET /calculation?x=32&y=45 returns a token (some GUID or whatever) and then later they GET /calculation/status?id=[whatever] and you either return a 'not ready yet' message or their result to tell the user directly when their result is ready you might be able to use webhooks, but i've not looked into them myself
Would using something like RabbitMQ or a similar message queue allow the task to run in the background and update the database once it's completed?
if the user doesn't need to actually get a result back, and they're just triggering something to happen behind the scenes, then yes this works very well you can use RabbitMQ for this, or, if you would prefer something simpler that stays in-process, a simple combination of a Channel<T> and a BackgroundService which i can go into more detail on if it interests you
Pobiega
Pobiega3d ago
Could also use server side events or signalR to send the response when it's finished
Want results from more Discord servers?
Add your server