gRPC (grpc-node) vs REST (ExpressJs)

Nilay Sahu
3 min readDec 27, 2019
gRpc vs Rest (source: google images)

I was trying out a practical comparison between npm packages grpc-node and express.js provided the following settings:

  1. 2.3 GHz CPU + 8 GB Memory

2. Running server and client on the same machine.

3. npm packages used: express, request, grpc & @grpc/proto-loader

What is gRPC

gRPC is an RPC platform developed by Google which was announced and made open source in late Feb 2015. It is supposed to be a fast and efficient Remote Procedure Call. gRPC uses Protocol Buffer binary serialization for the request and response message format

Comparison

To begin with, I created two servers using filesexpress_server.js and grpc_server.js and corresponding clients express_client.js and grpc_client.js

//express_server.js
const express = require(‘express’);
var app = express();
const notes = { id: ‘1’, title: ‘Note 1’, content: ‘Content 1’}app.get(‘/notes’, function(request, response){
response.status(200).json(notes);
});
const port = 50052;
app.listen(port);
console.log(‘Express listening to port’+ port);

below is the grpc_server.js

//grpc_server.js
let grpc = require(“grpc”);
var protoLoader = require(“@grpc/proto-loader”);

const server = new grpc.Server();
const SERVER_ADDRESS = “0.0.0.0:5001”;

// Load protobuf
let proto = grpc.loadPackageDefinition(
protoLoader.loadSync(“chat.proto”, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
})
);
function getNotes(_,callback){
const notes = { id: ‘1’, title: ‘Note 1’, content: ‘Content 1’};
callback(null,notes)
}

// Define server with the methods and start it
server.addService(proto.example.NoteService.service, { list: getNotes });
server.bind(SERVER_ADDRESS, grpc.ServerCredentials.createInsecure());
server.start();

For the above grpc_server to work we need the file protocol buffer as well (chat.proto).

//chat.proto
syntax = “proto3”; //Specify proto3 version.
package example; //Optional: unique package name.
service NoteService {
rpc List(Empty) returns (Note) {}
}
message Empty {}
message Note {
string id = 1;
string title = 2;
string content = 3;
}

Now coming to the client-side. Corresponding clients are named as express_client.js based on request.js

//express_client
const request = require(‘request’);
const options = {
‘url’: “http://localhost:50052/notes",
‘method’: ‘GET’,
‘json’: true
};
request(options, function(err, response, body) {
if(err){
console.log(“err “,err);
}
});

and the grpc_client as

//grpc_client
let grpc = require(“grpc”);
var protoLoader = require(“@grpc/proto-loader”);
var proto = grpc.loadPackageDefinition(
protoLoader.loadSync(“chat.proto”, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
})
);
const REMOTE_SERVER = “0.0.0.0:5001”;
let client = new proto.example.NoteService(
REMOTE_SERVER,
grpc.credentials.createInsecure()
);
client.list({}, (error, notes) => {
if (error) {
console.log(error)
}
});

Below are the results (I will keep updating this.. with better comparison tools)

+ — — — — — -+ — — — — -+ — — — — — — -+ — — — — — — |
|iterations | grpc | express | server in |
+ — — — — — -+ — — — — -+ — — — — — — -+ — — — — — — |
| 10 | 208ms | 233ms | local |
| 100 | 205ms | 259ms | local |
+ — — — — — -+ — — — — -+ — — — — — — -+ — — — — — — +

You can find above program files at https://github.com/nilaysahu/grpcTest

--

--