FG
📡 Networking

Performance degrades when using node-http-proxy to proxy http calls to EC2 instance

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score52%
52%

Problem

My current setup is as follows: I have a Node.js server that is serving http requests (let's call that the main server) and another Node.js server that is proxying requests to the main server (running node-http-proxy v1.0.3). If the main server and proxy server are both running on my machine, everything works great and I don't see any drop in performance when going through the proxy. However, as soon as the main server is running on an EC2 instance (and the proxy still on my local machine), I see the average response time go from 50-100ms to about 800-3000ms per request when I make requests through the proxy vs. directly calling the main server. I'm kind of at a loss because it seems like as far as EC2 (or the network/internet) is concerned, the actual request leaving my machine and coming back to my machine should be the same in both scenarios, correct? If that is the case, then if feels like the issue is somewhere in node/node-http-proxy. However, then why isn't the performance impacted when the main server is running on my machine? Honestly I'm not even sure about the right way to go about debugging the issue and if anyone can provide some guidance I'd be more than happy to run some more tests to help point people in the right direction. Also, I saw issues #334, #305, and #491, but I didn't feel like any of those could have the same root cause as the issues I'm seeing; feel free to correct me though if that assumption happens to be wrong.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Optimize Node-HTTP-Proxy Configuration for EC2 Performance

Medium Risk

The performance degradation when using node-http-proxy to proxy requests to an EC2 instance is likely due to network latency and the way node-http-proxy handles connections. When the main server is local, the requests are processed quickly, but when the main server is on EC2, the round-trip time increases significantly. Additionally, node-http-proxy may not be configured to handle keep-alive connections effectively, leading to increased overhead for each request.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Enable Keep-Alive for HTTP Connections

    Modify the proxy server to enable keep-alive connections, which can reduce latency by reusing existing connections for multiple requests.

    javascript
    const httpProxy = require('http-proxy');
    const proxy = httpProxy.createProxyServer({
      agent: new http.Agent({ keepAlive: true })
    });
  2. 2

    Increase Timeout Settings

    Adjust the timeout settings for both the proxy and the main server to ensure that connections are not prematurely closed, which can lead to increased latency.

    javascript
    proxy.on('proxyReq', (proxyReq, req, res, options) => {
      proxyReq.setTimeout(30000); // Set timeout to 30 seconds
    });
  3. 3

    Use HTTP/2 for Improved Performance

    If possible, upgrade to HTTP/2 on both the proxy and main server to take advantage of multiplexing and header compression, which can significantly reduce latency.

    javascript
    const http2 = require('http2');
    const server = http2.createServer();
    server.on('stream', (stream, headers) => {
      // Handle HTTP/2 streams
    });
  4. 4

    Monitor Network Latency

    Use tools like `ping` and `traceroute` to monitor the network latency between your local machine and the EC2 instance. This can help identify any network-related issues that may be contributing to the performance degradation.

    bash
    ping ec2-instance-ip
  5. 5

    Profile Application Performance

    Use performance profiling tools such as Node.js built-in profiler or third-party tools (e.g., Clinic.js) to identify bottlenecks in the application code that may be exacerbated by the proxying.

    bash
    node --inspect-brk your-app.js

Validation

To confirm the fix worked, measure the average response time of requests going through the proxy after implementing the changes. The response times should ideally drop closer to the original 50-100ms range. Additionally, monitor the network latency and application performance metrics to ensure improvements.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

proxyhttpnode.js