Keep Alive: What It Is and How It Works with Proxies

Keep-alive is a mechanism that allows a single TCP connection to be reused for multiple HTTP requests, reducing overhead and improving performance. When using rotating proxies, understanding keep-alive is crucial for managing sessions, connection limits, and traffic costs.

In short
  • Keep-alive reduces latency and server load by reusing connections.
  • It interacts with sticky sessions and IP rotation on a proxy gateway.
  • Misconfigured keep-alive can lead to unexpected IP changes or connection waste.
  • Most HTTP clients enable it by default, but you can control it.

What is keep alive in HTTP and TCP?

Keep-alive is a feature that maintains a network connection open after an HTTP request completes, allowing subsequent requests to reuse the same connection. At the TCP layer, keep-alive refers to periodic packets sent to prevent a connection from being dropped by intermediate routers or firewalls due to inactivity. For HTTP, the `Connection: keep-alive` header signals the intent to reuse the connection, which is the default in HTTP/1.1. This reuse avoids the three-way handshake and TLS negotiation for every request, cutting latency and CPU usage. When you use a proxy, the keep-alive mechanism applies to the connection between your client and the proxy gateway, as well as between the proxy and the target website.

How does keep alive work with rotating proxies?

When you connect through a rotating proxy gateway like gw.rotatingproxy.cc, your client establishes a TCP connection to the gateway on port 7070 or 7071. If keep-alive is enabled, that connection can be reused for multiple requests through the proxy. This interacts directly with IP rotation behavior. Without a session ID (sid), each new HTTP request over a keep-alive connection will still exit from a new IP address if the proxy is configured for per-request rotation. The gateway manages the backend exit node independently of the frontend connection from your client. With a sticky session (using the sid option), the keep-alive connection helps maintain the same exit IP for the duration of the session's time-to-live (ttl), as long as the connection remains open and active.

Keep alive example with a Python requests session

Most HTTP client libraries use connection pooling and keep-alive by default when you use a session object. Here's a Python example with the requests library and a rotating proxy.

import requests
from requests.auth import HTTPProxyAuth

proxies = {
    'http': 'http://USERNAME_net.res_cc.us:PASSWORD@gw.rotatingproxy.cc:7070',
    'https': 'http://USERNAME_net.res_cc.us:PASSWORD@gw.rotatingproxy.cc:7070'
}

session = requests.Session()
session.proxies = proxies

# First request - establishes a keep-alive connection to the proxy
resp1 = session.get('https://httpbin.org/ip')
print(resp1.json())

# Second request - reuses the same TCP connection if keep-alive is active
resp2 = session.get('https://httpbin.org/user-agent')
print(resp2.json())

# The session object manages connection pooling
session.close()

In this example, the two requests likely reuse the same connection to the proxy gateway, but depending on your username options, they may exit from different residential IPs. For sticky sessions, you would add a sid to the username to ensure IP consistency across requests on the same keep-alive connection.

Keep alive impact on proxy sessions and IP rotation

The relationship between keep-alive and IP rotation is often misunderstood. A single keep-alive connection to the gateway does not lock you to one exit IP unless you also use a sticky session ID. The gateway's backconnect architecture assigns an exit node per request when rotation is active. However, keeping a connection alive can improve performance for tasks like web scraping many pages from the same site, as it reduces connection setup time. For price monitoring scripts that make frequent requests, keep-alive can significantly reduce latency. It's important to note that if a sticky session IP becomes unavailable (e.g., the mobile device goes offline), the gateway may assign a new IP within the same location even on an existing keep-alive connection, which is part of the service's high availability design.

Common misunderstandings about keep alive and proxies

One common mistake is assuming that disabling keep-alive forces IP rotation. IP rotation is controlled by the presence of a sid option in your proxy username, not by the HTTP keep-alive setting. Another misunderstanding is that keep-alive ties up an exit IP indefinitely; it only maintains the connection to the gateway, not necessarily the backend resource. Some users worry that keep-alive connections will exhaust their concurrency limit; while an idle keep-alive connection does count against the limit, most clients will close idle connections after a timeout. Also, keep-alive does not affect traffic measurement; you are billed for the total GB transferred, regardless of how many connections were used. For headless browsers, keep-alive is managed automatically by the browser's network stack.

How to control keep alive in your HTTP client

You can usually enable or disable keep-alive through your client's configuration. In curl, use --http1.1 (keep-alive default) or --no-keepalive to disable it. For Python's requests library, the urllib3 connection pool manages keep-alive; you can adjust pool size and max keep-alive connections. In Node.js with axios or fetch, the http.Agent or https.Agent controls keep-alive. Disabling it can be useful for testing or when you want to ensure each request gets a fresh connection to the proxy, though this increases overhead. For production scrapers, especially those using Scrapy or Playwright, it's generally best to use default keep-alive settings and manage session behavior through the proxy username options.

Keep alive and pay-as-you-go proxy billing

Keep-alive does not directly change how traffic is metered. Our pay-as-you-go billing counts every byte that passes through the gateway, upload plus download, in 1,000,000,000-byte GB increments. Reusing a connection with keep-alive avoids re-establishing TLS, which saves a few kilobytes of handshake data, but the savings are minimal compared to the actual content downloaded. The primary benefit is reduced latency, which can let your crawler finish faster, indirectly affecting costs if you are paying for compute time elsewhere. For high-volume use cases like SERP data collection or social media management, the latency reduction from keep-alive can be significant. Always monitor your usage in the dashboard to understand your traffic patterns.

Related terms and concepts

Understanding keep-alive connects to several other proxy concepts. A sticky session uses a session ID to maintain the same exit IP, which works alongside keep-alive connections. The gateway (gw.rotatingproxy.cc) manages the keep-alive connections from clients. Backconnect refers to the gateway's architecture, where your client connects to a single endpoint that rotates backend nodes. Concurrency limits are affected by open keep-alive connections. Retry strategies often need to consider whether to reuse a keep-alive connection after a failure. Finally, latency is the performance metric most improved by proper keep-alive usage.

Questions

Does enabling keep-alive mean I will keep the same proxy IP address?

No, keep-alive only reuses the connection to the proxy gateway. IP rotation is controlled separately by whether you include a session ID (sid) in your proxy username. Without a sid, each request can get a new IP even over the same keep-alive connection.

Can keep-alive cause my proxy connections to "hang" or timeout?

It can if network issues occur. An idle keep-alive connection may be closed by intermediate firewalls. Most HTTP clients have idle timeout settings to close stale connections. If you experience hangs, check your client's timeout configurations or consider disabling keep-alive for debugging.

How does keep-alive affect the number of concurrent connections I can have?

Each keep-alive connection, even if idle, typically counts toward your account's concurrency limit (up to 1,000). Using connection pooling with a sensible maximum size ensures you don't exhaust the limit with idle connections. Close sessions when done.

Should I disable keep-alive for web scraping to avoid detection?

Usually not. Keep-alive is normal browser behavior, so using it can make your requests look more natural. Detection is more about fingerprinting, user agents, and request patterns. Focus on polite scraping practices outlined in how to avoid getting blocked.

Does the proxy gateway support HTTP/2 or HTTP/3 keep-alive?

The gateway supports HTTP/1.1 over the proxy ports (7070 for HTTP/HTTPS, 7071 for SOCKS5). HTTP/2 and HTTP/3 are not currently supported for the proxy protocol itself. The keep-alive mechanism discussed here applies to HTTP/1.1 connections.

If I use a sticky session, does keep-alive help maintain the IP longer?

It helps maintain the connection to the gateway, but the sticky session's duration is governed by the ttl (time-to-live) value you set in the username (e.g., ttl.30). The IP may be reassigned before the ttl expires if the exit device goes offline, regardless of the keep-alive state.

Start with one top-up, use any network

Create an account in a minute, add $40 or more, and the same balance pays for residential, mobile, datacenter and ISP traffic.