HTTP client connection reuse: manage sessions and pool lifetimes
HTTP connection reuse lets a client send multiple requests over existing transport connections, avoiding repeated connection and TLS setup. Creating a new client object for every request can create a separate pool and erase that benefit. Reuse the client or handler, release response bodies, and account for DNS changes and shared cookie state.
HTTP connection reuse lets a client send multiple requests over existing transport connections, avoiding repeated connection and TLS setup. Creating a new client object for every request can create a separate pool and erase that benefit. Reuse the client or handler, release response bodies, and account for DNS changes and shared cookie state.
What is reused
A client or session owns configuration and may own a connection pool directly or access one through a handler. Python Requests Session uses urllib3 pooling for requests to the same host; .NET HttpClient uses the pool of its default SocketsHttpHandler. Per-request construction and disposal can therefore repeatedly create and discard pools.
This is not a login session or server-side application state, and a pool does not guarantee the same TCP socket for every request. Concurrency, server closure, idle/lifetime limits, DNS results, and proxy configuration can cause new connections. This article covers library object and pool lifetimes; HTTP-version persistence rules are separate.
Release the response before expecting reuse
A connection may remain occupied while its response body is being read. With Requests stream=True, consume the body or call Response.close() before the connection returns to the pool. Unclosed streaming responses can occupy pool connections, making other requests wait or require new connections. A with context manager helps close a response on exceptions.
In .NET, make response processing and disposal explicit too. A request is not finished while its streamed body is still being read. Check the API documentation for the library-specific consumption and release behavior.
Client lifetime and DNS refresh
Each .NET HttpClient is associated with a handler and connection pool. Creating and disposing one per request causes unnecessary connection setup and, at high request rates, can contribute to port pressure while closed TCP ports await reuse. Microsoft recommends either a long-lived client with PooledConnectionLifetime (.NET Core and .NET 5+) or short-lived clients from IHttpClientFactory, which pools handlers. For .NET Framework, Microsoft recommends the factory.
Long-lived connections may not immediately follow DNS changes. .NET resolves DNS when a new connection is created and does not track DNS TTL, so set an environment-appropriate PooledConnectionLifetime when addresses change. The 2- and 15-minute examples in the docs are arbitrary illustrations, not universal recommendations. Requests Session reuses connections while the session lives; do not assume it offers an equivalent DNS-lifetime control.
Safe adoption
Identify the network hop whose connections you want to reuse. App-to-proxy and proxy-to-server pools are separate.
Check whether a new client is created for every request. Use a long-lived session/client or managed factory as recommended for your library.
Ensure streamed responses are consumed or closed on success, exception, and cancellation paths.
Observe pool waits, active/idle connections, body-processing time, and errors. Diagnose missed releases, slow responses, server limits, DNS, and proxies before increasing pool size.
If .NET needs DNS rotation, choose connection lifetime from actual address-change patterns and handler strategy. Check connection/error metrics after changes and roll back if they worsen.
Sharing a factory or handler can also share state such as cookies. Microsoft warns that IHttpClientFactory handler pooling can share CookieContainer instances, and handler rotation can discard cookies. Review the client and handler strategy when users need cookie isolation.
Sources and scope
[Microsoft Learn: .NET HttpClient guidelines](https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines)
[Requests: Advanced Usage (Session, Keep-Alive, response streaming)](https://requests.readthedocs.io/en/latest/user/advanced/)
.NET behavior and guidance are version-scoped; the Requests claims apply to its documented Session API. Choose connection lifetimes and concurrency limits for the network, load, and library settings. No production performance measurement or code execution was performed.