Demystifying TCP Connection Establishment in the World of Back-End Development

Tahseen Rasheed
2 min readDec 23, 2023

--

Photo by Roman Synkevych on Unsplash

Introduction:

In the realm of back-end development, understanding the intricacies of TCP connection establishment is crucial for optimizing performance and addressing potential bottlenecks. In this blog post, we will delve into the process of how connections are established, focusing specifically on TCP, the ubiquitous protocol that forms the backbone of many applications.

The TCP Three-Way Handshake:

The cornerstone of TCP connection establishment is the three-way handshake. When a client initiates a connection, it sends a synchronization request (SYN) packet to the server. The goal is to synchronize sequence numbers between the client and server, establishing a shared understanding of communication. The server responds with a SYN-ACK, indicating its readiness to sync, and finally, the client acknowledges with an ACK, completing the three-way handshake.

Back-End Connection Lifecycle:

Now, let’s zoom in on the back-end and explore how the kernel handles incoming connection requests. When a server listens on a specific address and port, it creates a socket, a unique identifier for the connection. Additionally, the kernel establishes two queues — the SYN queue and the accept queue.

As the client sends a SYN packet, the kernel adds the relevant information to the SYN queue. Upon receiving the SYN, the server responds with a SYN-ACK, and once the client acknowledges, the connection is moved to the accept queue. The back-end process then accepts the connection, removing it from the accept queue and creating a file descriptor that uniquely identifies it.

Addressing Common Issues:

One critical consideration is the listening address. It’s essential to specify the address explicitly to avoid accidentally exposing the back end to the public internet. This practice helps prevent security risks, such as unintentional database leaks.

Another potential challenge is the backlog size, which determines the number of pending connections in the queue. While increasing the backlog size can handle more concurrent connections, it also requires more memory. Striking a balance is crucial for optimal performance.

Shady Clients and SYN Flooding:

Shady clients engaging in SYN flooding can pose a threat by sending SYN packets without completing the handshake. Solutions like SYN cookies help mitigate this issue by making the handshake stateless, transferring necessary state information with the SYN itself.

Conclusion:

Understanding the journey of a connection from initiation to back-end processing sheds light on the complex yet fascinating world of TCP connection establishment. As back-end developers, this knowledge empowers us to optimize performance, address potential challenges, and contribute to the improvement of the underlying infrastructure.

In the ever-evolving landscape of back-end development, continuous learning and exploration are key. Whether you’re left with questions or inspired to contribute to the Linux kernel, the journey is ongoing, and the pursuit of perfection in the back-end realm is a never-ending adventure.

--

--