2.6. TCP/IP Basics
Table of Contents
Why TCP/IP Matters to Backend Developers
When you build backend applications, every HTTP request, database call, or API call between services travels over the network. The foundation of almost all of this traffic is a set of protocols called TCP/IP.
You do not need to become a network engineer, but you should understand what TCP/IP does and why it affects performance, reliability, and security of your backend.
This chapter gives you a practical, backend-focused view of TCP/IP. We will avoid deep theory and focus on what helps you reason about real backend behavior.
The TCP/IP Model in Simple Terms
Most backend traffic uses the TCP/IP protocol suite. People often draw it as layers, each with its own job.
Here is a simplified mapping that is enough for backend work:
| Layer (TCP/IP) | Rough OSI Equivalent | Example Protocols | Responsibility |
|---|---|---|---|
| Application | 5–7 | HTTP, HTTPS, DNS, SMTP | What your app works with directly |
| Transport | 4 | TCP, UDP | Moving data between processes, ports, reliability |
| Internet | 3 | IP, ICMP | Moving packets between devices, routing |
| Network Access | 1–2 | Ethernet, Wi-Fi | Actual physical network and local delivery |
You will mainly deal with Application and Transport layers, but you must know that everything ultimately rests on IP at the Internet layer.
Important:
Every HTTP request uses at least three key components of TCP/IP:
- IP for addressing and routing between machines.
- TCP for reliable, ordered delivery between ports on those machines.
- An application protocol like HTTP to define the meaning of the data.
IP: Internet Protocol
What IP Does
IP (Internet Protocol) provides:
- A way to identify machines with IP addresses
- A way to route packets from one machine to another, possibly across many networks
An IP packet contains:
- Source IP address
- Destination IP address
- Some metadata (like Time To Live)
- Payload (for example, a TCP segment with your HTTP data)
IP does not guarantee:
- Delivery
- Order
- Protection from duplication
It is best effort only.
IPv4 vs IPv6
There are two main versions:
| Version | Length | Example address |
|---|---|---|
| IPv4 | 32 bits | 192.168.1.10 |
| IPv6 | 128 bits | 2001:0db8:85a3::8a2e:0370:7334 |
For most beginner backend work, you can assume IPv4, but modern systems increasingly use IPv6 as well.
TCP: Transmission Control Protocol
Why TCP Is Important for Backends
TCP sits above IP and is what most backends use for HTTP, database connections, and many other protocols.
It provides:
- Reliable delivery: Lost segments are detected and retransmitted.
- Ordered delivery: Data arrives to the application in the correct order.
- No duplicates to the application: Duplicates are handled at the TCP layer.
- Stream abstraction: The app sees a continuous stream of bytes, not individual IP packets.
This is why your application can send a big HTTP response without worrying about how it is split across packets.
Ports and Sockets
TCP identifies each communication channel using:
- Source IP, source port
- Destination IP, destination port
A port is like a numbered door on a machine. Some common ports:
| Service | Default Port | Protocol |
|---|---|---|
| HTTP | 80 | TCP |
| HTTPS | 443 | TCP |
| PostgreSQL | 5432 | TCP |
| Redis | 6379 | TCP |
A socket is normally identified as:
<source_ip>:<source_port> -> <dest_ip>:<dest_port>For example:
192.168.1.10:51123 -> 93.184.216.34:443This might be your browser talking to an HTTPS web server.
As a backend developer, when you run a web server on port 8000 and your logs show 127.0.0.1:54123, that is a client socket connecting to your server’s listening port.
TCP Connections and the 3-Way Handshake
Opening a TCP Connection
Before HTTP data can flow, TCP must establish a connection. This is the 3-way handshake.
Conceptually:
- Client → Server: SYN
- Client asks to start a connection.
- Server → Client: SYN-ACK
- Server agrees and acknowledges.
- Client → Server: ACK
- Client confirms.
After this, the connection is established and both sides can send data.
As a backend developer, each inbound HTTP request over a new TCP connection costs:
- One TCP handshake (1 round trip)
- Possibly an extra TLS handshake if using HTTPS
This is one reason connection reuse and keep-alive matter for performance.
Closing a TCP Connection
To close, there is a similar process with FIN and ACK messages. Most of the time your backend code does not handle this directly. The OS and language runtime do it for you.
TCP Streams, Segmentation, and Reassembly
Streams, Not Messages
TCP presents data to your application as a byte stream:
- You write bytes to the socket.
- The other side reads bytes from the socket.
TCP does not know anything about HTTP or JSON. It just moves bytes.
Segmentation
If your backend responds with a large JSON payload, TCP may split it into multiple segments to fit into IP packets.
Example:
- Your handler writes 30 KB of JSON.
- TCP might send 20 KB in one segment and 10 KB in the next.
Your code does not manage this splitting.
Reassembly
On the receiving side, TCP reassembles out-of-order or fragmented segments so the application sees a continuous stream.
This is why in your HTTP client you just call something like:
response = httpx.get("https://example.com")
print(response.text)You do not loop over IP packets. TCP handles the packet-level complexity.
TCP Reliability and Flow Control
Reliability
TCP ensures reliable delivery with:
- Sequence numbers
- Acknowledgments (ACKs)
- Retransmission of lost segments
- Checksums to detect corrupted data
This matters when your backend communicates over unreliable networks. Your application can assume:
- Either data is delivered correctly, or the connection fails.
You do not have to manually re-send HTTP request bodies or responses on packet loss.
Flow Control and Congestion Control
TCP also includes:
- Flow control between two endpoints so a fast sender does not overwhelm a slow receiver.
- Congestion control so the network is not overloaded.
This adaptivity influences throughput and latency of your backend:
- Long-distance connections and congested networks will have higher latency and potentially lower bandwidth.
- Many concurrent connections can affect how quickly each client gets data.
You rarely tune these directly, but you should know they exist.
UDP: The Other Transport Protocol
While most backend HTTP traffic uses TCP, there is another important transport protocol: UDP (User Datagram Protocol).
Differences from TCP:
| Feature | TCP | UDP |
|---|---|---|
| Reliability | Reliable, ordered | No guarantee |
| Connection | Connection-oriented (handshake) | Connectionless |
| Data view | Stream of bytes | Individual messages (datagrams) |
| Typical use cases | HTTP, HTTPS, databases | DNS, streaming, some metrics |
Backend developers may encounter UDP in contexts like:
- DNS queries
- Metrics protocols such as StatsD
- Some internal service discovery tools
For web APIs and database communication, you will nearly always use TCP.
How HTTP Rides on TCP/IP
To connect this to the rest of the course, consider a simple HTTP request:
GET / HTTP/1.1
Host: example.com
User-Agent: curl/8.0
Accept: */*What happens at each layer?
- Application layer (HTTP)
- Your client formats this as an HTTP request.
- Transport layer (TCP)
- Client opens a TCP connection to
example.comon port 80 or 443. - 3-way handshake occurs.
- HTTP request bytes are sent over the TCP stream.
- Internet layer (IP)
- TCP segments are wrapped in IP packets.
- Each packet has source IP and destination IP.
- Routers forward packets across the internet.
- Network access layer
- Packets travel over Ethernet, Wi-Fi, etc.
On the server side, the reverse happens:
- Packets arrive at the server.
- IP delivers them to TCP.
- TCP reassembles the stream and passes bytes to the HTTP server.
- The HTTP server decodes the request and your backend code runs.
Practical Implications for Backend Developers
Connection Costs and Keep-Alive
Each new TCP connection costs:
- A 3-way handshake, at least 1 network round trip
- Possible TLS handshake for HTTPS
HTTP clients and servers use keep-alive to reuse TCP connections for multiple requests. This:
- Reduces latency
- Decreases CPU overhead
- Improves throughput
As a backend developer you should:
- Avoid closing connections unnecessarily.
- Be aware that some reverse proxies and load balancers have connection timeout settings.
Timeouts
Network calls in your backend can:
- Hang if the other side does not respond.
- Fail if the connection breaks.
You must set reasonable timeouts when making HTTP or database calls to avoid stuck requests and resource leaks.
Max Connections and Resource Limits
Each TCP connection consumes:
- File descriptors
- Memory
- Other OS-level resources
If you run a high-traffic backend, you must:
- Set appropriate connection limits.
- Use connection pooling for databases.
- Understand that too many open connections can exhaust server resources.
Summary
- The TCP/IP stack is the foundation for almost all backend network communication.
- IP provides addressing and routing using IP addresses. It is best effort.
- TCP sits on top of IP to provide reliable, ordered, stream-based communication between ports.
- Ports and sockets let multiple services run on a single machine.
- The 3-way handshake sets up a TCP connection before data flows.
- TCP handles segmentation, reassembly, reliability, and flow control so your application can think in terms of streams of bytes, not packets.
- UDP is a faster but unreliable alternative used for specific protocols like DNS.
- HTTP and HTTPS are application protocols that ride on top of TCP/IP, which is why understanding TCP/IP helps you debug and tune backend behavior.
With these basics, you are ready to better understand HTTP, HTTPS, and everything that follows in backend development.
Views: 9
KAHIBARO