Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
Node.js sharing the same HTTP agent across service calls

I have a node.js backend that connects to external service via HTTP request. I am using the node.js HTTP to manage my requests. My question is about how to properly use Node.js http Agent with the keep-alive option.

The official documentation explains that we should create a new Agent and pass it to the request. This is the same for all the examples I found. They all do something like this:

const http = require("http");

const agent = new http.Agent({
    keepAlive: true
});

const options = {
  host: 'myserver.com',
  port: 80,
  path: '/',
  method: 'GET',
  agent: agent,
};
  
const req = http.request(options, (res) => {
  console.log("StatusCode: ", res.statusCode);
});
  1. Since my agent has keepAlive turned on I should create only one instance of the agent, save it somewhere (static?) and reuse it everytime I do a request to that service. In other words, I should only call new http.Agent one time when my backend is created and keep using the same instance, otherwise I would be created a new agent for every request. Is this correct? is there a better way of keeping the agent instance other than making it global or static?

  2. I don't know if I'll be able to get an answer here or better as AWS directly, but let's say my backend is a AWS lambda fucntion, how can they share Agents between invocations, as lambdas are short lived, does it even make sense to have keepAlive for that use case?

Thank you for any clarification.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda