Multiple Axios Instances: Performance Implications

Creating dedicated instances of the Axios HTTP client using axios.create() is standard practice for configuring distinct API endpoints, custom headers, and independent interceptors. However, how and when these instances are created directly influences application performance. While maintaining a few static, long-lived instances introduces virtually zero overhead, dynamically generating instances on a per-request basis can cause severe bottlenecks, including memory bloat, garbage collection pressure, and inefficient connection pooling.

Memory Allocation and Garbage Collection Overhead

Each Axios instance is a JavaScript object containing its own configuration defaults, request/response interceptor managers, and internal utility references.

Connection Reuse and Socket Exhaustion in Node.js

The most critical performance drawback of repeatedly creating Axios instances in a Node.js environment involves TCP connection management.

By default, an Axios instance uses the underlying Node.js http.Agent and https.Agent. When you instantiate Axios repeatedly without explicitly configuring a shared agent:

  1. Failure of Keep-Alive: Connections are not properly reused across requests because each client instance may manage separate connection states.
  2. TCP Handshake Latency: Every request is forced to perform a full TCP (and TLS/SSL) handshake, adding significant round-trip time (RTT).
  3. Socket/Port Exhaustion: Opening and closing sockets at high velocity leaves ports in a TIME_WAIT state, eventually exhausting available network sockets and causing requests to fail with ECONNRESET or ETIMEDOUT errors.

Interceptor Execution Overhead

Every Axios instance manages its own interceptor stack via separate promise chains. When instances are reused, the interceptor pipeline remains static and optimized by the JavaScript V8 engine. However, when instances are generated dynamically or when interceptors are repeatedly appended to existing instances:

Impact in the Browser vs. Node.js

Best Practices for Managing Axios Instances

To avoid performance degradation, apply the following architectural patterns: