How the Internet Works

πŸ“‹ Quick Reference

LayerProtocolPurposeExample
ApplicationHTTP, HTTPS, DNS, SMTPUser-facing protocolsWeb browsing, email
TransportTCP, UDPReliable/unreliable deliveryTCP for web, UDP for video
NetworkIPRouting packetsIPv4, IPv6
Data LinkEthernet, WiFiLocal network deliveryLAN, WLAN
PhysicalElectrical signalsActual transmissionCables, radio waves

TL;DR: Internet uses layered architecture (OSI/TCP-IP model). Data travels in packets through routers, using IP for addressing and TCP/UDP for transport. DNS translates domain names to IP addresses.


Clear Definition

The Internet is a global network of interconnected computers that communicate using standardized protocols. It enables data exchange between devices worldwide through packet-switched networking, where data is broken into packets, routed through multiple intermediate devices (routers), and reassembled at the destination.

Think of it like the postal system: You write a letter (data), put it in an envelope with an address (IP address), the postal service routes it through sorting facilities (routers), and it arrives at the destination where it's opened and read (application receives data).

πŸ’‘ Key Insight: The Internet is decentralizedβ€”no single entity controls it. It's built on open standards (TCP/IP) that allow any device to connect and communicate, making it resilient and scalable.


Core Concepts

TCP/IP Model (Internet Protocol Suite)

The Internet uses a layered architecture:

1. Application Layer (Layer 7)

  • Protocols: HTTP, HTTPS, DNS, SMTP, FTP, SSH
  • Purpose: User-facing applications
  • Example: Web browser requesting a webpage

2. Transport Layer (Layer 4)

  • Protocols: TCP, UDP
  • Purpose: End-to-end communication, reliability
  • TCP: Reliable, connection-oriented (web, email)
  • UDP: Unreliable, connectionless (video streaming, DNS)

3. Network Layer (Layer 3)

  • Protocol: IP (Internet Protocol)
  • Purpose: Routing packets across networks
  • IP Address: Unique identifier (e.g., 192.168.1.1)
  • Routers: Forward packets based on IP addresses

4. Data Link Layer (Layer 2)

  • Protocols: Ethernet, WiFi (802.11)
  • Purpose: Local network delivery
  • MAC Address: Hardware identifier
  • Switches: Forward frames within local network

5. Physical Layer (Layer 1)

  • Media: Cables, radio waves, fiber optics
  • Purpose: Actual signal transmission
  • Example: Ethernet cable, WiFi radio

How Data Travels: Packet Switching

Step-by-Step Process:

  1. Application Creates Data

    • User types URL in browser
    • Browser creates HTTP request
  2. Transport Layer Adds TCP Header

    • Breaks data into segments
    • Adds source/destination ports
    • Sequence numbers for ordering
  3. Network Layer Adds IP Header

    • Adds source/destination IP addresses
    • Creates IP packets
    • Determines next hop (routing)
  4. Data Link Layer Adds Frame Header

    • Adds MAC addresses
    • Creates Ethernet frames
    • Sends to next hop (router or destination)
  5. Physical Transmission

    • Electrical signals over cable or radio waves
    • Transmitted to next device
  6. Routing Through Internet

    • Each router examines destination IP
    • Looks up routing table
    • Forwards to next router (hop)
    • Multiple hops until destination
  7. Reassembly at Destination

    • Reverse process: Remove headers
    • Reassemble segments into original data
    • Deliver to application

Packet Structure:

[Application Data]
    +
[TCP Header] (ports, sequence numbers)
    +
[IP Header] (source/dest IP addresses)
    +
[Ethernet Header] (MAC addresses)
    +
[Physical Signal]

DNS (Domain Name System)

What is DNS?

  • Translates human-readable domain names to IP addresses
  • Example: google.com β†’ 142.250.80.14

How DNS Works:

  1. User Types URL: www.example.com
  2. Browser Queries DNS: "What's the IP for www.example.com?"
  3. DNS Resolution Process:
    • Check browser cache
    • Check OS cache
    • Query local DNS resolver (ISP)
    • Resolver queries root DNS servers
    • Root directs to .com TLD servers
    • TLD directs to example.com authoritative server
    • Authoritative server returns IP address
  4. Browser Connects: Uses IP to connect to server

