๐ŸŒ

Networking & Protocols

๐Ÿ‘จโ€๐Ÿณ Chef

How a message travels through the internet

Imagine sending a letter. You don't throw it in the air hoping it arrives. You put it in an envelope with an address, take it to the post office, it goes through distribution centers, and finally arrives.

The internet works the same, but in milliseconds.


The simplified OSI model

7 layers is too much. Think of 4:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  APPLICATION (HTTP, DNS, SMTP)      โ”‚ โ† Your code lives here
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  TRANSPORT   (TCP, UDP)             โ”‚ โ† Reliable or fast delivery
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  NETWORK     (IP, ICMP)             โ”‚ โ† Addresses and routes
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  LINK        (Ethernet, WiFi)       โ”‚ โ† Cables and waves
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

TCP vs UDP

TCP: The reliable postman

Client                              Server
   โ”‚                                 โ”‚
   โ”‚โ”€โ”€โ”€โ”€ SYN โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’โ”‚
   โ”‚โ†โ”€โ”€โ”€ SYN-ACK โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚
   โ”‚โ”€โ”€โ”€โ”€ ACK โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’โ”‚
   โ”‚                                 โ”‚
   โ”‚  (Connection established)       โ”‚
   โ”‚                                 โ”‚
   โ”‚โ”€โ”€โ”€โ”€ Data โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’โ”‚
   โ”‚โ†โ”€โ”€โ”€ ACK โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚
  • Guarantees delivery: Retries if lost
  • Ordered: Arrives in correct order
  • Slower: Due to handshake and confirmations

Use TCP for: HTTP, Email, files, anything that can't lose data.

UDP: The fast messenger

Client                              Server
   โ”‚                                 โ”‚
   โ”‚โ”€โ”€โ”€โ”€ Data โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’โ”‚
   โ”‚โ”€โ”€โ”€โ”€ Data โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’โ”‚
   โ”‚โ”€โ”€โ”€โ”€ Data โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’โ”‚
   โ”‚                                 โ”‚
   โ”‚  (No confirmation)              โ”‚
  • No guarantees: Can lose packets
  • Unordered: Arrive as they can
  • Fast: No connection overhead

Use UDP for: Video streaming, games, DNS, VoIP.


HTTP deep dive

Methods and their semantics

MethodUseIdempotentBody
GETGet dataYesNo
POSTCreate resourceNoYes
PUTReplace completeYesYes
PATCHModify partialNoYes
DELETEDeleteYesNo

Status codes you should know

2xx Success
โ”œโ”€โ”€ 200 OK - All good
โ”œโ”€โ”€ 201 Created - Resource created
โ””โ”€โ”€ 204 No Content - Success without body

3xx Redirection
โ”œโ”€โ”€ 301 Moved Permanently - URL changed forever
โ”œโ”€โ”€ 302 Found - Temporary redirect
โ””โ”€โ”€ 304 Not Modified - Use your cache

4xx Client Error
โ”œโ”€โ”€ 400 Bad Request - Malformed request
โ”œโ”€โ”€ 401 Unauthorized - Needs authentication
โ”œโ”€โ”€ 403 Forbidden - Authenticated but no permission
โ”œโ”€โ”€ 404 Not Found - Resource doesn't exist
โ””โ”€โ”€ 429 Too Many Requests - Rate limited

5xx Server Error
โ”œโ”€โ”€ 500 Internal Server Error - Bug in server
โ”œโ”€โ”€ 502 Bad Gateway - Proxy couldn't connect
โ”œโ”€โ”€ 503 Service Unavailable - Server overloaded
โ””โ”€โ”€ 504 Gateway Timeout - Proxy timeout

HTTPS and TLS

The TLS handshake

Client                              Server
   โ”‚                                 โ”‚
   โ”‚โ”€โ”€โ”€โ”€ ClientHello โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’โ”‚ (TLS versions, ciphers)
   โ”‚โ†โ”€โ”€โ”€ ServerHello โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚ (certificate, chosen cipher)
   โ”‚                                 โ”‚
   โ”‚     (Verify certificate)        โ”‚
   โ”‚                                 โ”‚
   โ”‚โ”€โ”€โ”€โ”€ Key Exchange โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’โ”‚ (generate session key)
   โ”‚โ†โ”€โ”€โ”€ Finished โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚
   โ”‚                                 โ”‚
   โ”‚  === Encrypted connection ===   โ”‚

Verify a certificate

# See site certificate
openssl s_client -connect google.com:443 -servername google.com 2>/dev/null | openssl x509 -text -noout

# See expiration dates
echo | openssl s_client -connect luxia.us:443 2>/dev/null | openssl x509 -dates -noout

DNS: The phone book

Your browser asks for: luxia.us

1. Local cache (your machine)
2. Router cache
3. ISP DNS resolver
4. Root DNS (.us)
5. TLD DNS (luxia.us)
6. Authoritative DNS โ†’ IP: 123.45.67.89

Useful commands

# Resolve domain
nslookup luxia.us

# More detail
dig luxia.us

# See all records
dig luxia.us ANY

# Trace resolution
dig +trace luxia.us

WebSockets: Bidirectional communication

HTTP is request-response. WebSocket is an open channel.

Traditional HTTP:
Client โ”€โ”€requestโ”€โ”€โ†’ Server
Client โ†โ”€responseโ”€ Server
Client โ”€โ”€requestโ”€โ”€โ†’ Server
Client โ†โ”€responseโ”€ Server

WebSocket:
Client โ†โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ Server
         Open channel, both
         can send whenever they want

When to use WebSocket

  • Real-time chat
  • Push notifications
  • Multiplayer games
  • Live collaboration (Google Docs)
  • Price updates (trading)

CORS: Why it exists

Browsers block requests from one origin to another for security.

https://my-app.com       https://api.other.com
       โ”‚                         โ”‚
       โ”‚โ”€โ”€โ”€โ”€ fetch() โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’โ”‚
       โ”‚                         โ”‚
       โ”‚  BLOCKED by CORS        โ”‚
       โ”‚  (unless api.other      โ”‚
       โ”‚   allows it)            โ”‚

CORS Headers

// In your Express server
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', 'https://my-app.com');
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
  next();
});

Debugging tools

curl: HTTP Swiss army knife

# Basic GET
curl https://api.example.com/users

# With headers
curl -H "Authorization: Bearer TOKEN" https://api.example.com/me

# POST with JSON
curl -X POST -H "Content-Type: application/json" \
  -d '{"name":"John"}' https://api.example.com/users

# See response headers
curl -I https://example.com

# See full exchange
curl -v https://example.com

# Measure times
curl -w "@curl-format.txt" -o /dev/null -s https://example.com

ping and traceroute

# Check connectivity
ping google.com

# See packet route
traceroute google.com  # Linux/Mac
tracert google.com     # Windows

netstat/ss: Active connections

# See listening ports
ss -tlnp   # Linux
netstat -an | grep LISTEN  # Mac

# See established connections
ss -tn

Common ports

PortServiceNotes
22SSHSecure remote access
80HTTPUnencrypted web
443HTTPSEncrypted web
3000Dev serversNode, Rails, etc.
5432PostgreSQLDatabase
6379RedisCache
27017MongoDBDatabase

CDN: Content close to the user

Without CDN:
User (Mexico) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ Server (USA)
                   200ms

With CDN:
User (Mexico) โ”€โ”€โ†’ Edge (Mexico)
                   20ms

Static content is replicated to edges
around the world.

Resources


Practice

-> Network Debugging - Diagnose real network problems