Skip to content

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

Open

Description

@Aex12

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?

Activity

  1. gchicoye commented on Jul 6, 2021

    @gchicoye

    Same question, did you find out @Aex12 ?

  2. Aex12 commented on Jul 22, 2021

    @Aex12
    Author

    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

  3. brandondoran commented on Sep 15, 2021

    @brandondoran

    @Aex12 did you compare fifo vs lifo?

  4. Aex12 commented on Sep 15, 2021

    @Aex12
    Author

    @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.

  5. kurtextrem commented on Oct 5, 2021

    @kurtextrem

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

  6. harsh-sri commented on Oct 13, 2023

    @harsh-sri

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions