LearnThatStack Ace your next interview
API Design · question
Question 62 of 92

What is gRPC and when would you use it?

intermediate Pro
← All API Design questions

The full answer covers the core mechanics, when this pattern wins, and the trade-offs interviewers most often probe. It includes a code sample. Read it once at your own pace, then try to recall the structure from memory before the interview. Pro also includes this question's interview lens - the likely follow-up probes and what you can say in the room.

Full answer + code samples + AI explanations

Unlock to read the complete answer for this premium question.

Guided practice for API Design One question at a time. Answer out loud, get graded, see what you missed.
Related concept

Who controls the response shape

In REST the server decides the response shape, in GraphQL the client builds it, and in gRPC a compiled contract sets it. The trade-offs come from who decides the shape.

For this question · chapter 3 of 3

3/3 The contract compiles the call
grpcThe contract compiles the callfast inside, awkward at the edgebytes / call22connections1styles in use13protobuf over HTTP/2, one connectionone system, three answersthe interview answerclientsa browsera phonea partnerthe edgeREST the public APIGraphQL the app screensyour servicesordersproductsbilling22 bytes a callREST or GraphQLthe server decides,or the client composesgRPCthe contract compileseach one where its cost is cheapest, and none of them everywherehow to chooseall threepublic API, REST, because anything can call it and a CDN can cache itmany screens or one graph, GraphQL, because the client owns the shapeservice to service, gRPC, because the contract compiles and the wire is small
grpcThe contract compiles the callbytes / call22connections1styles in use13one system, three answersthe interview answerbrowser, phone, partnerthe edgeREST for the public APIGraphQL for the appyour servicesorders, products, billing22 bytes a callREST or GraphQLgRPChow to chooseall threepublic API, RESTmany screens, GraphQLservice to service, gRPC

The contract compiles the call

  1. In gRPC, a file checked into the repository beside the code decides the response shape. That file, orders.proto, names one service, one method, and the message the server returns. The number after each field in the message is what gets sent over the network. So once a field has the number 2, that field must keep the number 2 forever.
  2. In gRPC, a mismatch between client and server is now a compile error. The compiler writes the client and server stubs from one file, so nobody types them by hand and they cannot get out of sync. Rename product_name in the file, and both projects stop compiling. A REST client would have found the same mistake in production, when a user reported a 400 error.
  3. gRPC is fast for two reasons, and both matter. First, gRPC encodes messages with protobuf, a binary format that writes the field number 2 where JSON writes the text total_cents. So the same three fields take 22 bytes instead of 61. Second, HTTP/2 sends the four calls over one connection instead of four separate connections, so no call waits in a queue behind another call.
  4. The gRPC contract file covers more than the fields. It also decides how many messages each call sends. A REST endpoint gives you only one of the four call shapes, called unary: one message out and one back. The other three keep the stream open: one out and many back, many out and one back, or both directions at once. With REST, you build those three yourself.
  5. gRPC has a cost in the browser, because the browser cannot make a gRPC call on its own. The browser's fetch API cannot set the HTTP/2 frames that gRPC needs. So a grpc-web proxy between the browser and the server has to translate each call. The browser's network tab shows 22 bytes of binary, not a body you can read. This cost is why gRPC is used inside a system.
  6. No single choice of who decides the shape fits every caller, so a real system runs REST, GraphQL and gRPC together. REST runs at the public edge, because any client can call a REST endpoint and a CDN can cache REST responses. GraphQL runs where many screens each want their own response shape, and you would rather not deploy again for each screen. gRPC runs between your own services, where both ends are your code and the contract compiles.
Chapter 3 · step 1 of 6

In gRPC, a file checked into the repository beside the code decides the response shape. That file, orders.proto, names one service, one method, and the message the server returns. The number after each field in the message is what gets sent over the network. So once a field has the number 2, that field must keep the number 2 forever.

1 of 6 Use ← → or swipe See the concept: all 3 chapters in it

© LearnThatStack - diagrams may not be republished without permission.

Want a quick review of the fundamentals? See the API Design cheatsheet.

← Back to all API Design questions
Pro · $10/mo

74 of 92 API Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime