How to Set Up gRPC on a VPS

gRPC is a high-performance RPC framework using Protocol Buffers, well-suited for internal service-to-service communication where efficiency and strong typing matter. This guide covers basic setup on a VPS.

When gRPC Makes Sense

See How to Choose Between REST, GraphQL, and gRPC for the fuller comparison — gRPC excels for internal microservice communication, particularly where performance (binary protocol, HTTP/2 multiplexing) and strong contract typing matter more than human-readability or broad client compatibility.

Step 1 — Define Your Service with Protocol Buffers

syntax = "proto3";

service ProductService {
  rpc GetProduct (ProductRequest) returns (ProductResponse);
}

message ProductRequest {
  int32 id = 1;
}

message ProductResponse {
  int32 id = 1;
  string name = 2;
  double price = 3;
}

The .proto file defines your service contract — strongly typed, language-agnostic, and used to generate client/server code in multiple languages.

Step 2 — Generate Code from the Proto File (Node.js Example)

npm install @grpc/grpc-js @grpc/proto-loader
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync('product.proto');
const productProto = grpc.loadPackageDefinition(packageDefinition);

Step 3 — Implement the Server

const server = new grpc.Server();
server.addService(productProto.ProductService.service, {
  GetProduct: (call, callback) => {
    callback(null, { id: call.request.id, name: 'Widget', price: 9.99 });
  }
});
server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
  server.start();
});

Step 4 — Implement a Client

const client = new productProto.ProductService('localhost:50051', grpc.credentials.createInsecure());
client.GetProduct({ id: 123 }, (err, response) => {
  console.log(response);
});

Securing gRPC with TLS

const credentials = grpc.ServerCredentials.createSsl(
  fs.readFileSync('ca.crt'),
  [{ private_key: fs.readFileSync('server.key'), cert_chain: fs.readFileSync('server.crt') }]
);

Never use insecure credentials in production — configure proper TLS, particularly important since gRPC is often used for internal service communication that may carry sensitive data between services.

Configuring firewalld/ufw for gRPC's Port

sudo ufw allow 50051/tcp

gRPC typically uses a specific configured port (50051 is a common convention, but any port can be used) — open only to the specific sources that genuinely need access, particularly if this is meant as internal-only service communication.

Load Balancing gRPC Traffic

gRPC's HTTP/2 foundation means traditional round-robin load balancing (designed for HTTP/1.1's per-request connections) doesn't always distribute load evenly, since gRPC connections are long-lived and multiplexed — use a load balancer with genuine gRPC/HTTP2 awareness (Nginx with appropriate configuration, or a dedicated gRPC-aware proxy) for proper load distribution.

Common Errors

"14 UNAVAILABLE" errors from clients — typically indicates the client can't reach the server; verify the server is running, the port is correctly open in your firewall, and (if using TLS) certificates are properly configured on both sides.

Continue Reading

Browse more articles in Object Storage, Messaging & APIs.

  • grpc vps setup, protocol buffers tutorial, grpc nodejs example, grpc tls security
  • 0 Usuários acharam útil
Esta resposta lhe foi útil?

Artigos Relacionados

How to Set Up Self-Hosted S3-Compatible Object Storage with MinIO

MinIO is a high-performance, self-hosted object storage server compatible with the S3 API —...

How to Use Object Storage for Application File Uploads

Storing user-uploaded files directly on your application server's disk creates scaling and...

How to Install and Configure RabbitMQ on a VPS

RabbitMQ is a widely-used, robust message broker — enabling applications to communicate...

How to Install and Configure Redis as a Message Queue

Redis, primarily known as a cache, also works well as a lightweight message queue for simpler use...

How to Build and Secure a REST API on a VPS

This guide covers the essential security and architecture practices for deploying a REST API on...