Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

Streaming answers work locally but in production users see the whole answer appear at once after 20 seconds, and some streams are cut off after 60 seconds. What do you investigate and fix?

If the whole answer appears at once after 20 seconds, something between the model and the browser is buffering: usually a reverse proxy, an API gateway or a compression filter, so we disable buffering and gzip for text/event-stream, and we confirm the application itself streams instead of calling call() or collecting the Flux. Streams cut at 60 seconds point to an idle or response timeout at a load balancer, or to Spring MVC's async request timeout, which we raise deliberately or keep alive with heartbeat comments. Metrics for time to first token and stream duration, at client and server, tell us which hop is at fault.

Why this question is asked

It worked on my machine is never more true than with streaming. Locally, the browser talks straight to the application; in production the response passes through a CDN, a load balancer, an ingress controller, maybe an API gateway and a servlet filter or two, and each of them may have its own idea of when to send bytes and how long a response may last. Interviewers ask this question to see whether we can debug a request path hop by hop. In this answer, we will separate the two symptoms, find the buffering, check that the application really streams, fix the timeouts, and add the metrics that make the next incident quick.

1Two Symptoms, Two Different Causes

The symptoms in the question point to different problems, so we treat them separately. The whole answer appearing at once after 20 seconds means the generation is streaming somewhere but the bytes are being held and released together; the 20 seconds is simply how long the model takes to write the full answer. Streams cut off after exactly 60 seconds means something has a 60-second limit; round numbers like 60 are almost always a configured timeout, and 60 seconds is a common default for load balancer idle timeouts and proxy read timeouts.

Symptom to likely cause
SymptomLikely causeFix
All text arrives at once, lateproxy or gateway bufferingdisable buffering for SSE
All text at once, also locallyapp calls call() or collects the Fluxreturn the Flux directly
All text at once, only some clientscompression filter or CDNno gzip on text/event-stream
Cut at exactly 60 sLB idle or proxy read timeoutraise for route; heartbeats
Cut at a fixed time, all pathsspring.mvc.async.request-timeoutset it deliberately

2Finding the Hop That Buffers

The quickest method is to call the stream at each hop with curl -N, which prints data as it arrives, and watch whether the first lines appear after a second or only at the end. We start next to the application, then go through the ingress, then through the public URL. The first hop where the output stops trickling is the one that buffers.

Java
# 1. Directly on the pod: streams
curl -N "http://10.0.3.17:8080/chat?q=test" # first data: line after 0.6 s
# 2. Through the ingress (nginx): arrives in one block
curl -N "https://internal-gw/chat?q=test" # everything after 19.8 s
# nginx fix for the streaming location
location /chat {
proxy_pass http://chat-service;
proxy_http_version 1.1;
proxy_buffering off; # forward bytes as they arrive
proxy_read_timeout 300s; # longer than the longest answer
gzip off; # compression waits for data
}

The application can also send the X-Accel-Buffering: no header on the response, which tells nginx not to buffer that response without changing the global config. API gateways and CDNs have their own settings for response buffering or streaming; some need a specific option enabled for event streams, so check the product's documentation.

Compression deserves its own mention. A gzip filter, in the proxy or in Spring Boot with server.compression.enabled, may wait to collect a block of data before compressing it, which looks exactly like buffering. We exclude text/event-stream from compression; Spring Boot's server.compression.mime-types lists which types are compressed, and the event stream should not be one of them.

3Check That the Application Really Streams

Sometimes the buffering is in our own code, and then it fails locally too, if anyone tests it with curl -N. The two usual mistakes are calling call() instead of stream() somewhere in the chain, and turning the Flux into a single value before returning it.

Java
// Broken: waits for the whole answer, then returns one string
@GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<String> chat(@RequestParam String q) {
return chatClient.prompt().user(q).stream().content()
.collectList() // waits for completion
.map(parts -> String.join("", parts)) // one big element
.flux();
}
// Also broken: blocks a thread and loses streaming
String all = chatClient.prompt().user(q).stream().content().blockLast();
// Fixed: return the Flux as it is
return chatClient.prompt().user(q).stream().content();

A third, quieter cause is an advisor or filter that needs the complete response, for example one that post-processes the whole answer, which forces aggregation. Advisors that support streaming must work element by element.

4Fixing the 60-Second Cut-Off

Long answers or slow models can take more than a minute. Cloud load balancers commonly close connections that have been idle, or open, for longer than their configured timeout, and 60 seconds is a common default. Ingress controllers and proxies have read timeouts too. We raise these for the streaming route, to something like 5 minutes, rather than globally.

