moleculer icon indicating copy to clipboard operation
moleculer copied to clipboard

Feat: moleculer-runner in cluster mode with settings instance by service

Open intech opened this issue 4 years ago • 3 comments

Describe the solution you'd like Broker in runner cluster mode load all services by default and create every service replicate. Need settings and logic in moleculer-runner for load services on every worker in cluster and every service.

In service add internal params $cluster: false or number replicate. For for compatibility I suggest set default by true or 'max'.

Example: For service1 set $cluster: 1. For service2, nothing (by default max). For service3, set $cluster: 3.

As a result, we should see with moleculer-runner -i 10 service1 - 1 instance service2 - 10 instances service3 - 3 instances

intech avatar Aug 27 '20 19:08 intech

The problem is that, you can't access the service schema before loading it. So maybe you can define them in broker metadata and process that in the Runner cluster mode, e.g:

metadata: {
  $cluster: {
    service1: 1,
    service2: 10,
    service3: 3
  }
}

icebob avatar Aug 29 '20 11:08 icebob

I am thinking of a variant with dynamic worker balancing. We can define the logic for connecting services in the loadService method for loading services, add a check for already running instances in the registry and compare with the installed settings.

Thus, rebalancing will occur every respawn trying to load all services. And it will not load those of which the required number is already in the registry.

I understand that this will make the application start slowly. It will take a while for each worker to make sure the exact number of running instances is accurate.

intech avatar Sep 07 '20 17:09 intech

I think this can be implemented as a middleware without modifying the core modules because via broker it can start/stop services anytime.

icebob avatar Sep 09 '20 08:09 icebob

In terms of container architecture, this was my wrong suggestion. It is correct when there is one process per 1 CPU inside the container without a race for resources and control over the container.

intech avatar Oct 10 '22 23:10 intech