---
title: "How to Use Keep-Alive With a Proxy: Connection Pooling"
description: "Keep-alive reuses one connection for several requests and skips the handshakes. With a rotating proxy, the exit IP stays the same while it is open."
url: https://proxynet.io/blog/proxy-keep-alive-connections
date: 2026-12-01
author: "Acar Diveroli"
category: "Web Scraping"
lang: en
---

# How to Use Keep-Alive With a Proxy: Connection Pooling

HTTP keep-alive means holding one TCP connection open for several requests. The client keeps these connections in a pool and hands them to the next request for the same host. Through a proxy, reuse skips the TCP, `CONNECT` and TLS steps. The trap is in rotating proxies: while the connection stays open, the exit IP stays the same too. This post covers how the pool works, the Python and Node settings, and how to get a new IP on every request.

> **Note: Short answer**
>
> `requests.Session`, `httpx.Client` and a Node agent built with `keepAlive: true` keep connections in a pool and reuse them for later requests. With a rotating proxy, that means the same exit IP while the connection is open. For a new IP on every request, send `Connection: close`.

## Persistent connections and the pool

In HTTP/1.1, connections are persistent by default. After sending a response, the server does not close the connection; it waits for the next request on it. If either side sends `Connection: close`, the connection closes after that response. The rule is in section 9 of RFC 9112.

A connection pool is the client-side store of these open connections. When a request finishes, its connection goes back to the pool. When a new request for the same host arrives, the client checks the pool first. It uses an idle connection if there is one and opens a new one if not.

This post stays at the HTTP layer. TCP keepalive probes (`SO_KEEPALIVE`) are a separate mechanism. They send small packets to check whether an idle connection is still alive on the other end, and they never use the connection for a new request.

## Reuse through a proxy

The first request to an HTTPS site through an HTTP proxy takes three steps:

1. The client opens a TCP connection to the proxy.
2. A `CONNECT` request with a `Proxy-Authorization` header goes to the proxy. The proxy connects to the target and opens a tunnel.
3. Inside the tunnel, the client runs the TLS handshake with the target site.

Only then does the actual request go out. When a pooled connection is reused, none of the three steps repeats, and the request goes straight through the open tunnel.

In our test we sent five requests with `requests.Session`. The proxy saw one `CONNECT` tunnel, and the target server saw one TLS connection. The same five requests with plain `requests.get()` opened five tunnels and five TLS connections. In a scraping job that sends many small requests, the difference lies in those handshakes.

## Session and Client in Python

In Requests, the pool lives inside the `Session` object. `requests.get()` opens a new session on every call and closes it when done. So consecutive `requests.get()` calls do not share connections. The basic ways to give Requests a proxy are in [How to use a proxy with Python Requests](/blog/python-requests-proxy).

```python
import requests

proxy = "http://user:pass@pr.proxynet.io:8000"

with requests.Session() as s:
    s.proxies = {"http": proxy, "https": proxy}
    for _ in range(5):
        r = s.get("https://httpbin.org/ip", timeout=15)
        print(r.json()["origin"])
```

A connection returns to the pool only after the response body is fully read. If you use `stream=True`, read the body to the end or open the response in a `with` block. Otherwise every request opens a new connection.

In HTTPX, `httpx.Client` does the same job, and pool limits are set with `httpx.Limits`.

```python
import httpx

proxy = "http://user:pass@pr.proxynet.io:8000"
limits = httpx.Limits(
    max_connections=50,
    max_keepalive_connections=20,
    keepalive_expiry=5.0,
)

with httpx.Client(proxy=proxy, limits=limits, timeout=15) as client:
    for _ in range(5):
        print(client.get("https://httpbin.org/ip").json()["origin"])
```

Like Requests, `httpx.get()` opens a new client on every call. Create the client once and share it across the whole job.

## http.Agent in Node

In Node, the pool is managed by `http.Agent` and `https.Agent`. An agent created with `keepAlive: true` does not close the socket when a request ends; it hands it to the next request. The `proxyEnv` option, added in Node 24.5.0, gives the agent the proxy address directly.

```js
import https from "node:https";

const agent = new https.Agent({
  keepAlive: true,
  maxSockets: 10,
  proxyEnv: { HTTPS_PROXY: "http://user:pass@pr.proxynet.io:8000" },
});

for (let i = 0; i < 5; i++) {
  const ip = await new Promise((resolve, reject) => {
    https
      .get("https://httpbin.org/ip", { agent }, (res) => {
        let body = "";
        res.on("data", (c) => (body += c));
        res.on("end", () => resolve(JSON.parse(body).origin));
      })
      .on("error", reject);
  });
  console.log(ip);
}

agent.destroy();
```

In an agent you create yourself, `keepAlive` defaults to `false`. Node 24's built-in `http.globalAgent`, however, comes with `keepAlive: true` and a 5-second `timeout`. Despite its name, `keepAliveMsecs` does not tune the HTTP pool. Per the Node docs, it is the initial delay for TCP keepalive packets. For Axios and node-fetch setup, see [Using a proxy in Node.js](/blog/nodejs-proxy).

## The same-IP trap in rotation

With a rotating proxy, the exit IP is chosen when a new connection opens. For HTTPS requests, that moment is when the `CONNECT` tunnel is set up. While the tunnel stays open, traffic leaves through the same exit. When the pool reuses the connection, the new request also leaves with the old IP.

Our test proxy gave every new connection a different exit address. We sent five requests with each setup:

| Setup | Proxy connections | Distinct exit IPs |
|---|---|---|
| `requests.get()` (no session) | 5 | 5 |
| `requests.Session()` | 1 | 1 |
| `Session` + `Connection: close` | 5 | 5 |
| `httpx.Client()` | 1 | 1 |
| `httpx.Limits(max_keepalive_connections=0)` | 5 | 5 |
| Node, `keepAlive: true` | 1 | 1 |
| Node, `keepAlive: false` | 5 | 5 |

Every setup that used a pool sent all five requests from one IP. Code that expects a new IP per request raises no error here; it just keeps seeing the same address. If you see a table like this, the problem is in the pool, not the gateway.

For a new connection on every request, add a `Connection: close` header to the session in Requests, set `max_keepalive_connections=0` in HTTPX, and use `keepAlive: false` in Node.

```python
with requests.Session() as s:
    s.proxies = {"http": proxy, "https": proxy}
    s.headers["Connection"] = "close"  # every request gets a new tunnel, a new IP
    for _ in range(5):
        print(s.get("https://httpbin.org/ip", timeout=15).json()["origin"])
```

