TCP in Depth
Tags: networking, backend, programming, beginners
Published: 2021-08-30 01:55:13

<!-- wp:paragraph -->
<p>As backend engineers, we really like our black boxes.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We don't enjoy getting into the nitty-gritty details.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>One of the topics that backend engineers avoid learning are communication protocols.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Sure you know what is HTTP, and how it works on the surface level.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But do you know how packets get transferred?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You don't?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Don't worry, it's not the end of the world.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this article, I will go over one of the three main internet protocols, which powers the world wide web.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>In this article you will learn:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>What the TCP protocol is</li><li>TCP Packet Format</li><li>The different stages of the TCP protocol</li><li>Features of the TCP protocol</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>What is the TCP protocol?</h2>
<!-- /wp:heading -->

<!-- wp:image {"align":"center","id":683,"sizeSlug":"full","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-full"><img src="https://softwareadventuring.com/wp-content/uploads/2021/08/1-2.png" alt="" class="wp-image-683"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>TCP which stands for <strong>transmission control protocol</strong> is one of the main protocols in the internet suite. It is used on top of the IP (internet protocol) to ensure reliable transmission of packets.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>TCP fixes many issues that arise when you use IP such as lost packets, out of order packets, duplicate packets, and corrupted packets.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Since TCP is most commonly used on top of IP, it is referred to as TCP/IP.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now that we know the basics of TCP, let us go a bit deeper and discuss the packet format of TCP.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>TCP Packet Format</h2>
<!-- /wp:heading -->

<!-- wp:image {"align":"center","id":684,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://softwareadventuring.com/wp-content/uploads/2021/08/2-2-1024x377.png" alt="" class="wp-image-684"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The figure above shows the difference between the TCP and the IP packet. As you can see the TCP has a lot more fields in the header, this is made to ensure reliability.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let us briefly go over each field in the TCP header:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>Source Port Number</strong> — The sending device port.</li><li><strong>Destination Port Number</strong> — The receiving device port.</li><li><strong>Sequence Number</strong> — The initial client initiating the connection must choose a random sequence number, which is then incremented according to the number of bytes.</li><li><strong>Acknowledgement Number</strong> — This field holds the sequence number of the next byte the receiver expects. The acknowledgement field is defined only if the ACK field is set.</li><li><strong>Offset</strong> — Size of the TCP header.</li><li><strong>Reserved</strong> — This field must always be zero, it is reserved for future use.</li><li><strong>Flags fields</strong> — TCP uses nine control flags to manage data flow in specific situations:<ul><li><strong>URG</strong> — Indicates that some urgent data has been placed.</li><li><strong>ACK</strong> — Indicates that acknowledgement number is valid.</li><li><strong>PSH</strong> — Indicates that data should be passed to the application as soon as possible.</li><li><strong>RST</strong> — Resets the connection.</li><li><strong>SYN</strong> — Synchronizes sequence numbers to initiate a connection.</li><li><strong>FIN</strong> — Means that the sender of the flag has finished sending data.</li></ul></li><li><strong>Window Size</strong> — This field is used by the receiver to indicate to the sender how much data it can accept.</li><li><strong>Checksum</strong> — Indicates whether the header was damaged in transit.</li><li><strong>Urgent Pointer</strong> — Returns the urgent pointer of a TCP segment. If the URG flag is set, this field is an offset from the sequence number indicating the last urgent data byte.</li><li><strong>Options/Padding</strong> — Specifies various TCP options.</li><li><strong>Data</strong> — Contains the information that your sending.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>TCP Phases</h2>
<!-- /wp:heading -->

<!-- wp:image {"align":"center","id":685,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://softwareadventuring.com/wp-content/uploads/2021/08/3-1-1024x683.png" alt="" class="wp-image-685"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Let's go through the process of transmitting a packet using TCP.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Connection Phase</h3>
<!-- /wp:heading -->

<!-- wp:image {"align":"center","id":686,"sizeSlug":"full","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-full"><img src="https://softwareadventuring.com/wp-content/uploads/2021/08/4-1.png" alt="" class="wp-image-686"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Before we send data using TCP, we first have to establish a connection.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The client and server must know each other.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>They go through a process known as the TCP three-way handshake.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's go through the process:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The client sends a packet with the SYN flag set to 1 along with it's random sequence number.</li><li>The server sends a packet with both the SYN and ACK flags set to 1.</li><li>Finally the client sends a packet with the ACK flag set to 1 indicating that it acknowledges the last request.</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Data Transfer Phase</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now that the client and server are acquainted with each other, they are ready to send and receive data to each other.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is essentially no man's land, but there are two very important rules that every request must follow:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Every request must be followed by an ACK request, acknowledging that the receiver received the previous request.</li><li>If the receiver doesn't send the ACK request, this indicates to the user that the receiver did not receive the request, and will re-transmit it again.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Connection Termination</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Once the client and server are satisfied, the connection is finally ready to be closed.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's go through this process:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Either the client or the receiver can initiate the termination by sending a request with the FIN flag set to 1.</li><li>The receiver will respond back with the FIN and ACK flags set to 1.</li><li>One more ACK request is needed, and finally the connection is closed.</li></ol>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Features of TCP</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now that you know most of the nitty details of the TCP protocol.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's go over some of the features that TCP provides us.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Connection Based</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Every TCP connection must be stateful, meaning the client and server must know each other. This is both good and bad, it honestly depends on your application use case. Some apps need a stateful connection while others don't.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Congestion Control</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>TCP has several different methods for controlling congestion or traffic. We do this because we don't want to overload our servers. Methods in controlling congestion include:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>TCP Slow Start</li><li>Congestion Avoidance</li><li>Fast Retransmit</li><li>Fast Recovery</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Acknowledgement</h3>
<!-- /wp:heading -->

<!-- wp:image {"align":"center","id":687,"sizeSlug":"full","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-full"><img src="https://softwareadventuring.com/wp-content/uploads/2021/08/6-1.png" alt="" class="wp-image-687"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>As we have seen in the previous section, every request must be acknowledged in TCP. We do this to make sure no packets are lost, and this helps us handle packets that are out of order.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>TCP is pretty much used everywhere, but you would very rarely use it raw. Most of the time you would use another protocol that is built on top of TCP such as HTTP, Web Sockets, SMTP, FTP, etc...</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Many developers feel overwhelmed by the sheer amount of protocols, but the secret is to study from first principles. Most of these protocols come up from either TCP or UDP, once you know these two, the others are simply variants that are easy to learn.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Before we end it off, I would like to say if you enjoy my content feel free to follow me on Twitter @<a href="https://twitter.com/tamerlan_dev">tamerlan_dev</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Thanks for reading, if I missed anything feel free to leave them down in the comments and start a discussion.</p>
<!-- /wp:paragraph -->