gRPC & Protocol Buffers
Definition
Remote procedure call
- In plain English
- A remote procedure call lets a program run a function on another machine as if it were local.
- Think of it like
- It is like phoning a colleague to do a task and waiting on the line for the answer.
gRPC
- In plain English
- gRPC is a framework for fast remote procedure calls between services using a strict, shared contract.
- Think of it like
- It is like a pre-printed order pad shared by kitchen and waiters, so orders are compact and never ambiguous.
Why it exists
Two services exchange a price. One sends text; the other expects a number. That mismatch breaks requests. Repeatedly converting text also uses network space and processor time.
A shared message format defines each field's type before release. gRPC generates code to read and write that format.
Streaming
- In plain English
- Streaming means sending a sequence of messages over one call rather than only one request and one reply.
- Think of it like
- It is like a live commentary instead of one final score.
How it works
Protocol Buffers
- In plain English
- Protocol Buffers is a compact binary format whose message shapes are defined in a schema file.
- Think of it like
- It is like a shipping manifest with numbered boxes instead of long written descriptions.
HTTP/2
- In plain English
- HTTP/2 is a version of HTTP that carries multiple concurrent request streams over one connection.
- Think of it like
- It is like a multi-lane road instead of a single lane.
- Write the contract
Declare messages, field numbers and service methods in a .proto file.
- Generate code
Generate matching client and server code in each team's language.
- Make the call
The client sends the encoded message over HTTP/2. The server reads it and runs the requested method.
syntax = "proto3";
package baggage;
service BaggageRouter {
rpc RouteBag (BagScan) returns (RouteReply);
}
message BagScan {
string bag_id = 1;
string scanner_id = 2;
}
message RouteReply {
int32 belt = 1;
}package main
import "context"
type BagScan struct{ BagId string }
type RouteReply struct{ Belt int32 }
type BaggageRouterClient interface {
RouteBag(context.Context, *BagScan) (*RouteReply, error)
}
func route(ctx context.Context, client BaggageRouterClient) (*RouteReply, error) {
return client.RouteBag(ctx, &BagScan{BagId: "B42"})
}
func main() {}A proto file feeds code generation. The generated code serves a Go server and a TypeScript client.
| Call type | Message flow | Example |
|---|---|---|
| Unary | One request, one response | Route one bag |
| Server streaming | One request, many replies | Watch routing updates |
| Client streaming | Many requests, one final reply | Upload a batch of scans |
| Bidirectional streaming | Many messages both ways | Continuous scans and instructions |
Examples
Use integer prices in orders
An order gateway sends prices to a trade-matching service. Every microsecond matters.
For a currency divided into cents, use int64 cents for prices. Agree on the currency and units. Whole-number cents avoid rounding mistakes from storing fractions in binary.
syntax = "proto3";
message Order {
string symbol = 1;
int64 price_cents = 2;
int64 quantity = 3;
}The insight
Agreed field types catch mistakes before services exchange messages.
Stream baggage scans and instructions
Airport scanners continuously send bag-scan events to a routing service.
Use client streaming for scans with one final summary. Use two-way streaming for instructions after each scan. Router-to-belt commands form a separate conversation.
The scanner starts a stream and reports bag B42. The router tells the belt to divert B42 to belt 7 and replies to the scanner.
The insight
Streaming fits continuous event flows.
Keep the weather API browser-friendly
Your weather platform serves browsers and calls internal forecasting services.
Browsers call a REST forecast API. A gateway translates those requests into internal gRPC calls. Direct browser access needs gRPC-Web and a compatible gateway that forwards requests.
The browser sends REST requests to a gateway. The gateway sends gRPC requests to the forecast service.
The insight
One system often uses both.
Trade-offs
Pros
- Use compact binary messages.
- Generate matching client and server code in many languages.
- Support streaming calls.
Cons
- Messages need tools to inspect.
- Browsers need extra translation tools.
- Keep generated code compatible as messages change.
Use when
- Internal services call frequently.
- Teams share contracts across languages.
Avoid when
- Public clients need ordinary HTTP tooling.
- A prototype gains little from code generation.
Quick check
Takeaways
- gRPC sends typed messages using generated client and server code.
- The .proto file is the contract.
- A common mix uses REST for public clients and gRPC between internal services.