Skip to content

RPC load balancing

dataBridges exposes powerful RPC messaging service for devices and applications to implement high performance non-blocking request / response messages. Detailed API documentation

dataBridges by defaults load balances RPC calls. You can run multiple instances of RPC server(s) to increase the throughput. dataBridges in turn will route the rpc calls from clients to all the registered RPC servers.

We have created a sample RPC server and RPC client to validate the above functionality. You can run multiple instances of RPC servers and a single instance of RPC client program. The RPC client program will execute RPC calls N times and will output routing information.

You will see that dataBridges does a best-fit call routing and uses all the RPC servers thereby increasing the throughput and resiliency of your RPC server deployment.


A basic understanding of Node.js is needed to follow this tutorial.

Set up a dataBridges account and app

Before we jump right into setting up an application with dataBridges, you’ll need to create a dataBridges account and app, if you don’t already have one:

  1. Sign up for a dataBridges account.

  2. Create a new app by selecting Apps and clicking Create New button.

  3. You can retrieve your app credentials from the App Keys tab.

Initialize Node app

Assuming you’ve installed Node.js on your machine, Now create a working directory to hold your application and init the NodeJS app.

Run command npm init, It will create a package.json file for your application in your working directory. This command prompts you for a number of things, including the name and version of your application and the name of the initial entry point file (by default this is index.js). For now, just accept the defaults.

Download source

Download the rpc.loadbalancer.server by clicking on this link

Download the rpc.loadbalancer.client by clicking on this link


For this application, you will require to use databridges-sio-client-lib databridges-sio-client-lib (dataBridges Client Library) and databridges-sio-server-lib (dataBridges Server Library) with Node.js.

  • Install dataBridges library
  • npm install databridges-sio-client-lib databridges-sio-server-lib --save

Download the package.json by clicking on this link

Major Components of this example

Lets first understand how this example is structured and how to run it.

  • rpc.loadbalancer.server.js
  • rpc.loadbalancer.client.js

rpc.loadbalancer.server exposes MathTutor RPC server which has add function which can be utilized by RPC client connecting to this server. We will be starting multiple instance of rpc.loadbalancer.server in this example.

let mathTutor = dbridge.rpc.init('MathTutor');

// Define all exposed functions.
mathTutor.functions = function () {
    function add(inparameter, response) {
        const payload = JSON.parse(inparameter.inparam);
        const a = parseFloat(payload.a);
        const b = parseFloat(payload.b);
        response.end(payload.a + ' + ' + payload.b + ' = ' + (a + b).toString() + ' from ' + sessionID);
    // Bind the exposed function to mathTutor RPC server.
    mathTutor.regfn('add', add);

// Register RPC Server with dBrdige network.

rpc.loadbalancer.client connects to MathTutor RPC server and executes add function to get the result. As there are multiple instance of rpc.loadbalancer.server running, we will be getting sessionId of Callee RPC server and we will be analyzing loadbalancing based on the received sessionId.

// Connect to rpcServer
rpcClient = dbridge.rpc.connect('MathTutor');'add', JSON.stringify(funParam), 1000 * 60 * 2).then((response) => {
    // split the response and get the server session id by spliting the response data
    const sessionid = response.split("from")[1]
    if (sessionid in sessionIdCount) {
        //if the session id is available in the dict increment the count 
        sessionIdCount[sessionid] = sessionIdCount[sessionid] + 1
    } else {
        //if not available add the session id to the key of dict and add count = 1
        sessionIdCount[sessionid] = 1
}).catch((err) => {
    // Catch exception for .call
    console.log('RPC Call Error ', err.source, err.code, err.message);

Running this example

To run this example open at least two terminal window. On one window start the RPC Server with multiple instance. You can do this using pm2 or any other npm which supports multi instance running of an NodeJS application. If you are using pm2 you can run above with this command pm2 start rpc.loadbalancer.server.js -i 4 (This will start 4 instance of rpc.loadbalancer.server.js).

Next step is to start rpc.loadbalancer.client.js. In a terminal window run node rpc.loadbalancer.client.js to start the simulation. rpc.loadbalancer.client.js accepts 1 command line argument. This argument is for number of RPC calls to be done. Default is 20. If you need more RPC call run node rpc.loadbalancer.client.js N where N is number of RPC call you want to run during simulation.

Before you run rpc.loadbalancer.client.js NodeJS programs you need to edit rpc.loadbalancer.client.js and replace appkey and auth_url as shown below.

// Replace your dataBrdiges Application Key and Authentication URL below.
const dbApplnKey = 'appkey';
const dbAuthURL = 'auth_url';
Properties Description
auth_url (string) Client authentication url from dataBridges dashboard.
appkey (string) Client application Key from dataBridges dashboard.

Before you run rpc.loadbalancer.server.js NodeJS programs you need to edit rpc.loadbalancer.server.js and replace appkey ,appsecret and auth_url as shown below.

// Replace your dataBrdiges Application Key,Application Secret and Authentication URL below.
const dbApplnKey = 'appkey';
const dbAuthURL = 'auth_url';
const dbApplnSecret = 'appsecret';
Properties Description
auth_url (string) Server authentication url from dataBridges dashboard.
appkey (string) Server application Key from dataBridges dashboard.
appsecret (string) Server application Secret from dataBridges dashboard.

Understanding simulation output

Upon starting RPC Server using node rpc.loadbalancer.server.js each instance of application will give below output.

sessionID is ou73l
MathServer is now Online and ready for Math function processing ...

Each instance will have an unique sessionID which will identify the client has communicated to this instance. This sessionID will be passed back to client RPC call which will be recorded at client end for simulation output.

Upon starting RPC Client using node rpc.loadbalancer.client.js N , application will give below output depending on number of calls passed in the argument parameter N.

executing function(add) xx times
Rpc Call :add -> 46 + 70 = 116 from ou73l
Rpc Call :add -> 91 + 2 = 93 from fkit5
Rpc Call :add -> 27 + 33 = 60 from ou73l
Rpc Call :add -> 11 + 59 = 70 from fkit5
Rpc Call :add -> 60 + 66 = 126 from fkit5
Rpc Call :add -> 60 + 12 = 72 from ou73l

Press Ctrl + C to stop the application and get the simulation execution summary as below.

Response from RPC servers
 ou73l = 12
 dciay = 3
 fkit5 = 5

Above output shows which all RPC server instance has executed how many RPC Calls.