Keep-Alive connection on inter-service HTTP requests

Tomorrow Tech Reviews
2 min readJan 17, 2019

--

Each HTTP request to the server we’ll 1) open a connection to the server 2) send a request to the server 3) receive data 4) close this connection.

If we use HTTP request as communication between microservices this can be a bottleneck. Server spends some time and resources to open and close a connection. What if we can use a persistent connection for it?

Schema of multiple vs. persistent connection. [wikipedia.org]

HTTP persistent connection, also called HTTP keep-alive, or HTTP connection reuse, is the idea of using a single TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair. The newer HTTP/2 protocol uses the same idea and takes it further to allow multiple concurrent requests/responses to be multiplexed over a single connection. [wikipedia.org]

Let’s test it on Node.js

I did two tests with small (c5.large — 2 vCPU) and big AWS instances (c5.4xlarge-16 vCPU). I use three instances in the same region and the same AZ.

Microservice

  • Ubuntu Server 18.04 LTS
  • Node.js (without Nginx)
  • Node v10.15.0 LTS
  • PM2
  • Express 4.16.4

Load testing server

  • Ubuntu Server 18.04 LTS
  • wrk

I use WRK with 1000 open connections to send GET requests to Service A. Service-A send a request to the Service-B and return JSON data.

Communication schema

Source Code

Results

So, by using keep-alive we save resources and speed up requests. Huge difference you’ll see with fast/lightweight requests and powerful servers.

This is how we can enable keep-alive in Node.js for all request with a single line of code.

const http = require(‘http’);http.globalAgent.keepAlive = true;

I hope you like this experiment.

--

--