moleculer icon indicating copy to clipboard operation
moleculer copied to clipboard

Load Balancing on custom metric

Open sysrun opened this issue 1 year ago • 4 comments

Is your feature request related to a problem? Please describe.

I want to use custom metrics like the cpu strategy

Additional context

I have multiple instances of one service. Every service can handle a limited amount of "slots". If no slot is available the load balancer should not try to send the request to the service which has no slots available.

sysrun avatar Aug 07 '23 07:08 sysrun

What is the question or problem?

icebob avatar Aug 07 '23 16:08 icebob

I am not sure how to make a service aware of a "dynamic" value (like the cpu in the CpuStrategy) which i could use in a custom LB-Strategy.

Example:

i have 3 instances/nodes of 1 service. This service is exposing an action called "assign". The service also has a "assigncounter" which is incremented if a "service.assign" is called at the specific instance.

Now i want to Loadbalance - so i have to know which instance has the lowest "assigncounter".

So: How could a LB-Strategy access such dynamic service variables? :)

sysrun avatar Aug 07 '23 18:08 sysrun

It can be only complicated way. The CPU strategy can work because CPU usage is shared between nodes, the INFO packet contains it. But a custom value is not shared and synchronized between the nodes. For this purpose, I recommend to use Redis to store and increment the value of your variable and get this value from the Redis in your Strategy.

icebob avatar Aug 11 '23 17:08 icebob

Ah, ok. Thanks :)

Redis is already my "plan b".

Would be nice if there would be an option to register custom stats in the future.

sysrun avatar Aug 11 '23 18:08 sysrun