Fundamentals 8 min read

HTTP Keep-Alive vs TCP Keepalive: Key Differences Explained

An in‑depth comparison of HTTP Keep‑Alive (application‑layer persistent connections) and TCP Keepalive (kernel‑level health checks), covering their implementation layers, usage, configuration, timeout handling, and impact on performance, with clear diagrams and code examples to help developers master both mechanisms.

Ops Development Stories
Ops Development Stories
Ops Development Stories
HTTP Keep-Alive vs TCP Keepalive: Key Differences Explained

Hello, I am Xiao Lin.

A reader asked two interview questions: "How does MySQL store NULL values?" and "What is the difference between HTTP Keep-Alive and TCP Keepalive?" The first concerns MySQL record storage; the second concerns persistent connections.

The two concepts are completely different and operate at different layers:

HTTP Keep-Alive is implemented at the application layer (user space) and is called an HTTP persistent connection.

TCP Keepalive is implemented at the TCP layer (kernel space) and is called the TCP keepalive mechanism.

HTTP Keep-Alive

HTTP follows a request‑response model. Each request normally creates a TCP connection, sends the request, receives the response, and then closes the connection, which is called an HTTP short connection.

To avoid the overhead of repeatedly establishing TCP connections, HTTP Keep-Alive allows multiple requests and responses to share a single TCP connection, forming an HTTP long connection.

In HTTP/1.0, Keep-Alive must be enabled by adding the header:

<code>Connection: Keep-Alive</code>

The server also replies with the same header to keep the connection alive.

<code>Connection: Keep-Alive</code>

From HTTP/1.1 onward, Keep-Alive is enabled by default; to disable it, send:

<code>Connection: close</code>

HTTP long connections reduce TCP connection overhead and enable HTTP pipelining, where a client can send multiple requests without waiting for each response, improving overall response time. However, the server still responds in order, which can cause head‑of‑line blocking.

To avoid wasting resources when a connection remains idle, web servers provide a

keepalive_timeout

parameter that closes the connection after a period of inactivity.

TCP Keepalive

TCP Keepalive is the kernel‑level keepalive mechanism. When no data is exchanged for a certain period, the TCP stack sends probe packets to the peer.

If the peer is alive, it responds, and the keepalive timer resets.

If the peer does not respond after several probes, TCP marks the connection as dead.

Applications must enable TCP keepalive by setting the

SO_KEEPALIVE

socket option.

Summary

HTTP Keep-Alive (HTTP long connection) is implemented by the application and allows multiple HTTP requests/responses over a single TCP connection, reducing the overhead of short connections.

TCP Keepalive is implemented by the kernel and periodically probes idle connections to determine if they are still alive, closing them when they are not.

PerformanceTCPHTTPkeep-alivenetworkingprotocol
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.