0

I have some queries I am making that cause timeouts. Because the computing of the data takes too long on the server in some very specific edge cases. What is the best solution for my specific stack? I was thinking of polling the server every 20 seconds or so to see if my data is ready. But I'm not sure how to accomplish this. Or how to keep track of which client made which request that I'm currently processing.

My stack:

anthony-dandrea
  • 2,583
  • 7
  • 26
  • 46

2 Answers2

0

Since you are using Django you can easly use Django Channel to enable websockets in Django.

With websocket you can send an asynchronous notification from the server to the client made the request when the data is ready so you don't need to poll every second.

In that link there are all the info to create a bidirectional communication. The tutorial is for a chat system, but you can easly adapt to your needs

TheEnigmist
  • 906
  • 1
  • 10
  • 23
  • That has crossed my mind. I'm using Apollo for my frontend and based on their [docs](https://www.apollographql.com/docs/react/data/subscriptions/#when-to-use-subscriptions) it seems to say subscriptions(websockets) don't really fit this use case. I also would like to stay away from websockets because this request will work for +95% of use cases. I just would like to avoid timeouts for the cases where it does not. – anthony-dandrea Apr 21 '21 at 02:13
0

You have options. One is Apollo retry link which which you can use as follows

    const myLink = new RetryLink({
          delay: {
            initial: 300,
            max: Infinity,
            jitter: true
          },
          attempts: {
            max: 5,
            retryIf: (error, _operation) => !!error
          }
    });

I think this is the least hustle way to do it. Retry Link Doc

Second option is to use subscriptions instead of queries. Subscriptions are an open link between the client and server usually use for chat apps or any anything needed in real-time, so you can use that and the client will get a response when the server is done with calculation. You'll have to do a bit work on the client side and server side to get it to work.

Yankz Kuyateh
  • 612
  • 1
  • 5
  • 18
  • I think Retry would restart my backend process for every time it makes a request right? So if I have a call that consistently hits a timeout. Wouldn't just retrying just cause another timeout. Worth noting I don't have any caching mechanisms implemented currently. For Subscriptions, I also would like to stay away from websockets because this request will work for +95% of use cases. I just would like to avoid timeouts for the cases where it does not. – anthony-dandrea Apr 21 '21 at 03:13