HttpClient Socket Exhaustion

HttpClient Socket Exhaustion

Leader 1 2 7
calendar_todayschedule1 min read
— Originally published at dev.to

Your .NET API is Slowly Dying (And You Don't Even Know It)

Last month, our production system started timing out randomly. No errors. No warnings. Just... slow death.
After 3 sleepless nights and diving through 60+ microservices, I found the culprit:

var client = new HttpClient(); // This single line
Here's what was happening:
Every HTTP call was creating a NEW TCP socket. Under load, we exhausted the system's connection pool. Requests started queuing. Response times went from 200ms to 30+ seconds.

The Silent Killer:
• ✅ Your code compiles fine
• ✅ Your tests pass
• ✅ It works in dev/staging
• ❌ It crashes in production under real load

The Fix That Saved Our SLA:

// In Startup.cs
`services.AddHttpClient("MyService", client => {

client.Timeout = TimeSpan.FromSeconds(120);

}) .ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler {

PooledConnectionLifetime = TimeSpan.FromMinutes(2),
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(5),
MaxConnectionsPerServer = 20,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate

});`

`// In your service
public class MyService {

private readonly IHttpClientFactory _clientFactory;    
public MyService(IHttpClientFactory clientFactory) {
    _clientFactory = clientFactory;
}    
public async Task<string> GetDataAsync() {
    var client = _clientFactory.CreateClient("MyService");
    // Use the client
}

}
`

The Results:
95% reduction in connection failures
⚡ 40% improvement in throughput
Prevented potential revenue loss from timeouts

The Lesson:
IHttpClientFactory isn't just a "best practice" - it's a production survival tool.
Microsoft documented this in 2018, but I still see this anti-pattern EVERYWHERE.
Have you been bitten by socket exhaustion?

Share your battle story in comments

#performance #dotnet #csharp #softwareengineering #productionissues #microservices #debugging #azure #performanceoptimization #lessonslearned #techleadership

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Why “Building in Public” Is Hollowing Out Your Developer Career

Karol Modelski - Jun 18

Modern Use OF Telnet with an example In dotnet,The Rise and Fall of Telnet: A Network Protocol's Journey

Moses Korir - Mar 12, 2025

Parallel Processing Gone Wrong

nouman - Aug 24

LINQ Looks Clean Until You Care About Performance

a95yman - Apr 27

The $320 Billion Garbage Tax: What My Stress Tests Revealed About C# Memory Costs

The Singularity Workshop - Dec 7, 2025
chevron_left
1.9k Points10 Badges
Karachi, Pakistanmicrosaas360.com
4Posts
6Comments
9Connections
I design systems that scale and build the teams that ship them.

Over 18 years I've architected clo... Show more

Related Jobs

View all jobs →

Commenters (This Week)

8 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!