DNS Record Types:

  • A: IPv4 address
  • AAAA: IPv6 address
  • CNAME: Alias to another domain
  • MX: Mail server
  • NS: Name server

IP Addressing

IPv4 (32-bit addresses)

  • Format: 192.168.1.1 (4 octets, 0-255 each)
  • Total addresses: ~4.3 billion
  • Running out β†’ IPv6 adoption

IPv6 (128-bit addresses)

  • Format: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
  • Total addresses: 3.4 Γ— 10³⁸ (essentially unlimited)
  • Gradually replacing IPv4

Private vs Public IPs:

  • Private: Used within local networks (192.168.x.x, 10.x.x.x)
  • Public: Routable on Internet (assigned by ISP)
  • NAT: Network Address Translation (maps private to public)

Routing

How Routers Work:

  1. Receive packet
  2. Examine destination IP address
  3. Look up routing table
  4. Determine next hop (next router)
  5. Forward packet to next hop
  6. Repeat until destination reached

Routing Algorithms:

  • Distance Vector: Routers share distance to destinations (RIP)
  • Link State: Routers share network topology (OSPF)
  • BGP: Border Gateway Protocol (routing between ISPs)

Use Cases

Real-World Examples

  1. Web Browsing

    • User types URL β†’ DNS lookup β†’ TCP connection β†’ HTTP request β†’ Response β†’ Display webpage
    • Example: Visiting google.com
  2. Email

    • SMTP for sending, IMAP/POP3 for receiving
    • Emails travel through multiple servers
    • Example: Gmail to Outlook email delivery
  3. Video Streaming

    • UDP or TCP for transport
    • CDN for content delivery
    • Example: Netflix streaming to your device
  4. Online Gaming

    • UDP for low latency
    • Game servers handle game state
    • Example: Multiplayer games (Fortnite, League of Legends)
  5. File Transfer

    • FTP or HTTP for file downloads
    • Large files broken into packets
    • Example: Downloading software updates

Advantages & Disadvantages

Internet Advantages

βœ… Global Connectivity: Connect devices worldwide

  • Enables global communication
  • Example: Video call from US to Japan

βœ… Decentralized: No single point of failure

  • Resilient to failures
  • Example: If one router fails, traffic routes around it

βœ… Standardized Protocols: Open standards

  • Any device can connect
  • Interoperability

βœ… Scalable: Handles billions of devices

  • IPv6 provides unlimited addresses
  • Example: IoT devices connecting to Internet

βœ… Flexible: Supports various applications

  • Web, email, video, gaming, etc.
  • New protocols can be added

Internet Disadvantages

❌ Security Concerns: Vulnerable to attacks

  • DDoS attacks, malware, phishing
  • Requires security measures (HTTPS, firewalls)

❌ Latency: Physical distance causes delay

  • Speed of light limits (round-trip time)
  • Example: US to Asia = ~150-200ms minimum

❌ Reliability: No guarantees

  • Best-effort delivery
  • Packets can be lost, delayed, reordered

❌ Complexity: Many layers and protocols

  • Difficult to troubleshoot
  • Requires expertise

❌ Privacy Concerns: Data travels through multiple hops

  • Can be intercepted
  • Requires encryption (HTTPS, VPN)

Best Practices

  1. Use HTTPS: Encrypt web traffic

    • Prevents man-in-the-middle attacks
    • Example: TLS/SSL certificates
  2. Implement Proper DNS: Use reliable DNS servers

    • Google DNS (8.8.8.8) or Cloudflare (1.1.1.1)
    • Faster resolution, better security
  3. Monitor Network Performance: Track latency, packet loss

    • Use tools: ping, traceroute, mtr
    • Identify bottlenecks
  4. Implement CDN: Content Delivery Networks

    • Reduce latency by caching content closer to users
    • Example: Cloudflare, AWS CloudFront
  5. Use Appropriate Protocols: Choose TCP vs UDP wisely

    • TCP for reliability (web, email)
    • UDP for low latency (gaming, video)
  6. Plan for IPv6: Future-proof your systems

    • Support both IPv4 and IPv6
    • Gradual migration

