# TCP Explained Simply – Three way Handshake

## Why do we need rules to send data?

Imagine two people shouting messages across a crowded room.

* Messages may overlap
    
* Some words may be missed
    
* Order can get mixed up
    

If computers sent data like this, applications would constantly break.

This is exactly why **TCP exists**.

---

## What is TCP and why is it needed?

**TCP (Transmission Control Protocol)** is a communication rule that ensures:

* Data reaches the destination
    
* Data arrives in the correct order
    
* Missing data is detected and resent
    

TCP turns an unreliable network into a **reliable communication channel**.

---

## Problems TCP is designed to solve

Without TCP, networks suffer from:

1. **Packet loss** – data disappears in transit
    
2. **Out-of-order delivery** – pieces arrive mixed up
    
3. **Duplicate packets** – same data arrives twice
    
4. **No confirmation** – sender doesn’t know if data arrived
    

TCP was designed to solve **all of these**.

---

## What is the TCP 3-Way Handshake?

Before sending data, TCP first **establishes a connection**.

This connection setup is called the **3-Way Handshake**.

Think of it like starting a phone call.

---

## Simple conversation analogy

* Client: “Can I talk?”
    
* Server: “Yes, I hear you. Can you hear me?”
    
* Client: “Yes, let’s talk.”
    

Only after this confirmation does real communication begin.

---

## Step-by-step: SYN, SYN-ACK, ACK

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769839117595/fb8e84de-109c-4316-a908-da57cce37b5a.png align="center")

### Step 1: SYN (Synchronize)

* Client sends a **SYN** message
    
* Means: “I want to start a connection”
    
* Includes a starting sequence number
    

---

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769839150079/6f798048-170c-49cd-ad0c-02174ffb28cb.png align="center")

### Step 2: SYN-ACK (Synchronize + Acknowledge)

* Server replies with **SYN-ACK**
    
* Means: “I received your request and I’m ready”
    
* Sends its own sequence number
    

---

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769839168391/d879523b-e745-42eb-9e08-cfe1c41146fc.png align="center")

### Step 3: ACK (Acknowledge)

* Client sends **ACK**
    
* Means: “I received your response”
    

👉 Connection is now established.

---

## How data transfer works in TCP

Once the connection is open:

* Data is broken into segments
    
* Each segment gets a **sequence number**
    
* Receiver sends **ACKs** confirming receipt
    

Data flows in both directions safely.

---

## Sequence numbers (high-level)

Sequence numbers help TCP:

* Track order of data
    
* Detect missing segments
    
* Avoid duplicates
    

Think of pages in a book.

If page 5 is missing, you know something went wrong.

---

## How TCP ensures reliability

TCP ensures reliability using:

* **Acknowledgements (ACKs)** – confirms received data
    
* **Retransmission** – resends missing segments
    
* **Timeouts** – detects lost packets
    

If data is lost, TCP automatically retries.

---

## How TCP ensures correct order

Even if packets arrive out of order:

* TCP buffers them
    
* Reorders them using sequence numbers
    
* Delivers data correctly to the application
    

Applications never see scrambled data.

---

## How TCP handles packet loss

If a packet is lost:

1. Receiver notices missing sequence number
    
2. ACK is not sent
    
3. Sender waits for timeout
    
4. Sender retransmits missing packet
    

All of this happens automatically.

---

## How a TCP connection is closed

TCP closes connections **gracefully**, not suddenly.

This ensures all data is delivered.

---

## Connection termination: FIN and ACK

### Step 1: FIN

* One side sends **FIN**
    
* Means: “I’m done sending data”
    

### Step 2: ACK

* Other side acknowledges the FIN
    

### Step 3: FIN (other direction)

* Second side also sends FIN
    

### Step 4: ACK

* Final acknowledgement
    

👉 Connection is fully closed.

---

## TCP connection lifecycle

1. Connection setup (3-way handshake)
    
2. Data transfer
    
3. Connection termination
    

Every TCP connection follows this lifecycle.

---

## Why backend engineers should care

Understanding TCP helps you:

* Debug slow APIs
    
* Understand timeouts
    
* Diagnose dropped connections
    
* Design reliable systems
    

Many production issues are **TCP-level issues**, not application bugs.

---

## Final takeaway

* TCP makes unreliable networks reliable
    
* The 3-way handshake builds trust
    
* Sequence numbers and ACKs ensure correctness
    
* Retransmission handles packet loss
    
* Graceful termination prevents data loss
    

Once you understand TCP, networking stops being mysterious and starts being logical.
