पाठ 10 / 25

Server Streaming

Push updates to a client.

One request, many responses

In server streaming, the client sends one request and reads responses until the server finishes. It suits live updates (order status, prices), large result sets sent in chunks, and progress reporting. The server writes messages as they become available; HTTP/2 flow control slows the server down if the client reads slowly. Servers should check whether the client cancelled and stop work promptly.

Messages over time

Streaming RPCs send sequences of messages over one call, with built-in flow control.

Three ideas: server streaming, client streaming, bidirectional streaming.
Figure 4.1 — Server, client and bidirectional streams.

Streaming order events in Python

Server generator and client loop (a sketch).

# server: yield messages; the stream ends when the generator returns
def WatchOrder(self, request, context):
    for event in events.subscribe(request.id):
        if not context.is_active():          # client cancelled or deadline passed
            break
        yield order_pb2.OrderEvent(order_id=request.id, status=event.status)

# client: iterate over responses
for event in stub.WatchOrder(order_pb2.WatchOrderRequest(id="o-1001"), timeout=300):
    print(event.status)

Send periodic heartbeats for long streams

Idle connections may be closed by proxies; keepalive settings or small heartbeat messages keep long streams alive.

त्वरित जाँच: What slows a fast server when the client reads a stream slowly?

  • HTTP/2 flow control
  • TLS
  • Protobuf compression
  • DNS caching
Answer

HTTP/2 flow control — Built-in back-pressure.