Unit 1 of 13 · about 4 hours · explain, implement

Request, response and streaming state machines, and textual versus binary encoding

Every later guarantee in this track crosses an RPC boundary. If the call states and the encoding contract are vague, retries, versioning and tracing have nothing to attach to.

You'll be able to

  • Explain the states of a unary call and of each direction of a streaming call, including half-close and cancellation.
  • Contrast textual and binary encodings on size, schema dependence and human readability using measured byte counts.
  • Implement a unary request-response exchange with structured errors over a byte transport.

Covers

Request, response and streaming state machinesTextual versus binary encoding trade-offs
1

Read

In this order, only the parts listed

0 of 3 done

  1. DDIA 2e Chapter 5: services and encoding sizesDDIA 2e - Chapter 5 Encoding and Evolution

    Chapter 5, Dataflow Through Services: REST and RPC (pp. 180-186) for service, IDL and rolling-upgrade framing; Formats for Encoding Data (pp. 163-169) for the worked sizes: JSON 81 bytes, binary JSON 66 bytes, Protocol Buffers 33 bytes

  2. gRPC core concepts: unary and streaming lifecyclesgRPC Guides - Deadlines, Cancellation, Status Codes, Retry and Flow Control

    RPC life cycle: unary call versus server, client and bidirectional streaming; how one request maps to one response or to independent message streams

  3. Proto3 scalar types and binary wire shapeProtocol Buffers and Avro - Unknown Fields and Schema Resolution

    Scalar Value Types table and Assigning Field Numbers: field numbers 1 to 15 take one byte on the wire; numbers 19000 to 19999 are reserved

2

Focus on

  • A unary call has one terminal state per side. A streaming direction closes independently, so one side can finish sending while still receiving.
  • Textual JSON carries field names in every message. Binary schema encodings omit names and rely on shared field numbers or positional schemas, which is where the byte savings come from.
  • Services expose only predetermined inputs and outputs, unlike databases with ad hoc queries. The IDL plus the framework owns routing, metrics and auth plumbing, not the business handler.
  • Rolling upgrades mean old and new clients and servers coexist. The encoding decision is also a compatibility decision, finished in the next unit.
Common mistakes · 3
  • Calling a streaming RPC a single request and response. Each direction has its own open, half-closed and closed states and they change independently.
  • Reading compactness as the whole argument for binary encodings. The schema is also documentation and a compatibility contract; size is only one term.
  • Assuming field numbers are free metadata. On the wire the number identifies the field, so changing or reusing one changes the meaning of stored bytes.
3

Try it yourself

Closed-book, before checking anything

Without notes, draw the unary state machine and each direction of a bidirectional stream, then explain the DDIA byte counts: JSON 81 bytes, binary JSON 66 bytes and Protocol Buffers 33 bytes for the same record, and why the schema lets the binary form drop field names.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.A client finishes sending on a bidirectional stream but keeps receiving. Which states changed?Answer

    Only the client-to-server direction moved to half-closed. The server-to-client direction stays open until the server finishes, and cancellation can still end both.

  2. 2.Why is the Protocol Buffers encoding of the DDIA example 33 bytes while JSON needs 81?Answer

    JSON repeats field names and punctuation in every message. Protocol Buffers replaces each name with a numeric field tag from the shared schema and adds compact type and length bytes, so names never travel with the data.

  3. 3.Why do field numbers 1 to 15 cost one byte while higher numbers cost more?Answer

    The number is encoded on the wire with the type tag. Small numbers fit the short varint form, so frequently set fields should take numbers 1 to 15.

  4. 4.Two versions of a service run at once during a rollout. Why must the encoding be compatible across versions?Answer

    Old clients talk to new servers and new clients talk to old servers until the rollout finishes. An incompatible change corrupts or drops fields on those mixed paths.

5

Practice

You're done when

