Fundamentals 16 min read

Understanding Network Protocols, TCP/UDP Handshakes, Socket Long Connections, and Socket Connection Pools

This article explains the OSI seven‑layer model, the principles and handshake processes of TCP and UDP, the differences between them, the concept of long‑living socket connections with heartbeats, custom application‑layer protocols, and how to implement a reusable socket connection pool in Node.js.

Architecture Digest
Architecture Digest
Architecture Digest
Understanding Network Protocols, TCP/UDP Handshakes, Socket Long Connections, and Socket Connection Pools

Preface : Developers often encounter terms like HTTP, TCP/IP, UDP, Socket, long‑living sockets, and socket connection pools, yet many do not fully grasp their relationships, differences, and underlying principles. This article starts from basic network protocol concepts and progresses step‑by‑step to socket connection pools.

Seven‑Layer Network Model : The OSI model consists of Physical, Data Link, Network, Transport, Session, Presentation, and Application layers. IP resides in the Network layer, TCP/UDP in the Transport layer, and HTTP in the Application layer. Sockets are not part of OSI; they will be introduced later with code examples.

TCP and UDP Connections

TCP Three‑Way Handshake and Four‑Way Teardown

1. First handshake : Client sends SYN with sequence number x and enters SYN_SEND state. 2. Second handshake : Server replies with SYN+ACK, acknowledges x+1, and sends its own SYN with sequence number y, entering SYN_RECV. 3. Third handshake : Client sends ACK acknowledging y+1, both sides reach ESTABLISHED state.

After data transfer, the connection is closed via four steps: FIN from initiator, ACK from peer, FIN from peer, and final ACK, after which the initiator stays in TIME_WAIT for 2 MSL to ensure any lost ACKs are retransmitted.

Differences Between TCP and UDP

TCP is connection‑oriented and reliable, using handshakes and acknowledgments; UDP is connection‑less and unreliable, sending packets without confirmation.

Because UDP lacks handshakes, its overhead is lower and transmission speed higher, making it suitable for real‑time scenarios despite the need for application‑level validation.

Common Questions About the Transport Layer

Maximum concurrent TCP connections are not limited by the 65535 port range; they depend on system file descriptor limits and can be increased via ulimit -n and kernel parameters.

TIME_WAIT persists for 2 MSL to allow retransmission of a possibly lost final ACK.

Excessive TIME_WAIT sockets can exhaust local ports, causing "address already in use" errors; tuning net.ipv4.tcp_tw_reuse , net.ipv4.tcp_tw_recycle , and related sysctl settings mitigates the issue.

Socket Long Connections

A long‑living connection allows multiple data packets to be sent over a single TCP socket, with periodic heartbeat packets to keep the connection alive. Short connections open, transmit, and close for each request, which incurs handshake overhead.

Long connections are preferable for frequent, point‑to‑point communication where connection setup cost is significant, such as database connections.

Heartbeat Packets

Heartbeats are periodic messages that inform the peer of the connection’s alive status, helping detect broken sockets and maintain reliability.

Defining a Custom Application‑Layer Protocol

Define heartbeat packet format.

Define a message header that includes the payload length.

Choose a serialization format (e.g., JSON).

Example header: length:000000000xxxx where the last four digits represent the data length (total header length 20 bytes).

Socket Connection Pool

A socket pool maintains a collection of reusable long‑living socket connections, automatically checking their health, discarding invalid ones, and replenishing the pool as needed. It manages queues for idle sockets, active sockets, and pending requests.

When a request arrives, the pool provides an idle socket; if none are available and the active count is below the configured limit, a new socket is created; otherwise the request waits.

Implementation in Node.js often uses the generic-pool library. The article shows the directory structure, pool initialization, and usage with the previously defined custom protocol.

Source Code Analysis

The core implementation resides in lib/Pool.js , which defines queues for idle, active, and waiting resources. The acquire method handles the logic of obtaining a socket, creating new ones when needed, and managing the queues.

Overall, the article provides a comprehensive guide from networking fundamentals to practical socket pool implementation in a backend Node.js environment.

Node.jsConnection PoolTCPnetwork protocolsSocketUDP
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

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.