Fundamentals 12 min read

Short vs Long TCP Connections: When and Why to Use Each

This article explains the fundamentals of TCP connections, detailing the three‑way handshake, four‑step termination, the differences between short‑lived and persistent (long) connections, their advantages, keep‑alive mechanisms, and practical guidelines for choosing the appropriate connection type in various network scenarios.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Short vs Long TCP Connections: When and Why to Use Each

1. TCP Connection

When using TCP, a connection must be established before any read/write operation and released after communication ends. Establishment requires a three‑way handshake, while termination requires a four‑step handshake, both consuming resources and time.

Classic three‑way handshake diagram:

Classic four‑step termination diagram:

TCP state diagram (ten states):

Note: When one side receives a FIN, the kernel makes read return 0 to notify the application that the peer has terminated data transmission; sending FIN usually results from the application closing the socket.

TCP 2MSL problem diagram:

2. TCP Short Connection

In a short‑connection scenario, the client initiates a connection, sends a message, receives a response, and then either side can close the connection, typically the client. Short connections are simple to manage and only exist while useful.

3. TCP Long Connection

In a long‑connection scenario, after the initial handshake, the client and server keep the connection open for multiple read/write operations. TCP keep‑alive can be used: if no activity occurs for a configured period (e.g., two hours), the server sends probe packets; after several unanswered probes, the connection is considered dead and closed.

Long connections can lead to resource exhaustion if many idle connections accumulate, so servers may close long‑idle connections or limit the maximum number of concurrent long connections per client.

What are “long connection” and “short connection”?

Long connection: a TCP socket remains open regardless of data transfer, offering lower latency for frequent communication but consuming more resources. Short connection: the socket is opened, data is exchanged, and then immediately closed, freeing resources quickly.

HTTP Protocol Long and Short Connections

Short connection: client establishes a connection, sends a request, receives a response, and closes the connection. Long connection (persistent HTTP): the client and server keep the TCP connection open for multiple requests/responses, reducing handshake overhead.

When to use long vs short connections

Use long connections for frequent, point‑to‑point communication where the number of simultaneous connections is limited (e.g., database connections). Use short connections for high‑concurrency scenarios like web servers, where each client performs brief requests and keeping many connections open would strain resources.

Sending and receiving methods

Asynchronous: sending and receiving are independent, can be full‑duplex or half‑duplex.

Synchronous: send a message and wait for a response, typically with a timeout.

Message formats and I/O models

Non‑blocking I/O: read attempts return immediately with a timeout if no data is available.

Blocking I/O: read blocks until data arrives.

Looped read/write: handle partial reads/writes by looping until the full message is transferred, optionally using a length header for framing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TCPKeep-AliveNetworkinglong-connectionshort connection
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

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.