Quick revision
The short answer
The usual cause is a stale pooled connection: the backend, or a load balancer in front of it, closes a keep-alive connection after its idle timeout, while the gateway's Reactor Netty pool still thinks the connection is usable and sends the next request on it. The fix is to make the gateway retire idle connections first, by setting the pool's max idle time and max life time below the backend's keep-alive timeout. We confirm it by correlating pool logs or metrics with backend restarts and timeouts, and we can add retries for idempotent requests to reduce the impact, but retries do not fix the cause.
What to remember
In this answer, we saw that Connection prematurely closed BEFORE response usually means the gateway reused a pooled connection the backend had just closed for being idle. We checked other causes such as restarts and request timeouts, confirmed the diagnosis with Reactor Netty pool logs and metrics, fixed it by setting the pool's max idle time and max life time below the backend's keep-alive timeout, and kept retries for idempotent requests only as a safety net.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The usual cause is a stale pooled connection: the backend, or a load balancer in front of it, closes a keep-alive connection after its idle timeout, while the gateway's Reactor Netty pool still thinks the connection is usable and sends the next request on it. The fix is to make the gateway retire idle connections first, by setting the pool's max idle time and max life time below the backend's keep-alive timeout. We confirm it by correlating pool logs or metrics with backend restarts and timeouts, and we can add retries for idempotent requests to reduce the impact, but retries do not fix the cause.
Why this question is asked
This error is a classic of reactive gateways and HTTP clients, and it is frustrating because it is intermittent: most requests work, a few fail, and the backend often logs nothing at all. Interviewers ask it to see whether we understand connection pooling and keep-alive at the TCP level rather than only at the HTTP level. In this answer, we will recap how keep-alive connection pools work, see the race that produces the error, look at the other causes worth ruling out, investigate with logs and metrics, and then apply the fix and a safety net.
1A Quick Recap of Keep-Alive and Connection Pools
Opening a TCP connection, and a TLS session on top of it, costs time: one or more network round trips. So HTTP/1.1 clients keep connections open after a response and reuse them for later requests. This is called keep-alive. Spring Cloud Gateway on WebFlux uses Reactor Netty's HttpClient, which keeps a pool of such open connections for each backend host.
The server side also has a rule: it closes a connection that has been idle for too long, to free its own resources. That is the server's keep-alive timeout. It varies a lot. Node.js closes idle connections after 5 seconds by default, many servers and proxies use 60 seconds, and cloud load balancers have their own idle timeouts. The important point is that the two sides decide independently when a connection is finished.
2The Race That Produces the Error
Here is what happens. The gateway uses a connection, gets a response, and puts the connection back in its pool. The connection sits idle. Just as it reaches the backend's idle timeout, the backend closes it. At almost the same moment, a new request arrives at the gateway, which takes that connection from the pool, because from its side nothing has happened yet, and writes the request. The close from the backend arrives first, or the backend answers the write with a reset. Reactor Netty sees the connection end before any response bytes came back and reports PrematureCloseException: Connection prematurely closed BEFORE response.
8 messages in order. Play them, or step through them one at a time.
The close and the reuse cross on the wire.
Notice why it is intermittent. It only happens when a request picks a connection that was idle for almost exactly the backend's timeout. Under steady traffic connections rarely sit idle that long; under light or bursty traffic it happens more often. Notice also why the backend logs nothing: from its point of view, it closed an idle connection, which is normal, and the request never reached the application.
3Other Causes Worth Checking
Stale connections are the most common cause, but not the only one, so we check the others before changing settings. If the backend restarts or is redeployed, its instances close all open connections, and requests in flight fail the same way, so we compare error timestamps with deployment and pod restart events. If the backend has its own request timeout, or crashes while handling a request (an out-of-memory kill, for example), it closes the connection mid-request. A proxy or load balancer between the gateway and the backend can also close connections, on its own idle timeout, which is often 60 seconds, or when it scales.
A useful hint is the timing. Errors clustered at deployment times point to restarts. Errors spread across quiet periods, on requests that came after a gap, point to idle timeouts. Errors on slow requests only point to a request timeout somewhere in the path.
4Investigating With Logs and Metrics
We need to see the pool from the gateway's side. Reactor Netty can log connection lifecycle events, and the gateway can enable wiretap logging of the raw traffic for short periods. Reactor Netty also publishes connection pool metrics through Micrometer, such as active, idle and pending connections per remote address, when metrics are enabled on the HttpClient.
With connection ids we can line up the failing connection with its history. If the failing connections were idle for a time close to the backend's keep-alive timeout before reuse, the diagnosis is confirmed. A packet capture on one gateway host shows the same story directly: a FIN or RST from the backend just before or just after the gateway's request. Debug logging is noisy, so we turn it on for one instance and for a short time.
5The Fix: Retire Connections Before the Backend Does
The cure is to make the gateway the side that closes idle connections. We set the pool's max idle time below the backend's keep-alive timeout, so a connection idle for too long is discarded instead of reused, and a max life time so even busy connections are renewed regularly. A background eviction interval removes expired connections without waiting for the next acquire.
The property names follow the gateway's prefix, which moved in recent releases, so we check the version. If the backend sits behind a load balancer, the shortest idle timeout on the whole path is the one that counts.
As a safety net, the gateway's Retry filter can retry requests that failed with an IOException, which PrematureCloseException is. We restrict it to idempotent methods such as GET, because a POST may have reached the backend before the close. Retries hide most of the symptom, but they add load and latency, and they do not remove the cause, so they come after the pool fix, not instead of it.
6Follow-up questions
Why not just turn keep-alive off?
That would remove the race, but every request would then pay for a new TCP connection and, with HTTPS, a new TLS handshake. That adds latency to every call and load on both sides. Short idle times keep almost all the benefit of reuse with almost none of the risk.
Can we fix it on the backend instead?
Yes, raising the backend's keep-alive timeout above the gateway's idle time has the same effect, for example setting Node's server.keepAliveTimeout to 65 seconds. The rule is simply that the client must give up on an idle connection before the server does. It is often easier to control on the gateway, because one setting covers all backends.
Does HTTP/2 have the same problem?
It can, in a different form: the server may send a GOAWAY frame to close a connection while the client is starting a new stream on it. Good clients handle GOAWAY and retry safely, but idle and life time limits are still worth setting.
7Conclusion
In this answer, we saw that Connection prematurely closed BEFORE response usually means the gateway reused a pooled connection the backend had just closed for being idle. We checked other causes such as restarts and request timeouts, confirmed the diagnosis with Reactor Netty pool logs and metrics, fixed it by setting the pool's max idle time and max life time below the backend's keep-alive timeout, and kept retries for idempotent requests only as a safety net.
Open with
My first suspect would be a stale pooled connection. Reactor Netty keeps keep-alive connections to each backend in a pool. The backend, or a load balancer in front of it, closes connections that stay idle longer than its keep-alive timeout, say 5 seconds for a Node service.
Then, if they want more
If a new request takes that connection from the pool at the same moment the backend closes it, the gateway writes the request and the connection closes before any response, which is exactly PrematureCloseException. That explains why it is intermittent and why the backend logs nothing.
I would still rule out other causes by comparing error times with backend deployments and restarts, and by checking whether only slow requests fail, which would point to a request timeout or crashes.
To confirm, I would turn on debug logging for the PooledConnectionProvider on one gateway instance, or look at the pool metrics, and check that failing connections had been idle for about the backend's timeout; a packet capture would show the FIN or RST just before our request.
The fix is to make the gateway retire connections first: set the pool's max idle time below the backend's keep-alive timeout, for example 4 seconds, plus a max life time and an eviction interval.
As a safety net I would enable the Retry filter for GET and other idempotent requests on IOException, but retries reduce the impact rather than fixing the cause.
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 Cloud documentation, Resilience4j documentation
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 "Requests through Spring Cloud Gateway (WebFlux) intermittently fail with "Connection...": https://scrivio-interview-library.netlify.app/spring-boot/spring-cloud/ac9de1d846/