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
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
| Pattern | Direction | Transport | Reconnect | Good for |
|---|---|---|---|---|
| Polling | Client asks | A new request every interval | Each poll is new | Slow-changing status, simplest setups |
| Long polling | Client asks, server holds | A request held open until data or timeout | Client asks again | Notifications where SSE or WebSocket is blocked |
| Server-Sent Events | Server → client | One long HTTP response, text/event-stream | Built into EventSource, resumes with Last-Event-ID | LLM tokens, live feeds, progress |
| WebSocket | Both ways | HTTP upgrade (101), then frames | App must do it | Chat, games, collaboration |
| gRPC streaming | Either or both | HTTP/2 streams | App or library retry | Service-to-service streams |
When a stream breaks
| Symptom | Likely cause | Fix |
|---|---|---|
| Whole answer appears at once | A proxy, gateway or compression layer buffers | proxy_buffering off or X-Accel-Buffering: no; test each hop with curl -N |
| Stream stops after about a minute of silence | Idle or read timeout on one hop | Heartbeats more often than the shortest timeout; raise timeouts on streaming routes |
WebSocket handshake fails, no 101 | A proxy does not pass Upgrade and Connection | Enable WebSocket support on that proxy or load balancer |
| Streams end during every deploy | Grace period shorter than streams | Drain on SIGTERM, longer terminationGracePeriodSeconds, client reconnect |
| New Pods idle after scale-out | Long connections pinned to old Pods | Max 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
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