In IIS 7, the default HTTP keep-alive timeout is set to 120 seconds (2 minutes). This means that by default, a connection will remain open for reuse for 2 minutes after completing a request, unless explicitly closed by either the client or server.
HTTP Keep-Alive is crucial for performance optimization in web applications. Maintaining persistent connections reduces:
- TCP connection overhead
- SSL/TLS handshake costs
- Latency for subsequent requests
You can modify this setting through several approaches:
1. Using ApplicationHost.config
<system.applicationHost>
<webLimits connectionTimeout="00:02:00" />
</system.applicationHost>
2. Via IIS Manager
- Open IIS Manager
- Select the server node
- Double-click "Configuration Editor"
- Navigate to system.applicationHost/webLimits
- Modify the connectionTimeout value
3. PowerShell Automation
Import-Module WebAdministration
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'
-filter "system.applicationHost/webLimits"
-name "connectionTimeout"
-value "00:01:30"
Timeout Value | Pros | Cons |
---|---|---|
Shorter (30s) | Reduces idle connections | More connection overhead |
Longer (5m) | Better for AJAX apps | Higher memory usage |
Use this C# code to test your current keep-alive settings:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://yoursite.com");
request.KeepAlive = true;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine("Connection header: " + response.Headers["Connection"]);
Console.WriteLine("Keep-Alive timeout: " + response.Headers["Keep-Alive"]);
}
For load-balanced environments, consider using ARR (Application Request Routing) with these settings in applicationHost.config:
<webFarms>
<webFarm name="Farm1" enabled="true">
<server address="server1" enabled="true">
<applicationRequestRouting httpPort="80" />
</server>
<applicationRequestRouting>
<protocol timeout="00:02:00" />
</applicationRequestRouting>
</webFarm>
</webFarms>
Remember that the effective keep-alive behavior depends on both server configuration and client HTTP headers. Modern browsers typically have their own keep-alive strategies that interact with these server settings.
HTTP Keep-Alive is a critical performance feature that maintains TCP connections between client and server for multiple requests. In IIS7, the default keep-alive timeout is 120 seconds (2 minutes). This setting determines how long the server maintains an idle connection before closing it.
You can verify the current keep-alive timeout through IIS Manager or by examining the applicationHost.config file:
<system.applicationHost> <httpProtocol> <headerLimits> <add name="Keep-Alive" value="120" /> </headerLimits> </httpProtocol> </system.applicationHost>
To adjust this value programmatically using C#:
using Microsoft.Web.Administration; ServerManager serverManager = new ServerManager(); Configuration config = serverManager.GetApplicationHostConfiguration(); ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol"); ConfigurationElementCollection headerLimitsCollection = httpProtocolSection.GetCollection("headerLimits"); ConfigurationElement addElement = headerLimitsCollection.CreateElement("add"); addElement["name"] = "Keep-Alive"; addElement["value"] = "60"; // New timeout in seconds headerLimitsCollection.Add(addElement); serverManager.CommitChanges();
Factors to consider when adjusting keep-alive timeout:
- Higher values reduce TCP handshake overhead but may consume server resources
- Lower values free up connections faster but increase connection establishment latency
- For high-traffic sites, typically set between 30-120 seconds
Example configuration for various use cases:
// API server with frequent requests from same clients ConfigurationElement apiSetting = headerLimitsCollection.CreateElement("add"); apiSetting["name"] = "Keep-Alive"; apiSetting["value"] = "300"; // 5 minutes // Static content server with infrequent requests ConfigurationElement staticSetting = headerLimitsCollection.CreateElement("add"); staticSetting["name"] = "Keep-Alive"; staticSetting["value"] = "30"; // 30 seconds