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

Authentication, authorization, service discovery and gRPC interceptors

An RPC that reaches business logic unauthenticated has already failed. Identity, discovery and cross-cutting policy belong in the path before dispatch, not inside each handler.

You'll be able to

  • Explain channel versus call credentials and why auth runs before application dispatch.
  • Propagate caller identity through metadata and enforce per-method authorization in an interceptor.
  • Explain discovery staleness and generation changes, and where interceptors stop being the right tool.

Covers

Authentication before application dispatchAuthorization and identity propagationService discovery and endpoint generation changesProduction gRPC interceptors and reflection
1

Read

In this order, only the parts listed

0 of 3 done

  1. gRPC authentication guidegRPC Guides - Deadlines, Cancellation, Status Codes, Retry and Flow Control

    Supported mechanisms and Credential types: channel credentials such as SslCredentials versus call credentials, composite credentials sending a token per call, server-side token verification in an interceptor

  2. gRPC interceptors guidegRPC Guides - Deadlines, Cancellation, Status Codes, Retry and Flow Control

    When to Use Interceptors: metadata, logging, fault injection, caching, metrics, policy, server-side auth and authz; client versus server interceptors and order effects

  3. HLD service discovery and API designHLD Handbook - Networking, API Design and Backpressure

    Service Discovery and Service Mesh: DNS with TTL, registry, Kubernetes endpoints, sidecars; plus the API Design Basics chapter for auth, versioning and idempotency-key placement

2

Focus on

  • Authenticate the channel, authorize the call. Mutual TLS proves which workload dialed (plain TLS proves only the server), while per-call tokens plus a server interceptor decide which methods that caller may invoke.
  • Identity travels in metadata, and the interceptor is the enforcement point. Handlers behind it assume a verified caller and never re-parse tokens.
  • Discovery data is stale by design: TTLs, caches and propagation delays mean the client routes on a recent generation, so servers must reject misrouted requests with an explicit error.
  • The Systems unit on identity, authorization, least privilege and secrets taught the credential mechanics. Here the new work is the RPC path: credentials to metadata to interceptor to handler.
Common mistakes · 3
  • Checking auth inside each handler. Handlers drift, so one forgotten method stays open; enforce once in the interceptor before dispatch.
  • Sending call credentials on an insecure channel. Most implementations refuse, and tokens on plaintext are stealable; compose call credentials over TLS channel credentials.
  • Treating discovery as exact. Endpoints change under you, so clients must handle wrong-generation redirects and servers must validate ownership on arrival.
3

Try it yourself

Closed-book, before checking anything

From a blank file, add a server interceptor that verifies a bearer token, attaches the caller ID to the call context, rejects one unauthorized method with PERMISSION_DENIED, and logs the decision; then show a stale-endpoint retry against a bumped generation.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Where should token verification live, and why not in each handler?Answer

    In a server interceptor before dispatch. One path verifies, propagates identity and enforces policy, so handlers cannot forget or disagree.

  2. 2.A client uses TLS but no call credentials. What is proven and what is missing?Answer

    The server identity and channel privacy are proven. Caller identity and per-method permission are missing, so the server must deny protected methods with UNAUTHENTICATED or PERMISSION_DENIED.

  3. 3.Discovery says the shard moved but the client still dials the old owner. What should happen?Answer

    The old owner rejects with a versioned wrong-target error naming the new generation. The client refreshes discovery and retries once against the new owner.

  4. 4.What can interceptors not do?Answer

    Manage connections, ports or TLS setup. They run per call between application and network, so transport configuration stays outside them.

You're done when

Unauthenticated calls never reach handlers, one policy test denies the right method, and a stale endpoint produces a versioned redirect rather than a silent wrong write.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 14 more in the section
  • 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.

  • 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.

Also in this 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.

  • 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.

  • 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.

  • 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.