Lab 11: gRPC Node.js

Time: 60 minutes | Level: Architect | Docker: docker run -it --rm node:20-alpine sh

gRPC is the backbone of microservice communication. This lab covers loading .proto files at runtime, all four RPC patterns (unary, server-streaming, client-streaming, bidirectional), interceptors, and deadlines.


Step 1: gRPC Architecture

Client                          Server
  │                               │
  ├──── Unary RPC ───────────────→│  req/res (like HTTP)
  │                               │
  ├──── Server Streaming ────────→│  one req, stream of responses
  │←──────────────────────────────│
  │                               │
  ├──── Client Streaming ────────→│  stream of requests, one response
  │←──────────────────────────────│
  │                               │
  ├──── Bidirectional ───────────→│  full-duplex streaming
  │←──────────────────────────────│

Transport: HTTP/2 (multiplexed, binary framing, header compression) Serialization: Protocol Buffers (3-10x smaller than JSON)


Step 2: Install Dependencies


Step 3: Define the Proto File


Step 4: gRPC Server Implementation


Step 5: gRPC Client — Unary Call


Step 6: Complete Server + Client Test

📸 Verified Output:


Step 7: Interceptors


Step 8: Capstone — Resilient gRPC Client


Summary

Pattern
Direction
Use Case

Unary RPC

req → res

Standard API call

Server streaming

req → stream

Real-time feeds, logs

Client streaming

stream → res

File upload, batch ingest

Bidirectional

stream ↔ stream

Chat, real-time sync

Interceptors

Both

Auth, logging, metrics

Deadlines

Client

Prevent hanging calls

Status codes

Both

Error classification

Retry + backoff

Client

Transient failure recovery

Last updated