Skip to the page

gRPC, real-time APIs and webhooks · mid

gRPC calls and Protobuf

gRPC sends Protobuf messages over HTTP/2 with a typed contract. It has four kinds of call, and its status codes and trailers replace HTTP status codes.

The notes

What you must know

  • Four call kinds: unary (one request, one response), server streaming (one request, many responses), client streaming (many requests, one response), bidirectional streaming (both sides send independently).
  • On the wire each call is an HTTP/2 stream on a long-lived connection. Many calls share one connection (multiplexing). Messages are length-prefixed Protobuf frames, and the call ends with trailers that carry grpc-status and grpc-message.
  • Errors are status codes such as OK, INVALID_ARGUMENT, NOT_FOUND, DEADLINE_EXCEEDED and UNAVAILABLE, not HTTP codes. UNAVAILABLE is the usual code to retry.
  • In the .proto file: syntax and package (with a version, such as delivery.orders.v1), a service with rpc methods, and messages with numbered fields. Use one request message and one response message per method so you can add fields later.
  • The first value of every enum must be 0 and named like ORDER_STATUS_UNSPECIFIED, because 0 is the default when a field is missing.
  • Use google.protobuf.Timestamp for times, int64 for money in paise, and repeated for lists.
  • A browser cannot make native gRPC calls because fetch does not expose HTTP/2 trailers and framing. Options: gRPC-Web with a proxy (unary and server streaming only), Connect (serves gRPC, gRPC-Web and a simple HTTP protocol), or transcoding to REST and JSON with grpc-gateway or Envoy.

The red pen

Common traps: mistake → fix

  • Mistake: gRPC is just faster REST

    Fix: It is a different contract style: a typed schema, streaming, deadlines and generated clients, over HTTP/2.

  • Mistake: Browsers can call gRPC directly

    Fix: They need gRPC-Web with a proxy, or Connect, or a REST gateway.

  • Mistake: I can use the same message for all methods to save time

    Fix: Separate request and response messages let you add fields without changing signatures.

Easy to hard

Questions interviewers ask, easy to hard

  1. Name the four kinds of gRPC call and give an example of each.
  2. What carries the error in a gRPC response?
  3. Why must an enum start with an UNSPECIFIED value 0?
  4. Why can a browser not call a gRPC service directly, and what can you do?
  5. Write the proto for live order tracking: get one order, list orders, and watch status changes.

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview