Skip to content

Lesson 4 of 9 · 27m

Streaming Patterns That Do Not Eat Servers

Choose unary, client, server, or bidirectional streaming based on ownership and backpressure, then bound streams before they pin memory and connections forever.

Streaming is the feature that makes teams choose gRPC and the feature that makes them regret it. A stream is not a faster unary call; it is a long-lived state machine sharing memory, goroutines or threads, connection capacity, and failure semantics. Use it when continuity is the point, not because large JSON makes you nervous.

Know the four shapes

rpc GetInvoice(GetInvoiceRequest) returns (Invoice);
rpc UploadUsageBatch(stream UsageEvent) returns (UploadSummary);
rpc WatchInvoices(WatchRequest) returns (stream InvoiceEvent);
rpc OpenTerminal(stream TerminalInput) returns (stream TerminalOutput);

Unary fits independent operations. Client streaming fits ingestion. Server streaming fits subscriptions and large ordered reads. Bidirectional streaming fits genuinely interactive protocols. If one side is only streaming because the payload is big, ask whether object storage or pagination is cleaner.

Backpressure is the real design constraint

Streams fail when production outpaces consumption:

async def watch_events(request, context):
    async for event in queue:
        if context.cancelled():
            return
        yield event

That example hides the hard part: where does queue buffer? If the client slows down, do events accumulate in memory? Are they dropped? Is the sequence resumable? A stream without an overflow policy is a memory leak with a networking API.

Bound every stream

Production streams need explicit limits:

maximum lifetime                 reconnect and rebalance
maximum message size             parser and memory protection
maximum in-flight messages       backpressure control
idle timeout                     detect dead consumers
event sequence or resume token   recovery without duplication chaos

Infinite subscriptions sound elegant until one bad client pins a server worker for three weeks.

Server streaming is not pub-sub

WatchInvoices can notify changes, but gRPC does not inherently provide durable consumer offsets, replay, dead-lettering, or fan-out storage. If clients need reliable event processing, put Kafka, NATS, or a durable log behind the stream and make the stream an edge protocol.

Client streaming needs transactional clarity

If a client uploads 100,000 events and the stream breaks at 92,000, what happened? Choose and document one:

all-or-nothing commit at stream completion
incremental commit with idempotency keys
resume token plus server-side deduplication

Do not leave clients guessing. They will retry, and retries double-write whatever the first attempt actually committed.

Bidirectional streams are protocols

Once both sides can speak at any time, you need message correlation, ordering rules, close semantics, and state diagrams. Treat it like protocol design, not method invocation. If there is no interactive requirement, separate streams or unary RPCs are easier to operate.

Field notes

  • Long streams interact badly with naive load balancing; they concentrate connections and resist draining.
  • A mobile client that disappears usually leaves no clean close frame; idle detection matters.
  • Use flow control defaults from mature gRPC libraries before inventing application-level throttles.
  • Measure stream lifetime, active streams, sent/received messages, and drops per stream type.
  • If every stream starts with SubscribeAll, you are building an accidental message broker.

Was this lesson helpful?

Sign in to rate