System Design Academy

Saving…

Lesson 8 of 10 in Module 10 of 54 lessons complete

gRPC & Protocol Buffers

Module 1, lesson 8Concept10 min

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.
  1. Write the contract

    Declare messages, field numbers and service methods in a .proto file.

  2. Generate code

    Generate matching client and server code in each team's language.

  3. Make the call

    The client sends the encoded message over HTTP/2. The server reads it and runs the requested method.

The contract defines a unary routing call and its request and reply.
Protobuf
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;
}
Client-call fragment in a compilable teaching harness; local stand-ins replace generated gRPC types and omit transport setup.
Go
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.

One contract generates bindings for clients and servers in different languages.

A proto file feeds code generation. The generated code serves a Go server and a TypeScript client.

Call typeMessage flowExample
UnaryOne request, one responseRoute one bag
Server streamingOne request, many repliesWatch routing updates
Client streamingMany requests, one final replyUpload a batch of scans
Bidirectional streamingMany messages both waysContinuous 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.

Explicit integer fields prevent accidental floating-point prices.
Protobuf
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.

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

A game backend has 40 internal services calling each other constantly. Best fit for those internal calls?
What does the .proto file give you?

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.

Try the quick check above whenever you feel ready.

Previous: APIs & REST