Skip to content

Overview

Networking is how computers communicate with each other. Every web page you load, every API you call, and every message you send travels through a network. Understanding networking helps you build faster applications, debug mysterious failures, design secure systems, and reason clearly about distributed architecture.

The OSI (Open Systems Interconnection) model breaks network communication into 7 abstract layers. Each layer has a specific responsibility and communicates only with the layers directly above and below it.

graph TD L7["Layer 7 - Application HTTP, FTP, SMTP, DNS"] L6["Layer 6 - Presentation Encryption, Compression, Encoding"] L5["Layer 5 - Session Session Management, Authentication"] L4["Layer 4 - Transport TCP, UDP - Ports, Reliability"] L3["Layer 3 - Network IP - Routing, Addressing"] L2["Layer 2 - Data Link MAC - Frames, Switches"] L1["Layer 1 - Physical Bits - Cables, Wi-Fi, Signals"] L7 --> L6 --> L5 --> L4 --> L3 --> L2 --> L1
  • Layer 1 - Physical - raw bits on the wire; cables, Wi-Fi signals, fibre optics
  • Layer 2 - Data Link - frames sent between devices on the same local network; MAC addresses and switches
  • Layer 3 - Network - packets routed between different networks; IP addresses and routers
  • Layer 4 - Transport - segments of data sent end-to-end; TCP and UDP; ports
  • Layer 5 - Session - managing sessions (opening, maintaining, closing connections)
  • Layer 6 - Presentation - data formatting, encryption, and compression
  • Layer 7 - Application - protocols your applications use directly: HTTP, DNS, SMTP, FTP

In practice, the TCP/IP model (4 layers) is what the internet actually uses. It collapses OSI layers 5, 6, and 7 into one Application layer.

Every device on a network has an IP address that identifies it.

  • IPv4 - 32-bit addresses written as four octets: 192.168.1.1; approximately 4 billion unique addresses
  • IPv6 - 128-bit addresses written as eight groups of hex: 2001:0db8:85a3::8a2e:0370:7334; virtually unlimited addresses
  • Public vs Private IP - private addresses (10.x.x.x, 172.16-31.x.x, 192.168.x.x) are used inside networks; public addresses are routable on the internet
  • Subnets and CIDR Notation - 192.168.1.0/24 means the first 24 bits are the network address, leaving 8 bits for host addresses (256 possible hosts)
  • NAT (Network Address Translation) - allows many devices with private IPs to share one public IP; your home router does this
  • Loopback - 127.0.0.1 (IPv4) and ::1 (IPv6) always refer to the local machine

DNS (Domain Name System) translates human-readable domain names into IP addresses.

  • Why DNS - people use names like example.com; machines use IP addresses like 93.184.216.34
  • DNS resolution process:
    1. Browser checks its local cache
    2. OS checks /etc/hosts and its own cache
    3. Query goes to the Recursive Resolver (usually provided by your ISP or a public resolver like 8.8.8.8)
    4. Resolver queries the Root Nameserver
    5. Root directs the resolver to the TLD Nameserver (e.g., .com)
    6. TLD directs the resolver to the Authoritative Nameserver for the domain
    7. Authoritative Nameserver returns the IP address
  • Record Types:
    • A - maps a domain to an IPv4 address
    • AAAA - maps a domain to an IPv6 address
    • CNAME - maps a domain to another domain (alias)
    • MX - mail exchange; where to send email for this domain
    • TXT - arbitrary text; used for verification and SPF/DKIM email authentication
    • NS - nameserver records; which DNS servers are authoritative for this domain
    • SOA - start of authority; administrative information about the zone
  • TTL (Time to Live) - how long a DNS record is cached before being re-fetched
  • DNS over HTTPS (DoH) and DNS over TLS (DoT) - encrypting DNS queries for privacy

The Transport layer is responsible for sending data reliably (or quickly) between two endpoints.

TCP is reliable, ordered, and connection-oriented.

  • Three-Way Handshake - establishing a connection:
    1. Client sends SYN
    2. Server responds SYN-ACK
    3. Client sends ACK; connection is established
  • Four-Way Teardown - closing a connection: FIN -> FIN-ACK -> FIN -> FIN-ACK
  • Reliability mechanisms:
    • Sequence numbers and acknowledgements (ACKs)
    • Retransmission of lost packets
    • Flow control (sliding window) - prevents the sender from overwhelming the receiver
    • Congestion control - prevents the sender from overwhelming the network (slow start, AIMD)
  • Use cases: HTTP/HTTPS, email, file transfers, anything where data must arrive intact

UDP is fast, connectionless, and unreliable (no guarantee of delivery or order).

  • No handshake, no acknowledgements, no retransmissions
  • Much lower overhead than TCP
  • The application handles any needed reliability
  • Use cases: DNS lookups, video streaming, online gaming, VoIP, live broadcasts
  • QUIC - a modern protocol built on UDP that adds reliability and encryption (used by HTTP/3)

HTTP is the protocol that powers the web. It is a text-based, request-response protocol.