Where we cannot change a timeout, for example a corporate proxy on the client's side, we send heartbeats: an SSE comment line (a line starting with a colon) every 15 seconds. Comments are ignored by the browser but count as traffic, so idle timeouts do not fire. This helps for idle timeouts, but not for a hard limit on total response time.

Inside the application, Spring MVC's asynchronous request processing has its own timeout. If spring.mvc.async.request-timeout is set to 60s, or the container's default applies, the stream ends at that point regardless of the network. We set it deliberately, longer than the longest expected stream.

Java
spring.mvc.async.request-timeout=5m
// Heartbeat merged into the stream (WebFlux or MVC returning ServerSentEvent)
Flux<ServerSentEvent<String>> tokens = chatClient.prompt().user(q).stream().content()
.map(t -> ServerSentEvent.builder(t).build());
Flux<ServerSentEvent<String>> beats = Flux.interval(Duration.ofSeconds(15))
.map(i -> ServerSentEvent.<String>builder().comment("keep-alive").build());
return Flux.merge(tokens, beats).takeUntilOther(tokens.ignoreElements()); // sketch: stop beats when tokens end
Step throughDebugging a broken stream
  1. Does curl -N on the pod stream?
    YesGo out one hopNoFix the application code
  2. Does the next hop stream?ingress, gateway, CDN
    YesGo out one more hopNoDisable buffering and gzip there
  3. Cut at a round number of seconds?
    YesRaise that hop's timeout; add heartbeatsNoCheck async request timeout
  4. Add time-to-first-token metrics

4 steps in order. Play them, or step through them one at a time.

The heartbeat sketch shows the idea; in production code we share one subscription to the token Flux (for example with publish or share), so the model is not called twice.

5Metrics That Make the Next Incident Easy

We measure time to first token and total stream duration in two places: on the server, from request start to the first element written and to completion, and in the browser, from sending the request to the first event received and to the end. If the server's time to first token is 0.6 seconds but the client's is 20 seconds, something in between buffers. If streams end at 60 seconds on the client but complete on the server, the network cut them. We also count streams that end without the final done event, which catches cut-offs early.

6Follow-up questions

How would you resume a stream after a network drop?

Give each SSE event an id, for example the index of the chunk, and keep the generated text for that answer on the server for a short time, keyed by an answer id. When the connection drops, the browser's EventSource reconnects automatically and sends the Last-Event-ID header; the server then replays the chunks after that id from its buffer and continues if generation is still running. The model call itself cannot be resumed, so the server must keep generating into the buffer independently of the client connection, with a limit on how long it waits for a reconnect.

Why does it work locally but not in production?

Locally there is usually no proxy, no compression and no load balancer, so none of the hops that buffer or time out exist. The production path must be tested with curl -N or an end-to-end test that measures time to first token.

Should we switch to WebSockets?

Not to fix this. WebSockets go through the same proxies and timeouts and add complexity. SSE works well once buffering and timeouts are configured.

7Conclusion

In this answer, we split the problem in two. An answer that arrives all at once is buffering, usually in a proxy, gateway or compression filter, fixed by disabling buffering and gzip for text/event-stream, or our own code collecting the Flux. A cut at 60 seconds is a timeout at a load balancer or proxy, or Spring MVC's async request timeout, fixed by raising it for the route and sending heartbeats. Time to first token and stream duration measured at client and server show which hop is at fault.

What to say in the interviewAbout 70 seconds aloud

Open with

The two symptoms have different causes. If the whole answer appears at once after 20 seconds, something is buffering the response, and the 20 seconds is just the generation time.

Then, if they want more

I would curl with -N at each hop, from the pod outwards, to find the first hop where it stops trickling. Typically it is nginx or the ingress with proxy_buffering on, an API gateway, or gzip compression, so I disable buffering and compression for text/event-stream, or send X-Accel-Buffering no.

I also check our code really streams, because a call() somewhere or collecting the Flux before returning defeats it, and that would fail locally too.

Streams cut at exactly 60 seconds are a timeout, very often the load balancer's idle timeout or a proxy read timeout, so I raise it for that route and send SSE comment heartbeats every 15 seconds. Inside Spring MVC, spring.mvc.async.request-timeout can also end the stream, so I set it deliberately.

Then I add metrics for time to first token and stream duration on both server and client, because comparing them shows immediately which hop is to blame next time.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring AI reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Streaming answers work locally but in production users see the whole answer appear at...": https://scrivio-interview-library.netlify.app/spring-boot/spring-ai/dad4daee8d/