We Switched from Axios to Undici

Choosing the right tools can make a significant difference in performance, scalability, and maintainability. Recently, our engineering team decided to switch from Axios, a popular HTTP client, to Undici. This decision was driven by our pursuit of efficiency and modern technology.

Understanding Axios and Its Popularity

Axios has been a staple in many JavaScript projects due to its simplicity and rich feature set. It provides a promise-based interface, supports request and response interceptors, and automatically transforms JSON data. These features, along with broad browser compatibility, have made Axios a go-to solution for handling HTTP requests.

Why We Needed a Change

Despite its popularity, Axios presented some limitations as our application scaled:

  1. Performance: Axios was becoming a bottleneck under high concurrency. In our microservices architecture, where services frequently interact with each other, the performance slowdown was noticeable and impacted overall system efficiency.
  2. Modernity: Newer libraries offer better performance and utilize modern JavaScript features more effectively.
  3. Native Compatibility: We wanted a library that could fully leverage Node.js’s native HTTP capabilities.

Key Reasons for Switching to Undici

Undici, which means “eleven” in Italian (1.1 -> 11 -> Eleven -> Undici), is an HTTP/1.1 client developed from scratch for Node.js. It’s designed for speed, scalability, and leveraging modern JavaScript features. Developed by contributors to the Node.js project, Undici aligns closely with Node.js’s evolving capabilities.

  1. Performance: Undici is built for high performance, using the latest V8 engine optimizations and asynchronous I/O operations to handle many requests efficiently.
  2. Native HTTP/1.1 Implementation: Unlike Axios, Undici implements its HTTP/1.1 client, offering finer control and optimizations for Node.js environments.
  3. Reduced Overhead: Designed to minimize overhead and maximize throughput, Undici is ideal for high-traffic applications.
  4. Modern Features: Support for features like HTTP keep-alive, pipelining, and connection pooling provide a modern and capable HTTP client experience.

Detailed Performance Stats

We conducted benchmarks comparing Axios, Undici, Got, and Fetch under various scenarios, including single requests, burst traffic, and sustained high-concurrency workloads. Here are the results:

Data Fetching Benchmarks (50 TCP connections with pipelining depth of 10 on Node 20.10.0)

HTTP ClientRequests per Second (req/sec)Tolerance (%)Difference with Slowest (%)
Undici3704.43± 2.95
Axios4951.97± 2.88+ 33.68
Got5969.67± 2.64+ 61.15
Fetch4759.42± 0.87+ 28.48

Data Sending Benchmarks (50 TCP connections with pipelining depth of 10 on Node 20.10.0)

HTTP ClientRequests per Second (req/sec)Tolerance (%)Difference with Slowest (%)
Undici1968.42± 2.63
Axios3040.45± 1.72+ 54.46
Got2787.68± 2.56+ 41.62
Fetch2485.36± 2.70+ 26.26

From the benchmarks, it’s clear that Undici, especially in its advanced configurations, outperforms Axios, Got, and Fetch in various scenarios. This performance improvement is particularly evident in high-concurrency environments, making Undici an excellent choice for modern, high-traffic applications.

Our Experience

Transitioning from Axios to Undici was straightforward. The primary changes involved updating our request-handling code to use Undici’s methods and adjusting our error-handling logic to align with Undici’s response structure. Undici’s well-documented API and active community support made the transition smooth and efficient.

Switching from Axios to Undici has significantly boosted our application’s performance and scalability. Undici’s modern architecture, designed specifically for Node.js, aligns perfectly with our goals of maintaining a high-performance, efficient, and scalable system. This decision has been validated by the substantial performance gains we’ve observed, making Undici a key component of our tech stack.

✌️❤️

Discover more from Subodh Jena

Subscribe now to keep reading and get access to the full archive.

Continue reading