How HTTP Keep-Alive will increase the scalability of your website

Imagine a busy highway. Every car leaving your house needs to exit, come to a complete stop, and then re-enter the highway to visit each store. This would be incredibly inefficient, right? HTTP requests work similarly by default, creating a new connection for each request. This can overwhelm your server on a large scale because lot of resources like CPU and memory will go into creating new connections.

Courtesy: HAPROXY

Enter HTTP Keep-Alive: a game-changer for website performance and scalability. Let’s explore how it works and how it can benefit your website.

What is HTTP Keep-Alive?

HTTP Keep-Alive, also known as persistent connections, allows a single TCP connection to handle multiple HTTP requests and responses. This eliminates the need to constantly establish new connections for each request thus significantly improving efficiency.

Think of it like keeping the highway exit open. Cars can now visit multiple stores on the same trip, reducing congestion and speeding up travel.

How Does Keep-Alive Improve Scalability?

Here’s how Keep-Alive benefits your website’s ability to handle increased traffic:

Reduced Overhead
Setting up a new TCP connection requires a “three-way handshake,” consuming resources. Keep-Alive eliminates this for subsequent requests, freeing up server resources.

Faster Response Times
No need to wait for a new connection each time. This translates to quicker page loads and a smoother user experience.

Lower Server Load
With fewer connections to manage, your server can handle more concurrent requests without getting overloaded.

Keep-Alive in Action

When a user visits your website with Keep-Alive enabled, their browser sends a header indicating it wants to reuse the connection. Your server acknowledges this and keeps the connection open for subsequent requests on the same page. This allows the browser to efficiently download all the website’s components (images, CSS, JavaScript) through a single connection.

Keep-Alive Best Practices

While Keep-Alive is generally beneficial, consider these points for optimal performance:

Timeout
Set a timeout value to prevent inactive connections from tying up resources.

Limits
Configure the maximum number of requests allowed per connection to avoid keeping idle connections open for too long.

By implementing Keep-Alive with these considerations, you’ll create a more scalable and efficient web experience for your users.

How to enable this setting?

Most of the web servers have this setting. Below are a few servers and how you can enable it.

HAProxy: https://www.haproxy.com/blog/http-keep-alive-pipelining-multiplexing-and-connection-pooling

Nginx: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive

Apache: https://httpd.apache.org/docs/2.4/mod/core.html#keepalive

If you like the small write-up, do share and keep following this space.


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.