Skip to main content
App basics: HTTP, auth, dataLesson 4 of 11

Long-Lived Connections and Streaming

A chat UI wants each token as soon as the model writes it, not one big answer 30 seconds later. Follow one answer from client → load balancer → reverse proxy → Pod → model server, compare the ways to keep a response flowing, and see what every hop in between does to a connection that stays open.

PatternRequest / response
DirectionClient → server
ConnectionShort
Client seesAll at the end
Kubernetes clusterChat UIBrowserLoad balancerIdle timeoutReverse proxyIngress / GatewayAPI Pod 1StreamsAPI Pod 2StreamsAPI Pod 3New after scale-outModel serverGenerates tokensPOST /chat
RequestResponse / reportAppears in a later step

What you will learn

  • Polling and long polling, and why they waste requests
  • SSE: one-way text/event-stream with auto reconnect
  • WebSocket and gRPC streams for two-way traffic
  • How LLM token streaming uses SSE
  • Buffering, idle timeouts, draining and uneven load at the proxies

Choosing a pattern​

PatternDirectionTransportReconnectGood for
PollingClient asksA new request every intervalEach poll is newSlow-changing status, simplest setups
Long pollingClient asks, server holdsA request held open until data or timeoutClient asks againNotifications where SSE or WebSocket is blocked
Server-Sent EventsServer → clientOne long HTTP response, text/event-streamBuilt into EventSource, resumes with Last-Event-IDLLM tokens, live feeds, progress
WebSocketBoth waysHTTP upgrade (101), then framesApp must do itChat, games, collaboration
gRPC streamingEither or bothHTTP/2 streamsApp or library retryService-to-service streams

When a stream breaks​

SymptomLikely causeFix
Whole answer appears at onceA proxy, gateway or compression layer buffersproxy_buffering off or X-Accel-Buffering: no; test each hop with curl -N
Stream stops after about a minute of silenceIdle or read timeout on one hopHeartbeats more often than the shortest timeout; raise timeouts on streaming routes
WebSocket handshake fails, no 101A proxy does not pass Upgrade and ConnectionEnable WebSocket support on that proxy or load balancer
Streams end during every deployGrace period shorter than streamsDrain on SIGTERM, longer terminationGracePeriodSeconds, client reconnect
New Pods idle after scale-outLong connections pinned to old PodsMax connection age, per-request L7 balancing, least-connections

Try it yourself​

# Watch an SSE stream arrive; -N turns off curl's own output buffering
curl -N -H 'Accept: text/event-stream' https://app.example.com/events

# Stream tokens from an OpenAI-compatible server (for example vLLM) with a timestamp per line
curl -sN http://localhost:8000/v1/chat/completions \
-H 'Content-Type: application/json' \
-d '{"model":"my-model","stream":true,"messages":[{"role":"user","content":"Count to 20"}]}' \
| while IFS= read -r line; do echo "$(date +%T) $line"; done # one timestamp for all lines = buffering

# Same request straight to a Pod, skipping the proxies, to find the buffering hop
kubectl port-forward deploy/chat-api 8080:8080

# Streaming-related timeouts on Ingresses and HTTPRoutes
kubectl get ingress -A -o yaml | grep -E 'proxy-(read|send)-timeout|proxy-buffering'
kubectl get httproute -A -o jsonpath='{range .items[*]}{.metadata.name}{" "}{.spec.rules[*].timeouts}{"\n"}{end}'

# How long a Pod gets to drain its streams
kubectl get deploy chat-api -o jsonpath='{.spec.template.spec.terminationGracePeriodSeconds}{"\n"}'

Check yourself

  1. The app sends SSE events every 50 ms, but users see the whole answer at once after 20 seconds. Calling the Pod directly with curl -N streams fine. What is most likely wrong?
  2. A model sometimes thinks for 90 seconds before the first token, and those streams always die. The load balancer has a 60-second idle timeout. What is the most robust fix?
  3. After scaling from 3 to 6 Pods, the 3 new Pods stay almost idle while the old ones are busy. Clients hold long gRPC connections. Why?

Further reading: MDN: Using server-sent events · RFC 6455: The WebSocket Protocol · gRPC core concepts · nginx proxy_buffering · Gateway API HTTPRoute timeouts · HTTP/1.1, HTTP/2 and HTTP/3 · Reverse Proxies and Load Balancing · From API Request to Response