Full answer
Protobuf
- You describe messages in a .proto file with numbered fields (int32 id = 1; string name = 2;). The field number, not the name, identifies a field on the wire. Values use compact encodings (varints for integers, length-prefixed bytes for strings). Field names are not sent.
- Code generators create classes for many languages from the same file, so a Node client and a Go server share one contract.
gRPC
- HTTP/2 transport: many calls over one connection, header compression, and four call types: unary, server streaming, client streaming and bidirectional streaming.
- Built in: deadlines, cancellation, metadata (headers), status codes and interceptors.
Compared with REST + JSON
- Size and speed: binary protobuf is usually smaller and faster to parse than JSON.
- Contract: the schema is the source of truth, and breaking changes are easy to detect.
- Tooling: curl and browsers work with REST directly. gRPC needs special tools, and browsers need gRPC-Web or a gateway.
- Caching: HTTP caches and CDNs work well with GET in REST, and not with gRPC.
- Best fit: internal service to service calls, streaming, polyglot teams. REST fits public APIs and browser clients.
javascript
const protobuf = require('protobufjs');
const root = protobuf.parse(`
syntax = "proto3";
message Student { int32 id = 1; string name = 2; repeated string tags = 3; bool active = 4; }
`).root;
const Student = root.lookupType('Student');
const value = { id: 150, name: 'Asha', tags: ['web', 'node'], active: true };
const bytes = Student.encode(Student.create(value)).finish();
const json = JSON.stringify(value);
console.log('JSON bytes :', Buffer.byteLength(json));
console.log('protobuf bytes :', bytes.length);
console.log('protobuf hex :', Buffer.from(bytes).toString('hex'));
// Reading the first field by hand: tag 0x08 = field 1, wire type 0 (varint); then 0x96 0x01 = 150.
const [tag, b1, b2] = bytes;
console.log('tag byte :', tag.toString(16), '= field', tag >> 3, ', wire type', tag & 7);
console.log('varint 96 01 :', (b1 & 0x7f) | ((b2 & 0x7f) << 7));
const decoded = Student.toObject(Student.decode(bytes));
console.log('round trip :', JSON.stringify(decoded) === json);Uses the protobufjs library (npm install protobufjs) with an inline schema. Field names do not appear in the bytes, only field numbers and values. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): JSON bytes : 60 protobuf bytes : 22 protobuf hex : 0896011204417368611a037765621a046e6f64652001 tag byte : 8 = field 1 , wire type 0 varint 96 01 : 150 round trip : true
Key points
- Protobuf is a schema plus a compact binary format identified by field numbers.
- gRPC adds HTTP/2, streaming, deadlines and generated clients.
- JSON is readable and works everywhere; protobuf is smaller and strictly typed.
- Browsers need gRPC-Web or a gateway.
- gRPC suits internal services; REST suits public APIs.
Common mistakes
- Choosing gRPC for a public browser API without a gateway.
- Assuming binary is always much faster in the whole system (network and database often dominate).
- Reusing or renumbering field numbers.
- Using gRPC without deadlines.
- Ignoring load balancing (HTTP/2 keeps a long-lived connection).
Follow-ups
- How does gRPC streaming differ from WebSockets?
- What is gRPC-Web?
- How do you debug gRPC traffic?
Tips
- Say that the field number identifies the field.
- Give two strengths and two weaknesses.
- Place each protocol: internal, public.