Moleculer has several built-in load balancing strategies. If services have multiple running instances, ServiceRegistry uses these strategies to select a node from all available nodes.
Built-in strategies
To configure strategy, set strategy
broker options under registry
property. It can be either a name (in case of built-in strategies) or a Strategy
class which inherited from BaseStrategy
(in case of custom strategies).
Configure balancing strategy
const broker = new ServiceBroker({ |
RoundRobin strategy
This strategy selects a node based on round-robin algorithm.
Usage
const broker = new ServiceBroker({ |
Random strategy
This strategy selects a node randomly.
Usage
const broker = new ServiceBroker({ |
CPU usage-based strategy
This strategy selects a node which has the lowest CPU usage. Due to the node list can be very long, it gets samples and selects the node with the lowest CPU usage from only samples instead of the whole node list.
Usage
const broker = new ServiceBroker({ |
Strategy options
Name | Type | Default | Description |
---|---|---|---|
sampleCount |
Number |
3 |
the number of samples. To turn of sampling, set to 0 . |
lowCpuUsage |
Number |
10 |
the low CPU usage percent (%). The node which has lower CPU usage than this value is selected immediately. |
Usage with custom options
const broker = new ServiceBroker({ |
Latency-based strategy
This strategy selects a node which has the lowest latency, measured by periodic ping commands. Notice that the strategy only ping one of nodes from a single host. Due to the node list can be very long, it gets samples and selects the host with the lowest latency from only samples instead of the whole node list.
Usage
const broker = new ServiceBroker({ |
Strategy options
Name | Type | Default | Description |
---|---|---|---|
sampleCount |
Number |
5 |
the number of samples. If you have a lot of hosts/nodes, it’s recommended to increase the value. To turn of sampling, set to 0 . |
lowLatency |
Number |
10 |
the low latency (ms). The node which has lower latency than this value is selected immediately. |
collectCount |
Number |
5 |
the number of measured latency per host to keep in order to calculate the average latency. |
pingInterval |
Number |
10 |
ping interval (s). If you have a lot of host/nodes, it’s recommended to increase the value. |
Usage with custom options
const broker = new ServiceBroker({ |
Custom strategy
Custom strategy can be created. We recommend to copy the source of RandomStrategy and implement the select
method.
Create custom strategy
const BaseStrategy = require("moleculer").Strategies.Base; |
Use custom strategy
const { ServiceBroker } = require("moleculer"); |