awesome-everything RU
↑ Back to the climb

APIs

gRPC and Protobuf: schema and proto reading

Crux Read real .proto snippets — field-tag edits, reserved, streaming signatures — and predict the wire-level behaviour or the highest-leverage fix.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

A protobuf schema is a contract you read at code-review time, before the corruption ships. Read each diff or signature, work out what it does on the wire, and pick the change a senior would actually approve.

Goal

Practise the review you run on every proto PR: spot the field-tag traps, confirm a removal is reserved correctly, and read a streaming signature for the call shape it really declares.

Snippet 1 — the field-tag diff

 message Order {
   string id = 1;
   int64 amount_cents = 2;
-  int32 discount_pct = 3;
-  Priority priority = 4;
+  Priority priority = 3;
+  Coupon coupon = 4;
 }
Quiz

An old client wrote an Order with priority in field 4. A service deployed with this new schema reads that message. What does it see, and why?

Snippet 2 — the removal

message Customer {
  reserved 2, 5 to 7;
  reserved "email", "legacy_tier";
  string id = 1;
  string display_name = 3;
  Address address = 8;
}
Quiz

A new field is proposed: add `string email = 2;` back, because the team wants email again. Does this compile, and is it the right move?

Snippet 3 — the streaming signatures

service ChatService {
  rpc History(HistoryRequest) returns (stream Message);
  rpc Send(stream Message) returns (SendSummaryResponse);
  rpc Live(stream Message) returns (stream Message);
}
Quiz

Map each RPC to its call shape and the position of the stream keyword.

Snippet 4 — proto3 presence

message UpdateProfile {
  string user_id = 1;
  string bio = 2;            // plain scalar
  optional bool marketing_opt_in = 3;  // explicit presence
}
Quiz

A client sends UpdateProfile with bio set to the empty string and marketing_opt_in omitted. The server wants to PATCH only the fields the client actually meant to change. What does the schema let it distinguish?

Recap

Reviewing protobuf is reading numbers, not names: a renumber diff silently steers old values into the wrong field, so evolution must be append-only with reserved guarding removals (and protoc enforces the reservation). A streaming signature is read by which side carries stream — request for client, response for server, both for bidi. And proto3 presence is real but opt-in: plain scalars cannot tell unset from default, so PATCH-style APIs reach for optional or a FieldMask. Catch these at review time, before the corruption deploys.

Continue the climb ↑gRPC and Protobuf: build a contract that survives evolution
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources2
expand
  1. 01
  2. 02

Trademarks belong to their respective owners. Editorial reference only.