Networking for Developers
Understanding the Pipes of the Internet

Most backend issues are actually network issues. "Why can't Service A talk to Service B?" "Why is the connection timing out?" Understanding the fundamental layers of networking will save you hours of debugging.

Table of Contents

The Models: OSI vs TCP/IP

You don't need to memorize every detail, but you do need to know where things happen.

Layer OSI Name Developer's Reality (Examples)
7 Application HTTP, HTTPS, SSH, FTP, WebSocket (Your Code lives here)
4 Transport TCP, UDP (Ports live here)
3 Network IP, ICMP (IP Addresses live here)
2 Data Link Ethernet, MAC Addresses (Switches live here)

Note: Layers 5 (Session) and 6 (Presentation) are often handled within the Application layer in modern web development.

TCP vs UDP: Reliability vs Speed

TCP (Transmission Control Protocol)

TCP is the polite, reliable protocol. It cares about feelings.

UDP (User Datagram Protocol)

UDP is the honey badger of protocols. It doesn't care.

TCP: reliable + ordered + congestion control UDP: fast + unordered + no delivery guarantee

The TCP 3-Way Handshake

Before any data is exchanged in TCP (like loading this webpage), your computer and the server perform a specific ritual.

Client (You) Server (Locallhost.dev) | | | ------- SYN (Let's talk!) ------------> | (Listening) | | | <---- SYN-ACK (Okay! I can talk!) ----- | | | | ------- ACK (Great! Connected!) ------> | | | | [Connection Established] | | [Data Transfer Begins] |

Step 4 (Often forgotten): The 4-Way Handshake (Teardown). Connections must be closed properly (FIN packets) or you risk "Address Already In Use" errors (TIME_WAIT state).

Everything is a Socket

When you write app.listen(3000) in Node.js or Python, you are creating a Socket.

A socket is defined by: {Protocol, Local IP, Local Port, Remote IP, Remote Port}.

Socket States

Where HTTP and TLS Fit In

HTTP is an application-layer protocol that usually runs over TCP. HTTPS = HTTP + TLS encryption.

Client -> TCP 3-way handshake -> TLS handshake -> HTTP request/response

Latency, MTU, and Packet Loss

Speed issues are often caused by network fundamentals, not your code.

Developer symptom: Random timeouts and flaky tests often trace back to packet loss or MTU issues in VPNs and containers.

Decoding Network Errors

ECONNREFUSED (Connection Refused)
Result of a TCP RST packet. Usually means: Nothing is listening on that IP:Port. (Server is down or wrong port).
ETIMEDOUT (Connection Timed Out)
The establishing packet (SYN) was sent, but no reply ever came. Usually means: Firewall dropped the packet or the IP doesn't exist.
EADDRINUSE (Address Already in Use)
Another process is already bound to this port. Kill the other process.
ECONNRESET (Connection Reset by Peer)
The connection was established, but the other side abruptly closed it (crashed or panic).

Retries, Timeouts, and Backoff

Robust apps assume networks fail. Use timeouts and retries carefully.

Essential Commands

# Check if a port/socket is listening netstat -tuln OR ss -tuln # Check connectivity to a specific port telnet localhost 3000 # OR nc -zv localhost 3000 (Netcat) # See who is using a port lsof -i :3000 # Inspect HTTP headers curl -I https://google.com

Pro tip: Combine curl -v with --resolve to debug DNS vs IP behavior.