http javascript node.js

Node.js: Relationship among http.Server, http.Agent, sockets, and http.request

Per the docs:

Node.js maintains several connections per server to make HTTP requests. This function allows one to transparently issue requests.

The docs further specify that Node relies on http.globalAgent to make requests by default, but that you can use your own agent by creating a new http.Agent. Agents are used to “pool sockets” for http requests.

My interpretation of all this is that each time you do a http.createServer, by default you get several sockets (presumably that is what is meant by “connections”) to make http requests and these sockets are pooled / managed by http.globalAgent.

What isn’t clear to me is what happens when you create your own http.Agent. Does the Agent just “take over” the sockets that were previously being managed by http.globalAgent? Or do you have to create a new socket for your new Agent via agent.createConnection?

On a related note, if I were to start up two servers in the same node process and subsequently make an http request, e.g.

const server1 = http.createServer(function(req, res) {
res.end('Hello from server1');
const server2 = http.createServer(function(req, res) {
res.end('Hello from server2');

from which server would the request be made? Does http.Agent come into play here?

HTTP Agent

Why do we need the Agent?

The process of establishing a connection from one host to another involves multiple packet exchanges between two endpoints, this can be quite time consuming, especially for multiple small http requests. The Agent helps achieve a much higher data throughput by keeping the connections open and by reusing them to handle multiple requests from the same host.

What does the Agent do?

The Agent manages connection persistence for HTTP clients. It maintains a queue of pending requests for a given host and port, reusing a single socket connection for each until the queue is empty. After that, the socket is destroyed, if the keepAlive is set to false. If the keepAlive is set to true, the socket is put into a pool where it is kept to be used again for requests to the same host and port. However, servers may still close idle connections.

Socket Management

When a connection is closed by the client or the server, it is removed from the pool. Any unused sockets in the pool will be unrefed so as not to keep the Node.js process running when there are no outstanding requests.

If using an agent with keepAlive enabled, then it is best to explicitly shut down the agent when it will no longer be used. Otherwise, sockets may hang open for quite a long time before the server terminates them. Unused sockets consume OS resources.

HTTP globalAgent

The http.request() or http.get() method is used to make http requests to other servers from your Node.js server or your Node.js client app. For making these requests, the http.globalAgent is used by default.

http.globalAgent is a singleton object. In the source code of Node.js, the http.globalAgent is created using new http.Agent() with default options.

Each agent object has its own array of sockets. So, when you create a custom agent using http.Agent, it will have a different set of sockets from http.globalAgent. The unused sockets of the globalAgent will be destroyed automatically after the timeout.

Multiple Servers in a Process

As specified in question, if you start the two servers in the same node process and make the http request after the server code, the request will go through the same globalAgent. The server 1 and 2 are irrelevant to the http.get() request made in your code. The http.get() will be executed outside of the server code. However, you can also make an http request to another server from your server code as shown below:

const server1 = http.createServer(function(req, res) {
res.end('The request has been made from server1');

If you don’t specify your custom agent using the new http.Agent() or if you don’t specify {agent : false} in your request options, the http.globalAgent will be used for the code above too.


The function agent.createConnection() is used when you want to avoid creating a custom agent as well as http.globalAgent. This function directly gives you a socket for making your http request.

Hope this clears all your doubts about the HTTP Agent!