node-modules/agentkeepalive

Should I use different agent instances for different hosts or use a global one?

Aex12 opened this issue · 6 comments

Aex12 commented

If I need to connect to different hosts, it's better to use a separated agent instance for each of this hosts, or use the same agent for all the connections?

Same question, did you find out @Aex12 ?

Aex12 commented

Same question, did you find out @Aex12 ?

I stopped using this library, and started using the native Agent class of Node. It has a keepAlive parameter, which defaults to false, but when enabled, it keeps sockets alive in a per-host basis. I made several tests comparing performance with both agents (multiple requests to a few different hosts) and the native Node (v14) implementation gave me better results than this library.

As to the question if it is better to use the same instance for all hosts, or a separated one for each host, I found no noticeable difference in performance between these approaches. Using always the same Agent instance for all requests, no matter what host, is easier to maintain, so this is how I'm doing it.

I found this to be the better performant config, at least for my use case: high concurrency, lots of requests per second, and a few selection of hosts.

import { Agent as HttpsAgent } from 'https';

export const httpsAgent = new HttpsAgent({
    keepAlive: true,
    timeout: 30 * 1000,
    maxSockets: 8, // this is per-host.
    scheduling: 'fifo',
});

You can checkout the rest of parameters available here:
https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_new_agent_options

@Aex12 did you compare fifo vs lifo?

Aex12 commented

@Aex12 did you compare fifo vs lifo?

FIFO is better suited when you have lots of requests per second to the same host. LIFO is better when you have occasional requests to the same host.

Just a heads-up: The manual solution does not automatically call setNoDelay() like this package does.

Imo, Using same agent would be better considering the memory imact.