<- Back

Getting Started with gRPC

July 14, 2025

grpc

What is gRPC?

gRPC (gRPC Remote Procedure Calls) is a high-performance, open-source framework developed at Google in 2015, that enables distributed systems to communicate across networks efficiently. It uses HTTP/2 for transport and Protocol Buffers (protobufs) for data serialization. It is perfect for communicating between microservices and distributed systems thanks to its cross-language support and strong typing.

Why gRPC is Faster than REST?

1. Protocol Buffers

gRPC uses protobufs to serialize data into a compact binary format, which take less space and bandwidth compared to JSON which is typically used in REST.

2. Support for Streaming

gRPC supports client, server and bidirectional streaming, allowing for real-time, low latency communication. The same can be done with REST by using websockets, but it comes with its added complexity.

3. HTTP/2 for Transport

HTTP/2 allows for multiplexing multiple requests over a single connection, thereby improving throughput and reducing latency.

It also supports header compression via HPACK. Many of the headers in gRPC are repetitive, HPACK replaces these header with indexed references, reducing the size of the headers sent over the network.

When to Use gRPC

1. Microservices Architecture

gRPC is ideal for communicating between microservices due to it strongly typed nature. This helps catch errors at compile time rather than runtime, making it easier to maintain.

It also allows for teams work independently on different services while ensuring seamless integration.

2. Low Latency Systems

In environments where low latency is critical, such as real-time applications, gRPC's support for streaming and efficient serialization can significantly improve performance.

3. Environments with Multiple Languages

gRPC supports multiple programming languages, making it a great choice for polyglot environments. Protobufs generate client and server code in various languages, thus ensuring compatibility and type safety across different services. Take a look at the supported languages here.

When not to Use gRPC

1. Client side applications

Most browsers do not expose HTTP/2 features that gRPC relies on, making it difficult to use gRPC directly in client-side applications for security reasons. gRPC uses something called trailers, in which the server sends additional metadata after the response body. This is not supported in browsers. This can be worked around by using a proxy server that translates gRPC calls to REST, but it adds complexity.

2. When Readability is a Priority

gRPC uses Protocol Buffers for serialization, which is not human-readable like JSON. If your application requires human-readable data formats, REST with JSON might be a better choice.

3. Simplicity

If your application is simple and does not require the advanced features of gRPC, such as streaming or multiple language support, REST might be a simpler choice.

Now lets write a simple gRPC service

It is a simple gRPC service in Javascript that adds 2 numbers together. Uses Node.js for the server and client.

1. Initialize and Install Dependencies

npm init -y
npm install @grpc/grpc-js @grpc/proto-loader

2. Create a Protobuf File

Create a file named adder.proto with the following content:

syntax = "proto3";

package adder;

service Adder {
  rpc Add (AddRequest) returns (AddResponse);
}

message AddRequest {
  int32 a = 1;
  int32 b = 2;
}

message AddResponse {
  int32 result = 1;
}

The above code should be self-explanatory. It defines a service called Adder with a single RPC method Add that takes an AddRequest message and returns an AddResponse message.

If you're wondering what the numbers after the field names mean, they are called field numbers. They are used to identify fields format and must be unique within a message.

3. Server

Create a file named server.js with the following contents:

const grpc = require("@grpc/grpc-js")
const protoLoader = require("@grpc/proto-loader")

const packageDefinition = protoLoader.loadSync("adder.proto")
const adder = grpc.loadPackageDefinition(packageDefinition).adder // our package was called adder, remember

const add = (call, callback) => {
  const { a, b } = call.request
  const result = a + b
  callback(null, { result }) // null here signifies no errors, the callback's function signature is (error, response)
}

const server = new grpc.Server()
server.addService(adder.Adder.service, { Add: add }) // Register the service with the server

server.bindAsync("localhost:8080", grpc.ServerCredentials.createInsecure(), (error) => {
  if (error) {
    console.error("Error binding server:", error)
    return
  }
  console.log(`Server running at http://localhost:8080`)
  server.start()
})

In the above code, we load the protobuf file, define and register the add function that implements the Add RPC method, and start the server on port 8080.

And for your reference, createSsl() should be used instead of createInsecure() in production to ensure secure communication via SSL.

4. Client

Create a file named client.js with the following contents:

const grpc = require("@grpc/grpc-js")
const protoLoader = require("@grpc/proto-loader")

const packageDefinition = protoLoader.loadSync("adder.proto")
const adder = grpc.loadPackageDefinition(packageDefinition).adder

const client = new adder.Adder("localhost:8080", grpc.credentials.createInsecure())

// Call the Add method defined inside the Adder service
client.Add({ a: 2, b: 3 }, (error, response) => {
  if (error) {
    console.error("Error calling Add:", error)
    return
  }
  console.log("Result:", response.result) // Should print "Result: 5"

  client.close()
})

The above code simply loads the protobuf file, creates a client for the Adder service, and calls the Add method with two numbers. The response is logged to the console.

5. Run the Server and Client

Open two terminal windows. In the first terminal, run the server:

node server.js

In the second terminal, run the client:

node client.js

And Voila!. You just wrote your first gRPC service.

Hope you learnt something new today.

Find the resources I used while messing around with gRPC below:

Written with <3 by Ananth.