GET /api/users?page=1 HTTP/1.1
Host: example.com
Authorization: Bearer <token>
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 123
{"users": [...]}
  • GET - retrieve a resource; safe and idempotent
  • POST - create a resource or submit data; not idempotent
  • PUT - replace a resource entirely; idempotent
  • PATCH - partially update a resource
  • DELETE - remove a resource; idempotent
  • HEAD - like GET but returns only headers, no body
  • OPTIONS - describes the communication options for the target resource (used in CORS preflight)
  • 1xx Informational: 100 Continue
  • 2xx Success: 200 OK, 201 Created, 204 No Content
  • 3xx Redirection: 301 Moved Permanently, 302 Found, 304 Not Modified
  • 4xx Client Error: 400 Bad Request, 401 Unauthorised, 403 Forbidden, 404 Not Found, 429 Too Many Requests
  • 5xx Server Error: 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable
  • HTTP/1.0 - one request per connection; very inefficient
  • HTTP/1.1 - persistent connections and pipelining; still widely used
  • HTTP/2 - multiplexing (multiple requests over one connection), header compression, server push
  • HTTP/3 - built on QUIC (UDP); eliminates head-of-line blocking; faster connection setup
  • TLS (Transport Layer Security) - encrypts the connection between client and server
  • TLS Handshake - negotiating cipher suites and exchanging certificates to establish an encrypted session
  • Certificates - issued by Certificate Authorities (CAs); prove the server’s identity
  • HSTS (HTTP Strict Transport Security) - forces browsers to always use HTTPS for a domain

Sockets are the fundamental API for network communication.

  • A socket is one endpoint of a two-way communication link; identified by IP address + port
  • TCP socket lifecycle: socket() -> bind() -> listen() -> accept() -> recv/send -> close()
  • UDP socket lifecycle: socket() -> bind() -> recvfrom/sendto -> close()
  • Ports:
    • Well-known ports (0–1023): HTTP (80), HTTPS (443), SSH (22), DNS (53), SMTP (25)
    • Registered ports (1024–49151): application-specific
    • Dynamic/ephemeral ports (49152–65535): assigned to clients automatically

WebSockets enable full-duplex, persistent communication between a client and server.

  • Problem with HTTP for real-time - HTTP is request-response; the server cannot push data to the client unprompted
  • WebSocket handshake - starts as an HTTP Upgrade request; switches to the WebSocket protocol
  • Full-duplex - both sides can send messages at any time without waiting for the other
  • Use cases: live chat, real-time notifications, collaborative editing, stock tickers, online games
  • Server-Sent Events (SSE) - a simpler alternative for server-to-client streaming over HTTP (one-directional)
  • Long Polling - a workaround before WebSockets; client holds a connection open until the server has something to send
  • Encryption: symmetric (AES) vs asymmetric (RSA, ECC); how TLS combines both
  • Hashing: SHA-256, bcrypt; password storage and data integrity
  • Common Attacks:
    • DDoS (Distributed Denial of Service) - flooding a server with traffic to make it unavailable
    • Man-in-the-Middle (MITM) - intercepting communication between two parties
    • SQL Injection - injecting malicious SQL through user input
    • XSS (Cross-Site Scripting) - injecting malicious scripts into web pages
    • CSRF (Cross-Site Request Forgery) - tricking a user’s browser into making unintended requests
    • DNS Spoofing / Cache Poisoning - sending malicious DNS responses to redirect traffic
  • Firewalls - filtering traffic based on rules (IP, port, protocol)
  • VPN (Virtual Private Network) - encrypting all traffic and routing it through a private server
  • Zero Trust Architecture - never trust any connection by default; verify every request explicitly

Load balancers distribute incoming requests across multiple servers to improve availability and performance.

  • Why load balancing - no single server can handle unlimited traffic; spreading load enables horizontal scaling
  • Layer 4 Load Balancing - routes based on IP and TCP/UDP; fast but no application awareness
  • Layer 7 Load Balancing - routes based on HTTP content (URL, headers, cookies); smarter but more overhead
  • Algorithms:
    • Round Robin - requests distributed in order to each server
    • Least Connections - send to the server with the fewest active connections
    • IP Hash - the same client IP always goes to the same server (session affinity)
    • Weighted Round Robin - servers with more capacity receive more requests
  • Health checks - the load balancer periodically checks each server and removes unhealthy ones
  • Examples: NGINX, HAProxy, AWS ELB/ALB, Cloudflare

A Content Delivery Network (CDN) caches content on servers distributed around the world, reducing latency for end users.

  • How CDNs work - when a user requests content, they are served from the nearest CDN edge node (PoP) rather than the origin server
  • What CDNs cache: static assets (images, JS, CSS), videos, HTML pages
  • Cache-Control headers - the origin server controls how long and how CDN nodes cache a response
  • Cache invalidation - purging or updating cached content when the origin changes
  • CDN benefits: lower latency, reduced load on origin, protection against DDoS, better availability
  • Examples: Cloudflare, AWS CloudFront, Akamai, Fastly
  • Switch - connects devices within the same local network (LAN); uses MAC addresses to forward frames (Layer 2)
  • Router - connects different networks; uses IP addresses to forward packets (Layer 3)
  • Routing Tables - a list of known networks and the next hop to reach them
  • Routing Protocols: OSPF, BGP (the protocol that connects autonomous systems on the internet)
  • ARP (Address Resolution Protocol) - maps an IP address to a MAC address on a local network

Networking underpins everything in modern software. Once you understand how packets travel from one machine to another, why TCP is reliable while UDP is fast, and how HTTP is structured, you will be much better equipped to build and debug distributed systems.