node.jsaxiosnestjshttprequestnode-modules

Is keep alive set to true by default for outgoing calls from nestjs application


I am using NestJS version "@nestjs/core": "^9.0.9" and it has axios version as "axios": "1.8.2" and nestjs/axios version as "@nestjs/axios": "^3.0.1"

For Incoming Calls to my NestJS app from a client (example: mobile or web):

But for Outgoing Calls from my NestJS app to a end point (Db, or a third API)

According to NestJS Documentation: Http-Module is just a wrapper over the Axios, and Axios don't support keep-alive:true by default.

NESTJS DOCUMENTATION: https://docs.nestjs.com/techniques/http-module

AXIOS DOCUMENTATION: https://github.com/axios/axios#request-config

Sample Code:

import { HttpService } from '@nestjs/axios';

export class StudentService {
    constructor(    
       private readonly httpService: HttpService,
    ) {}

    private async fetchStudentDetails(studentIds: string[], string targetUrl): Promise<Map<string, object>>      
    {
      const response = await this.httpService.post(targetUrl, { studentIds });
    }
}

Solution

  • Yes, if using Node.js >=19, because by default Axios is using Node.js's HTTP client, and starting with v19, Node.js sets keepAlive to true by default, as stated in

    Node.js docs for http.globalAgent

    Global instance of Agent which is used as the default for all HTTP client requests. Diverges from a default Agent configuration by having keepAlive enabled and a timeout of 5 seconds.

    Node.js v19 release announcement:

    HTTP(S)/1.1 KeepAlive by default

    Starting with this release, Node.js sets keepAlive to true by default. This means that any outgoing HTTP(s) connection will automatically use HTTP 1.1 Keep-Alive. The default keep-alive duration is 5 seconds.

    Axios docs:

    // `httpAgent` and `httpsAgent` define a custom agent to be used when performing http
    // and https requests, respectively, in node.js. This allows options to be added like
    // `keepAlive` that are not enabled by default before Node.js v19.0.0. After Node.js
    // v19.0.0, you no longer need to customize the agent to enable `keepAlive` because
    // `http.globalAgent` has `keepAlive` enabled by default.
    httpAgent: new http.Agent({ keepAlive: true }),
    httpsAgent: new https.Agent({ keepAlive: true }),
    

    Request config

    So, if not set, Node.js will use globalAgent with keepAlive: true, but when creating Agent, its options still have default option set to false:

    new Agent([options])

    keepAlive <boolean> Keep sockets around even when there are no outstanding requests, so they can be used for future requests without having to reestablish a TCP connection. Not to be confused with the keep-alive value of the Connection header. The Connection: keep-alive header is always sent when using an agent except when the Connection header is explicitly specified or when the keepAlive and maxSockets options are respectively set to false and Infinity, in which case Connection: close will be used. Default: false.