The cost is that the three handshakes come back on every request. A new IP per request is also no way around a site's rate limit. Respect the site's limits and its robots.txt rules. To collect independent pages from different addresses, [Rotating Proxy](https://proxynet.io/rotating-proxy) works in this mode.

There is a trap in the other direction too. Do not rely on the pool to keep the same IP. An idle connection can close at any moment, and its replacement brings a different IP. If a flow needs one address throughout, tie continuity to a session ID. The mechanism is explained in [What is a sticky session?](/blog/sticky-session-explained)

## Pool size and idle timeouts

Pool size is the number of connections that can stay open at once for one host. Through a proxy, each open connection is a separate tunnel. In rotating mode, each tunnel carries its own exit IP.

| Library | Default limit | Idle timeout |
|---|---|---|
| Requests | 10 connections per host | None of its own |
| HTTPX | 100 total, 20 idle | 5 seconds |
| Node `https.Agent` | `maxSockets` unlimited, 256 idle | Via `timeout` |

In Requests, if the thread count exceeds the pool, the extra connections are closed when their work ends. The log shows the warning `Connection pool is full, discarding connection`. In our test, 20 threads with the default pool raised this warning. With `pool_maxsize=20`, it went away.

```python
from requests.adapters import HTTPAdapter

adapter = HTTPAdapter(pool_connections=10, pool_maxsize=20)
s.mount("https://", adapter)
s.mount("http://", adapter)
```

`pool_connections` sets how many hosts get a pool, and `pool_maxsize` sets how many connections are kept per host. Match the pool to the number of concurrent jobs.

Idle closes come from both sides. Servers and proxies close an idle connection after a while. RFC 9112 sets no limit on that time and expects the client to be ready for the close. In our test we used a proxy that closed idle connections after 3 seconds. Requests noticed the closed connection, opened a new one without an error, and the second request left from a different IP. In HTTPX, a 6-second wait exceeded the 5-second default and gave the same result.

If the close happens while a request is in flight, you get a connection error, so add retries to code that uses a pool.

## Troubleshooting

Most pool problems are silent. The code runs, but the number of connections or IPs differs from what you expect.

| Symptom | Cause | Fix |
|---|---|---|
| Same IP on every request | The pool reuses the connection | `Connection: close` or keep-alive off |
| IP changed mid-flow | Idle connection closed, a new one opened | Sticky session |
| `Connection pool is full` | More threads than pool slots | Raise `pool_maxsize` |
| Occasional connection error | Connection closed while a request was in flight | Add retries |
| More connections than expected | New `Session` or `Client` per request | Share one object |

To see the IP behavior, send your code to an IP echo service five times. If five requests show one address, the pool is working. If they show five different addresses, connections are not being reused.

## How we tested

We ran the tests on December 1, 2026 on Windows 11, with Python 3.13.17, Requests 2.34.2, urllib3 2.6.3, HTTPX 0.28.1 and Node 24.22.2. On `127.0.0.1` we ran an HTTP proxy that required authentication. The proxy carried each new client connection to the target from a different local address, imitating a rotating gateway. The target was a local HTTPS server that returned the connecting address. The proxy counted `CONNECT` tunnels, and the target counted TLS connections. We did not measure timing, because latency on a local network does not represent a real proxy. In the examples we replaced the local addresses with `pr.proxynet.io:8000` and `httpbin.org`.

## The bottom line

Share one `Session`, `Client` or agent across the whole job, and size the pool to the number of concurrent jobs. If you need a new IP on every request, send `Connection: close`; if you need the same IP, rely on a sticky session, not the pool. Above all, measure the IP behavior of your code with an echo service. Once the setup is ready, you can send your first request with [Residential Proxy](https://proxynet.io/residential-proxy).

## Sources

- [RFC 9112, §9.3 Persistence, §9.5 Failures and Timeouts: rfc-editor.org](https://www.rfc-editor.org/rfc/rfc9112)
- [Requests documentation, Advanced Usage, Session Objects and Keep-Alive: requests.readthedocs.io](https://requests.readthedocs.io/en/latest/user/advanced/)
- [HTTPX documentation, Resource Limits: python-httpx.org](https://www.python-httpx.org/advanced/resource-limits/)
- [Node.js documentation, `http.Agent` and Built-in Proxy Support: nodejs.org](https://nodejs.org/api/http.html)

## Frequently asked questions

### Does keep-alive make a proxy faster?

Yes, when you send many small requests. On a reused connection, the TCP connection to the proxy, the `CONNECT` tunnel and the TLS handshake with the target do not repeat. The request goes straight through the open tunnel. In our test, five requests with `requests.Session` opened one tunnel, while calls without a session opened five. For a single request that downloads a large file, the gain is small, because the transfer sets the time.

### Why does my rotating proxy keep giving the same IP?

Usually because of the pool. With a rotating proxy, the exit IP is chosen when a new connection opens. When a `Session`, an `httpx.Client` or an agent with `keepAlive: true` reuses the same connection, requests leave through the same tunnel and the same IP. For a new IP on every request, send a `Connection: close` header or turn off keep-alive in the client.

### What does Connection: close do?

It closes the connection after that response. Under RFC 9112, a client that sends or receives `close` sends no further requests on that connection. Through a proxy, this means every request opens a new tunnel. In rotating mode, each tunnel brings a new exit IP. The cost is that the TCP, `CONNECT` and TLS steps run again on every request.

### What is the difference between requests.get and Session?

The connection pool. `requests.get()` opens a new session on every call and closes it when done, so connections are not shared. `requests.Session` keeps the pool between calls and reuses the connection to the same host. Cookies and default headers are stored in the session too. In code that sends many requests, use a single `Session`.

### How big should the connection pool be?

As big as your number of concurrent jobs. Requests keeps 10 connections per host. If you run 20 threads, the extra connections are closed and the `Connection pool is full` warning appears. Enlarge the pool with `HTTPAdapter(pool_maxsize=20)`. In HTTPX, `httpx.Limits` does the same job; in Node, `maxSockets` and `maxFreeSockets`.

### Are TCP keepalive and HTTP keep-alive the same thing?

No. HTTP keep-alive holds a connection open for several requests and reuses it. TCP keepalive (`SO_KEEPALIVE`) sends small probe packets over an idle connection to check that the other side is still there. Despite its name, Node's `keepAliveMsecs` option belongs to the second group: it sets the initial delay of the TCP probes.
