Why Choose TCP Long Connections Over Short Ones? A Deep Dive
This article explains TCP connection basics, the three‑way handshake and four‑way termination, compares short and long connections, discusses keep‑alive mechanisms, and outlines when to use each approach in network and HTTP communication.
1. TCP Connection
When using TCP for network communication, a connection must be established between server and client before any read/write operations, and released after they finish. Establishing requires a three‑way handshake; releasing requires a four‑way termination, consuming resources and time.
Classic three‑way handshake diagram:
Classic four‑way termination diagram:
TCP state diagram:
Note: When one side receives a FIN, the kernel makes read return 0 to notify the application that the peer has stopped sending data; sending FIN is usually the result of closing the socket at the application layer.
TCP 2MSL problem:
2. TCP Short Connection
In a short‑connection scenario, the client initiates a connection, sends a message, receives the server’s response, and then either side can close the connection, typically the client first. Short connections involve only a single request/response exchange.
Advantages: simpler management, only useful connections exist, no extra control mechanisms needed.
3. TCP Long Connection
In a long‑connection scenario, after the initial request/response the connection remains open for subsequent reads/writes.
TCP keep‑alive: servers use keep‑alive probes to detect half‑open connections. If no activity occurs for two hours, the server sends a probe; the client must be in one of four states (alive, crashed, restarting, or unreachable). After ten probes spaced 75 seconds apart without response, the server assumes the client is dead and closes the connection.
Long connections can lead to resource exhaustion if many idle connections accumulate; servers may need to close idle connections or limit the maximum number of long connections per client.
Choosing between long and short connections depends on the application’s usage pattern; there is no universally optimal choice.
What Are “Long Connection” and “Short Connection”?
Long connection: a socket remains open regardless of data transfer, offering lower security.
Short connection: a socket is closed immediately after data is sent and received; commonly used in banking.
In TCP communications, a long connection keeps the socket open for multiple packets, while a short connection opens a socket only for the duration of data transfer.
HTTP Long vs Short Connections
Long connections keep the TCP link open for continuous message exchange, suitable for frequent, point‑to‑point communication with limited connection counts (e.g., database connections). Short connections close after each request/response, conserving server resources for high‑concurrency web traffic.
Choosing depends on operation frequency, connection count, and resource considerations.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