You can draw both state machines from memory, explain the three DDIA byte counts, and your local unary exchange returns a structured error for unknown methods and malformed payloads.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 13 more in the section
  • DDIA 2e - Chapter 5 Encoding and EvolutionMartin Kleppmann and Chris Riccomini · primary

    Read formats, schema evolution, service RPC, durable workflows and event-driven dataflow. Write a four-version compatibility table and connect each compatibility claim to a wire fixture.

  • gRPC Guides - Deadlines, Cancellation, Status Codes, Retry and Flow ControlgRPC Authors · required

    Read the deadlines, cancellation, status codes, retry (enabled by default with no default policy; no retry once response headers arrive), keepalive and flow-control guides. Turn each into a rule your own RPC layer follows, and check a claim against grpcio before you rely on it.

  • Protocol Buffers and Avro - Unknown Fields and Schema ResolutionGoogle and the Apache Software Foundation · required

    Read the proto3 guide on field numbers, defaults and unknown fields (kept through binary parsing and reserialization, lost through JSON) and the Avro 1.12.0 schema resolution rules (https://avro.apache.org/docs/1.12.0/specification/). Build round-trip fixtures that show each rule.

Also in this section

  • Building Distributed Systems - The MessengerBuild Distributed Systems · support

    Track 1 is free: do the RPC subtrack (sync RPC with timeout, timeout and retry loop, async callbacks, callback reaper, exponential backoff) and the deduplication task. Tasks are graded in the browser after login, so treat a pass as a drill and keep your local harness as the evidence.

  • MIT 6.5840 - RPC and Threads plus Lab 2 Key/Value ServerMIT PDOS · required

    Read the Lecture 2 notes (notes/l-rpc.txt and notes/kv.go) on RPC failure and at-most-once execution, then do Lab 2: a versioned Put key/value server whose client returns ErrMaybe when a retried Put may already have run, and a lock built on it. Keep the course tests, then add cancellation, size-limit and mixed-version faults in your own harness.

  • DSLabs - Lab 1 Exactly-Once RPCUniversity of Washington (Ellis Michael) · support

    Labs are Java 21 message-passing state machines tested on an emulated network and by model-checking search tests that print a minimal violating trace; run them with the run-tests.py script and its --lab and --part options. Lab 1 builds an "exactly-once" RPC abstraction for an unreplicated key/value server on a network that drops, duplicates and reorders messages. Use it to check your request identity and duplicate-suppression design against exhaustive orderings.

  • HLD Handbook - Networking, API Design and BackpressureHLD Handbook · required

    Read only Part 0 networking and API design, then the load balancer, service discovery, rate limiting and reliability chapters needed for the protocol design. Use the diagrams to review boundaries, not as a substitute for fault tests.

  • AWS Builders' Library - Timeouts, Retries and Backoff with JitterAmazon Web Services · required

    Read the whole article for timeout choice, retries at one layer, retry amplification across hops, capped exponential backoff and jitter. Derive the request multiplier for retries at every layer of a call chain before writing your retry policy.

  • AWS Builders' Library - Making Retries Safe with Idempotent APIsAmazon Web Services · support

    Read the caller-provided client request identifier pattern, the validation error for a repeated identifier with different parameters, the semantically equivalent response to a retry and why the identifier is retained only for a bounded interval. Compare that interval with your own duplicate-suppression state.

  • Building Distributed Systems - The NetworkerBuild Distributed Systems · optional

    Only the first three tasks are free (TCP echo, connection pool and backlog, graceful shutdown and drain). Tasks are graded in the browser after login, so treat a pass as a drill and keep your local harness as the evidence.

  • Beej's Guide to Network ProgrammingBrian Hall · support

    Use the client, server, send, receive, select or poll, and partial-transfer sections when socket behavior is unclear. This is a protocol mechanics reference, not the distributed-systems spine.

  • AlgoMaster System Design - Selected Networking and API ProblemsAlgoMaster · optional

    Use the problems on idempotency and retries (Deduplicate Idempotent Requests, Build an Idempotent Consumer, Classify Retry Outcomes), rate limiting (sliding-window log and token bucket), the Least Requests load balancer and the queue visibility timeout. For every solved component, add an external fault test and explain what its simplified judge does not establish.

  • Replicated Log and Stream Processor - Protocol SliceRoadmap-Final · support

    Create the repository, message schema, request and node identities, deadline and retry contract, deterministic network scheduler and raw event format. Keep this same artifact through DS-9.

  • RFC 9000 - QUIC Version 1IETF · reference

    Read Section 2 (streams), Section 4 (flow control) and Section 9 (connection migration) for QUIC transport behavior. No implementation; compare with TCP behavior from the Systems stage.

  • RFC 9114 - HTTP/3IETF · reference

    Read Sections 3 (connection setup), 6 (stream mapping) and 7 (framing) for how HTTP semantics map onto QUIC streams.

  • W3C Trace ContextW3C · reference

    Read Section 3.2 (traceparent fields), Section 3.3 (tracestate) and Section 4 (processing model) for trace correlation without synchronized clocks.