Nodejs- prevent concurrent async calls on clustered server

346 views Asked by At

I'm running a Nodejs Express server in a clustered environment. The server gets hundreds of requests for the same resource.

While processing the request for this resource, I make an async call to another service(say service A), wait for its result, memoizes the promise call to service A in-memory and return the response for the request.

Because I'm running a clustered server, other workers start accepting requests for the same resource. And because the memoized promise for service A is per worker and the promise hasn't resolved yet, I end up making multiple calls for the same resource to Service A.

Is there a way I can prevent making these duplicate calls to service A whilst still working with a clustered environment.

1

There are 1 answers

0
Mazki516 On

You should use a shared memory for all cluster processes.

As mentioned , you can use redis, which is preferred if you need to scale beyond one Cluster machine.

But if you don’t have a lot of data to memorize I would implement a super-simple key:value store with node-ipc (get,set)

All cluster process write&read from the same data source and it will solve your problem.

Another Approach

Create a micro service , Which calls API A,B,C on demand, and save the result to memory. You can also implement global throttling this way.

Then , the cluster gets requests , and calls your micro-service which either immediately return (result in cache) or calls api A.

P.S - if you need to scale this micro service, you may better use redis and simply hook it to the cluster.