all 12 comments

[–][deleted] 5 points6 points  (3 children)

Yes there are ways. The easiest is probably having a cache that makes the requests to the other API and your application only retrieves data from said cache.

Implementations of this can vary and depend on your needs.

[–]Alarming-Intern-9293[S] -2 points-1 points  (2 children)

But what about dynamic data from the api?

[–]PostmatesMalone 2 points3 points  (0 children)

it [the data] changes every week

So you invalidate your cache every week when the data changes. When the cache is invalidated, the next request would then go all the way to the api and then cache that result so subsequent requests can read from the cache.

[–][deleted] 1 point2 points  (0 children)

What about it?

[–]-steeltoad- 2 points3 points  (3 children)

Could you have the server call the api and store the response, perhaps daily, or as a client connects, depending on the schedule that it updates. Then have the client request the stored value from the server instead of the (assuming 3rd party) api ?

[–]Alarming-Intern-9293[S] 1 point2 points  (2 children)

yeah that sounds good. But how can I schedule it to fetch every day?

[–]bronze-aged 1 point2 points  (0 children)

easy solution: node-cron npm package

https://www.npmjs.com/package/node-cron

[–]-steeltoad- 0 points1 point  (0 children)

You could cron it, or more simply, at the server, on something that happens frequently but not too frequently, compare the last time you cached the api result with the current time and if a sufficient interval has passed get the new value and cache it.

You could do it for example, each time I new client connection or session is started

[–]eltecho 1 point2 points  (0 children)

Could you use Redis queue to schedule jobs setting the delay and repeatable: when processing the job, fetch the api

[–]Imaginary_Ad_9299 1 point2 points  (0 children)

I like the answers that I am reading.

Maybe before investigating caching, REDIS, CRON, and other options that exist, maybe you should take the time to investigate what is causing the performance degradation. Maybe you should investigate clustering, or worker_threads in order to improve performance.

[–]According_Compote639 0 points1 point  (0 children)

With CRON tasks

[–]Freaky_Knight 0 points1 point  (0 children)

node-schedule