๐Ÿš…Brokers

Message brokers in Node.js act as intermediaries for communication between different processes or components by facilitating the efficient exchange of messages. When it comes to direct one-way sending, message brokers excel in speed due to their optimized routing mechanisms, allowing messages to be quickly dispatched from a sender to a recipient without the need for direct connections or complex coordination.

Configuring

index.js
const channelId = 'test';

// Listening to messages from clusers.
manager.broker.listen(channelId, (message) => {
    console.log(`Manager received a message from ${channel}: ${message}.`);
});

// Sending messages to all clusters.
manager.broker.send(channelId, 'test');

// Sending message to cluster.
const clusterId = 1;
manager.broker.send(channelId, 'test', clusterId);
cluster.js
const channelId = 'test';

// Listening to messages from manager.
client.cluster.broker.listen(channelId, (message: string) => {
    console.log(`Client ${client.cluster.id} received a message ${message}.`);
});

// Sending message to manager.
client.cluster.broker.send(channelId, 'test');

Why?

The incorporation of message brokers in our architecture was driven by the need for streamlined communication between various components and processes. By introducing message brokers, we aimed to enhance the efficiency and reliability of data exchange, enabling seamless interaction. This decision is substantiated by the benchmark image below, which vividly illustrates the significant performance gains achieved through the utilization of message brokers in terms of message throughput and reduced latency sending data in only one way.

Source
Manager Sent
Client Received
Client Sent
Manage Received
Total Time #1
Total Time #2

Broker

17.76ms

17.42ms

17.04ms

17.65ms

661.5ยตs

616.4ยตs

IPC Send

19.72ms

19.78ms

19.37ms

19.55ms

1058.8ยตs

1171.8ยตs

Broker Leads For
Advantage in %
Category

397.3ยตs

60.06%

Manager -> Clusters

555.4ยตs

90.10%

Cluster -> Manager

Last updated

Was this helpful?