Understanding HTTP and TCP/IP: Long vs Short Connections Explained
This article explains how HTTP relies on TCP/IP, clarifies the stateless nature of HTTP, and compares long and short connections—including their TCP handshakes, keep‑alive mechanisms, advantages, disadvantages, and appropriate usage scenarios for web and database communications.
1. Relationship between HTTP and TCP/IP
HTTP's long and short connections are essentially TCP long and short connections. HTTP is an application‑layer protocol that uses TCP at the transport layer and IP at the network layer. IP handles routing and addressing, while TCP provides reliable, ordered delivery of data packets.
2. Understanding HTTP Statelessness
HTTP is stateless, meaning the protocol itself does not retain any memory of previous transactions; the server does not know the client's prior state. This does not prevent TCP from keeping a connection alive, nor does it imply that HTTP uses UDP.
3. What are Long and Short Connections?
In HTTP/1.0 the default is a short connection: each request creates a new TCP connection that is closed after the response. In HTTP/1.1 the default is a long (persistent) connection, indicated by the header Connection:keep-alive. With a persistent connection, the TCP link remains open after a page load, allowing subsequent requests to reuse the same connection.
3.1 TCP Connection
Establishing a TCP connection requires a three‑way handshake; closing it requires a four‑step termination, consuming resources and time for each connection.
3.2 TCP Short Connection
A short connection involves a single request‑response cycle followed by closure, typically initiated by the client. It is simple to manage and ensures that all connections are actively used.
3.3 TCP Long Connection
A long connection keeps the TCP link open after the first request, allowing multiple subsequent data transfers without re‑handshaking. TCP keep‑alive probes can detect half‑open connections, with four possible outcomes for the client host.
The client is running and reachable; the server resets its keep‑alive timer.
The client has crashed or is restarting; no response is received, and after several probes the server closes the connection.
The client crashed and restarted; the server receives a reset and terminates the connection.
The client is running but the server is unreachable; similar to case 2, no response is received.
3.4 Operation Process
Short connection flow: establish → data transfer → close (repeated for each request).
Long connection flow: establish → data transfer → (keep connection) → further data transfers → close.
4. Advantages and Disadvantages
Long connections reduce the overhead of repeatedly establishing and closing TCP sessions, benefiting frequent requests but can consume server resources and require keep‑alive timeout tuning. Short connections are simpler and use resources only when needed, but frequent requests incur handshake overhead.
5. When to Use Long or Short Connections
Use long connections for frequent, point‑to‑point communication where connection count is limited (e.g., database connections). Use short connections for high‑concurrency web services where each client makes infrequent requests, conserving server resources.
Source: WhyWin (original article: http://www.cnblogs.com/0201zcr/p/4694945.html)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
