Quick revision
The short answer
The code leaks connections. A ResponseInputStream from getObject holds a pooled HTTP connection until the body is fully read or the stream is closed, and this code reads 512 bytes and returns without closing it, so each call keeps one connection forever until the pool, 50 connections by default for the Apache client, is empty. The fix is try-with-resources around the stream, with in.abort() for partial reads of large objects, or better, asking S3 for only the bytes we need with a Range of bytes=0-511. Raising maxConnections only delays the failure, and we should also check that clients are not being built per request.
What to remember
In this answer, we saw that a ResponseInputStream from getObject holds a pooled HTTP connection until it is fully read or closed, so reading 512 bytes and returning leaks one connection per call until the Apache client's pool of 50 is empty. We fix it with try-with-resources and abort() for partial reads, or better, with a Range request for bytes=0-511 so the body is exactly what we need. Raising maxConnections only hides the leak, clients must be shared rather than built per request, and leased-connection metrics reveal leaks early.
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 code leaks connections. A ResponseInputStream from getObject holds a pooled HTTP connection until the body is fully read or the stream is closed, and this code reads 512 bytes and returns without closing it, so each call keeps one connection forever until the pool, 50 connections by default for the Apache client, is empty. The fix is try-with-resources around the stream, with in.abort() for partial reads of large objects, or better, asking S3 for only the bytes we need with a Range of bytes=0-511. Raising maxConnections only delays the failure, and we should also check that clients are not being built per request.
Why this question is asked
Connection pool exhaustion is a classic production bug: everything works in tests, works at low traffic, and then fails after a traffic increase with an error that blames the pool rather than the code. Interviewers like this question because the clue is in one line of code, and finding it requires knowing how the SDK manages HTTP connections. In this answer, we will recap how the SDK's connection pool works, see exactly how the code leaks, reproduce the failure, fix it in three ways, and look at how to detect such leaks with metrics.
1A Quick Recap of the SDK's Connection Pool
The synchronous S3Client sends HTTP requests through an HTTP client, by default the Apache-based one. To avoid opening a new TCP and TLS connection for every request, it keeps a pool of open connections. Before a request, a thread leases a connection from the pool; when the response is finished, the connection goes back to the pool for the next request. The pool has a maximum size, 50 connections by default for the Apache client, and a thread that cannot get a connection waits up to the connection acquisition timeout (10 seconds by default; check your version) and then fails with Unable to execute HTTP request: Timeout waiting for connection from pool.
For most calls, returning the connection is automatic, because the SDK reads the whole response. getObject with a stream is different: the SDK cannot read the body for us, because the whole point is to let us stream it. So the connection stays leased until we finish reading the body or close the stream.
2How This Code Leaks
Let us look at the code again with the connection's life marked in comments.
8 messages in order. Play them, or step through them one at a time.
The connection is returned only when the body is read to the end or the stream is closed.
Notice why CPU and S3 latency look normal: the service is not busy and S3 is not slow. Threads are simply waiting for a connection that will never come back. At low traffic, the garbage collector may eventually clean up some abandoned streams and their connections, which hides the leak; after the traffic increase, leaks happen faster than any cleanup and the pool runs dry.
3Fix 1: Close the Stream, and Abort Partial Reads
The minimum fix is try-with-resources, so the stream is closed whatever happens, including when parseHeader throws.
The reason for abort() is what close() does with an unfinished body. To return a connection to the pool for reuse, the HTTP client must first get past the rest of the response, which can mean reading, and discarding, megabytes of data we never wanted. abort() instead closes the underlying connection, so it is not reused but nothing is downloaded. For a small remaining body, closing normally is fine; for large objects read partially, aborting is cheaper.
4Fix 2: Ask Only for the Bytes We Need
Better still, we can avoid downloading the rest of the object at all. S3 supports HTTP range requests, so we ask for the first 512 bytes and S3 sends only those.
Now the body is exactly what we read, so the connection goes back to the pool ready for reuse, and we transfer 512 bytes instead of 8 MB, which also saves S3 data transfer cost and time. We still keep the try-with-resources, because a parsing exception should never leak a connection.
5Why Raising maxConnections Is Not the Fix
It is tempting to fix the error by configuring a bigger pool. With a leak, that only changes when the service fails, not whether it does: 200 connections leak four times later than 50. It can even make things worse, because hundreds of half-read connections hold sockets and memory. The pool size should be tuned for real concurrency once the leak is fixed, for example from Little's law: if 300 S3 calls per second each take 80 ms, we need about 24 connections on average, so 50 is fine.
While investigating, it is also worth checking that the code does not build a new S3Client per request. Each client has its own pool and threads; clients that are built and never closed leak those resources too, and they make every call pay for a fresh TLS handshake. The client should be one shared bean.
6Follow-up questions
How would you detect connection leaks with metrics?
The SDK can publish HTTP client metrics through a MetricPublisher, such as the CloudWatch publisher or a small custom one that forwards to Micrometer. The useful ones are LeasedConcurrency (connections in use), AvailableConcurrency, MaxConcurrency and PendingConcurrencyAcquires (threads waiting). A leak shows as leased connections climbing steadily and never falling back when traffic drops, while the request rate is flat; healthy usage rises and falls with traffic. An alert on pending acquires above zero for several minutes catches the problem before the timeouts start.
Would the async client have the same problem?
The same principle applies: a streamed response holds a connection until it is consumed or cancelled. With S3AsyncClient and AsyncResponseTransformer.toBytes or toFile, the SDK consumes the whole body for us; with a publisher-based transformer, our code must consume or cancel the subscription.
How do you reproduce this in a test?
Configure the client with a small pool, for example maxConnections of 2 and a short acquisition timeout, run the method three times against LocalStack or another S3-compatible server, and assert that the third call succeeds. With the leaky code the third call times out; with the fixed code it returns. That test protects against the bug coming back.
7Conclusion
In this answer, we saw that a ResponseInputStream from getObject holds a pooled HTTP connection until it is fully read or closed, so reading 512 bytes and returning leaks one connection per call until the Apache client's pool of 50 is empty. We fix it with try-with-resources and abort() for partial reads, or better, with a Range request for bytes=0-511 so the body is exactly what we need. Raising maxConnections only hides the leak, clients must be shared rather than built per request, and leased-connection metrics reveal leaks early.
Open with
The problem is a connection leak. getObject returning a ResponseInputStream keeps the underlying HTTP connection leased from the pool until the body is fully read or the stream is closed.
Then, if they want more
This code reads 512 bytes and returns without closing, so every call strands one connection. The sync client uses the Apache HTTP client with 50 connections by default, and after 50 leaked calls every new request waits for a connection and fails with timeout waiting for connection from pool.
That is why CPU and S3 latency look normal: the threads are just waiting. At low traffic it was hidden, because abandoned streams might eventually get cleaned up.
The minimal fix is try-with-resources around the stream, and for a partial read of a large object I would call abort, because closing would otherwise try to get past the rest of the body before reusing the connection.
The better fix is to request only what I need with a range of bytes 0 to 511, so S3 sends 512 bytes and the connection goes back to the pool cleanly; I would still keep try-with-resources. I would not raise maxConnections, because that only delays the failure.
I would also check that the code is not building a new S3Client per request. To catch this in future, I would publish the SDK's HTTP metrics, leased connections and pending acquires, and alert when leased keeps climbing while traffic is flat.
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
- AWS 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 "After a traffic increase, a service that reads objects from S3 starts failing with...": https://scrivio-interview-library.netlify.app/spring-boot/aws/a5b8fc1eda/