KAHIBARO
Discord Login Register

2.6. TCP/IP Basics

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 EquivalentExample ProtocolsResponsibility
Application5–7HTTP, HTTPS, DNS, SMTPWhat your app works with directly
Transport4TCP, UDPMoving data between processes, ports, reliability
Internet3IP, ICMPMoving packets between devices, routing
Network Access1–2Ethernet, Wi-FiActual 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:

  1. IP for addressing and routing between machines.
  2. TCP for reliable, ordered delivery between ports on those machines.
  3. An application protocol like HTTP to define the meaning of the data.

IP: Internet Protocol

What IP Does

IP (Internet Protocol) provides:

An IP packet contains:

IP does not guarantee:

It is best effort only.

IPv4 vs IPv6

There are two main versions:

VersionLengthExample address
IPv432 bits192.168.1.10
IPv6128 bits2001: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:

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:

A port is like a numbered door on a machine. Some common ports:

ServiceDefault PortProtocol
HTTP80TCP
HTTPS443TCP
PostgreSQL5432TCP
Redis6379TCP

A socket is normally identified as:

text
<source_ip>:<source_port> -> <dest_ip>:<dest_port>

For example:

text
192.168.1.10:51123 -> 93.184.216.34:443

This 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:

  1. Client → Server: SYN
    • Client asks to start a connection.
  2. Server → Client: SYN-ACK
    • Server agrees and acknowledges.
  3. 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:

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:

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 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:

python
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:

This matters when your backend communicates over unreliable networks. Your application can assume:

You do not have to manually re-send HTTP request bodies or responses on packet loss.

Flow Control and Congestion Control

TCP also includes:

This adaptivity influences throughput and latency of your backend:

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:

FeatureTCPUDP
ReliabilityReliable, orderedNo guarantee
ConnectionConnection-oriented (handshake)Connectionless
Data viewStream of bytesIndividual messages (datagrams)
Typical use casesHTTP, HTTPS, databasesDNS, streaming, some metrics

Backend developers may encounter UDP in contexts like:

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:

text
GET / HTTP/1.1
Host: example.com
User-Agent: curl/8.0
Accept: */*

What happens at each layer?

  1. Application layer (HTTP)
    • Your client formats this as an HTTP request.
  2. Transport layer (TCP)
    • Client opens a TCP connection to example.com on port 80 or 443.
    • 3-way handshake occurs.
    • HTTP request bytes are sent over the TCP stream.
  3. Internet layer (IP)
    • TCP segments are wrapped in IP packets.
    • Each packet has source IP and destination IP.
    • Routers forward packets across the internet.
  4. Network access layer
    • Packets travel over Ethernet, Wi-Fi, etc.

On the server side, the reverse happens:

Practical Implications for Backend Developers

Connection Costs and Keep-Alive

Each new TCP connection costs:

HTTP clients and servers use keep-alive to reuse TCP connections for multiple requests. This:

As a backend developer you should:

Timeouts

Network calls in your backend can:

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:

If you run a high-traffic backend, you must:

Summary

With these basics, you are ready to better understand HTTP, HTTPS, and everything that follows in backend development.

Views: 9

Comments

Please login to add a comment.

Don't have an account? Register now!