TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About Β· Labs Β· Cheat sheets Β· Learning paths

← Back to gRPC and Protocol Buffers

🎧 gRPC and Protocol Buffers: cheat sheet

🎨 The analogyWalkie-talkies with a shared codebook of numbered phrases, so messages are tiny and both sides can talk at once.
πŸŽ“ The regular explanationA contract-first RPC framework. Services and messages are defined in .proto files, encoded as compact Protocol Buffers binary, and called through generated stubs over HTTP/2, with unary and streaming calls, deadlines and status codes.
One contract, generated code on both sidesStep 4 of 4
πŸ“– Codebookquestions + answersπŸ‘©MiaπŸ—’οΈ Mia's pocket cardπŸ“‹ Sam's answer sheetπŸ§‘β€πŸ”§Sam
  1. Before the first shift, Mia and Sam write one codebook together. It lists every question Mia may ask and what each answer looks like.
  2. From the codebook, each of them gets a printed card. Mia's card turns her questions into codes; Sam's sheet turns codes back into questions.
  3. Mia just asks "how many apples?". Her card sends it as short codes over the radio, and Sam's sheet turns it back into the question.
  4. Sam answers "twelve", and the reply comes back the same way, as codes, until Mia hears "twelve".
Field numbers on the wireStep 4 of 4
First message1Β·akey item = 1: 1Β·apples02Β·1key quantity = 2: 2Β·1213Β·gkey gift_wrap = 3 (new field): 3Β·gift23Later message Β· reserved 21Β·akey item = 1: 1Β·apples02Β·9key price = 9, reusing 2: 2Β·914Β·9key price = 4: 4Β·9✦✦✦✦✦23πŸ‘©Mia (new codebook)πŸ‘¨Leo (old codebook)skips 4
  1. Mia sends "one, apples; two, twelve". Only the numbers and the values go over the radio, never the words "item" or "how many".
  2. Mia's new codebook adds "three, gift wrap". Leo has the old codebook, doesn't know "three", ignores it, and still gets the rest right.
  3. Later someone crosses out "two, how many" and reuses "two" for the price. Leo hears "two, nine" and thinks Mia wants nine apples.
  4. So crossed-out numbers stay crossed out for good. The price gets a brand-new number, four, and Leo just ignores it.
Four call types on one HTTP/2 connectionStep 4 of 4
one channelπŸ‘©MiaπŸ§‘β€πŸ”§Samboth at once
  1. Mia and Sam tune to one channel and keep it. First, the simplest way to talk, one question and one answer.
  2. Mia asks once, "read me today's orders", and Sam reads them out one by one until he's done.
  3. Now the other way round. Mia reads out every item she scanned, and Sam answers once at the end, "got all three".
  4. Finally, both talk at once. Mia and Sam chat freely on the same channel, and each speaks whenever they like.
Deadlines, cancellation and status codesStep 4 of 4
πŸ‘©Miaretry laterπŸ§‘β€πŸ”§Samβœ• βœ•πŸ’«Kim in the yard
  1. Mia asks Sam to put an order aside, with a time limit of two minutes and a word about who she is. Sam asks Kim and passes on the time that's left.
  2. Kim is slow today. The two minutes run out, so Mia stops waiting and gets the sign-off code "too late".
  3. Mia's "never mind" travels down the line, so Sam and Kim stop working on an order nobody is waiting for.
  4. Next time Kim's radio is switched off. Sam gets no answer and signs off to Mia with "nobody's there", so she tries again a little later.
Long-lived connections and load balancingStep 4 of 4
πŸ‘©MiaπŸŽ›οΈChannel switchper callπŸ§‘β€πŸ”§Samβœ• βœ•πŸ’«KimπŸ§‘β€πŸ­Lee
  1. A channel switch by the stockroom door connects Mia's radio to Sam when she tunes in, and every question goes to Sam.
  2. Two more helpers start work, but Mia's radio stays tuned to Sam. He's swamped while Kim and Lee stand around.
  3. A dispatcher replaces the switch. It listens to each question and hands it to the next free helper, so the work is shared.
  4. When Kim goes on a break, the dispatcher simply stops giving her questions, and Sam and Lee carry on.
grpc-java blocking stub and serviceStep 3 of 3
πŸ‘©Miano pearsπŸ“»Radio channelπŸ§‘β€πŸ”§Samanswer sheet
  1. Mia tunes in once and keeps the same channel all day, and Sam has his answer sheet ready.
  2. Mia sets a two-minute limit and asks for twelve apples. Sam writes down the order and answers with its number.
  3. Mia asks for pears, which the shop doesn't sell. Sam signs off with "we don't have that", and Mia's card tells her exactly that.

🧩 Key questions

Mia and Sam stop using "two, how many". What should happen to the number two?
Cross it out for good, so an old codebook never misreads it. Old codebooks still think two means how many, so a new meaning for two would be misread.
Mia adds "three, gift wrap" to her codebook. What happens when Leo, with the old codebook, hears it?
He ignores three and understands the rest. New parts get new numbers, so old and new codebooks can still talk to each other.
Mia gave Sam two minutes. Sam had to ask Kim, and the time ran out. What happens?
Mia hears "too late", and her "never mind" reaches Sam and Kim so they stop. Nobody keeps working on something nobody's waiting for, but work already done isn't undone.
The shop hires two more helpers, but every question still goes to Sam. Why?
Mia's radio was tuned to Sam once, and every question goes down that one channel. A dispatcher who hands out each question, or a radio that takes turns, shares the work.