Axios Connection Pooling with http and https Agent
This article explores the critical role of Node.js's native
http.Agent and https.Agent in managing
connection pooling and socket reuse within the Axios HTTP client. You
will learn how enabling persistent connections through
keepAlive optimizes network performance, eliminates latency
caused by repetitive TCP/TLS handshakes, prevents socket exhaustion in
high-concurrency applications, and how to properly configure custom
agents in Axios.
What Are
http.Agent and https.Agent?
In Node.js, http.Agent and https.Agent are
built-in classes responsible for managing the lifecycle, reuse, and
pooling of underlying TCP sockets for outgoing HTTP and HTTPS requests.
By default, every new network request creates a new socket connection
and tears it down immediately after the response is received unless
explicitly instructed to maintain it.
When configured as a connection pool, an Agent holds open idle sockets so that subsequent requests to the same origin (host and port) can reuse an existing connection rather than establishing a new one from scratch.
Why Connection Pooling Matters in Axios
Axios relies on the native Node.js HTTP/HTTPS modules when executed in a server environment. Without a persistent connection pool, high-volume Axios traffic introduces several performance bottlenecks:
- TCP and TLS Overhead: Establishing a standard TCP connection requires a three-way handshake, and HTTPS adds extra rounds of TLS negotiation. Reusing existing connections skips this overhead, cutting latency significantly.
- Socket Exhaustion: Rapidly opening and closing
thousands of connections causes sockets to linger in the
TIME_WAITstate, eventually exhausting the operating system's available file descriptors and ephemeral ports. - Resource Efficiency: Connection pooling lowers CPU and memory usage on both the client application and the target server by limiting concurrent handshakes.
How to Configure Connection Pooling in Axios
Axios provides the httpAgent and httpsAgent
configuration options to attach custom agent instances.
const axios = require('axios');
const http = require('http');
const https = require('https');
// Create persistent agents for HTTP and HTTPS
const httpAgent = new http.Agent({
keepAlive: true,
maxSockets: 50,
maxFreeSockets: 10,
timeout: 60000
});
const httpsAgent = new https.Agent({
keepAlive: true,
maxSockets: 50,
maxFreeSockets: 10,
timeout: 60000
});
// Attach agents to a global Axios instance
const apiClient = axios.create({
baseURL: 'https://api.example.com',
httpAgent,
httpsAgent,
timeout: 10000 // Request timeout in ms
});
module.exports = apiClient;Key Agent Configuration Options
When configuring http.Agent or https.Agent
for Axios, the following options control connection behavior:
keepAlive(boolean): Setting this totrueinstructs the agent to keep sockets open across multiple requests to the same origin.keepAliveMsecs(number): Defines the frequency in milliseconds for sending TCP Keep-Alive packets to keep intermediate network devices from dropping the idle connection. Defaults to1000.maxSockets(number): The maximum number of concurrent active sockets allowed per origin. If requests exceed this limit, they are queued until an active socket becomes free. Defaults toInfinity.maxFreeSockets(number): The maximum number of idle sockets left open per origin in a persistent state. Sockets exceeding this number are closed immediately upon completing their request. Defaults to256.timeout(number): The socket inactivity timeout in milliseconds before an idle socket in the free pool is closed.
Best Practices for Axios Agents
- Share Agent Instances: Create agent instances once at application startup and share them across all Axios requests. Instantiating a new agent per request defeats the purpose of connection pooling.
- Align Client and Server Timeouts: Ensure the agent's idle timeout is slightly shorter than the server's keep-alive timeout to avoid sending requests to a socket that the server is in the process of closing.
- Separate HTTP and HTTPS Agents: Always provide
distinct instances for
httpAgentandhttpsAgentto ensure the correct TLS wrap handlers are applied.