Common Pitfalls

⚠️ Common Mistake: Not understanding DNS caching

  • Problem: DNS changes take time to propagate
  • Solution: Understand TTL (Time To Live), use low TTL for changes

⚠️ Common Mistake: Assuming reliable delivery

  • Problem: Internet is best-effort, packets can be lost
  • Solution: Use TCP for reliability, implement retries

⚠️ Common Mistake: Ignoring latency

  • Problem: Physical distance causes delay
  • Solution: Use CDN, optimize for geographic distribution

⚠️ Common Mistake: Not securing connections

  • Problem: Data can be intercepted
  • Solution: Always use HTTPS, encrypt sensitive data

Interview Tips

🎯 Interview Focus: Understanding fundamentals and being able to explain simply

Common Questions:

  1. "What happens when you type a URL in a browser?"

    • Answer: DNS lookup β†’ TCP connection β†’ HTTP request β†’ Response β†’ Render. Explain each step in detail.
  2. "How does DNS work?"

    • Answer: Hierarchical system. Browser β†’ local resolver β†’ root β†’ TLD β†’ authoritative server β†’ IP address. Explain caching.
  3. "What's the difference between TCP and UDP?"

    • Answer: TCP is reliable, connection-oriented, ordered. UDP is unreliable, connectionless, faster. Use cases for each.
  4. "How do packets travel across the Internet?"

    • Answer: Packet switching. Source β†’ router β†’ router β†’ ... β†’ destination. Each router forwards based on IP address.
  5. "What is a CDN and why is it used?"

    • Answer: Content Delivery Network. Caches content at edge locations closer to users. Reduces latency, offloads origin server.

Red Flags to Avoid:

  • Oversimplifying (saying "magic happens")
  • Not understanding layers
  • Confusing protocols
  • Not mentioning DNS

  • TCP vs UDP (Step 5): Transport layer protocols
  • HTTP (Step 5): Application layer protocol
  • Load Balancing (Step 6): Distributes traffic across servers
  • CDN (Step 4): Content delivery optimization
  • WebSockets (Step 5): Persistent connections over Internet

Visual Aids

Internet Architecture

Your Computer          Internet          Server
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Browser    β”‚                        β”‚  Web Server β”‚
β”‚  (App)      β”‚                        β”‚  (App)      β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜                        β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚                                      β”‚
β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”                        β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
β”‚    TCP      β”‚                        β”‚    TCP      β”‚
β”‚  (Transport)β”‚                        β”‚  (Transport)β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜                        β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚                                      β”‚
β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
β”‚     IP      │─────▢│ Routers  │───▢│     IP      β”‚
β”‚  (Network)  β”‚      β”‚          β”‚    β”‚  (Network)  β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚                                      β”‚
β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”                        β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
β”‚  Ethernet   β”‚                        β”‚  Ethernet   β”‚
β”‚  (Data Link)β”‚                        β”‚  (Data Link)β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

DNS Resolution Flow

Browser: "What's IP for www.example.com?"
    β”‚
    β–Ό
Browser Cache? β†’ No
    β”‚
    β–Ό
OS Cache? β†’ No
    β”‚
    β–Ό
Local DNS Resolver (ISP)
    β”‚
    β–Ό
Root DNS Server: "Ask .com TLD"
    β”‚
    β–Ό
.com TLD Server: "Ask example.com NS"
    β”‚
    β–Ό
example.com Authoritative Server: "IP is 192.0.2.1"
    β”‚
    β–Ό
Browser: Connect to 192.0.2.1

Packet Routing

Source (192.168.1.100)
    β”‚
    β–Ό Packet: Dest=8.8.8.8
Router 1 (192.168.1.1)
    β”‚
    β–Ό Forward to Router 2
Router 2 (ISP Gateway)
    β”‚
    β–Ό Forward to Router 3
Router 3 (Internet Backbone)
    β”‚
    β–Ό Forward to Router 4
Router 4 (Google Network)
    β”‚
    β–Ό
Destination (8.8.8.8)

Back to: Step 1 Index | Main Index