Backend Communication Fundamentals: Internet Protocol (IP) – Day 3

 

Backend Communication Fundamentals: Internet Protocol (IP) – Day 3

Introduction to IP: The “Vehicle” of the Internet

The Internet Protocol (IP) is the backbone of data transmission. Every request from a client to a server (or vice versa) is packaged into an IP packet, regardless of the protocol (HTTP, gRPC, DNS, etc.). These packets contain:

  • Source IP address (where the data comes from).
  • Destination IP address (where the data goes).
  • Payload (the actual data, like a JSON request or SQL query).

Key Idea:
IP operates at Layer 3 (Network Layer). Routers use IP addresses to forward packets, but they don’t care about ports, encryption, or the data inside—only the destination.


IP Addresses and Subnets

1. IP Address Structure

  • An IPv4 address is 4 bytes (e.g., 192.168.1.100).
  • Divided into network and host portions using a subnet mask (e.g., /24 in 192.168.1.0/24 means the first 24 bits define the network, the last 8 bits define hosts).

Example:
For 192.168.1.0/24:

  • Network: 192.168.1.0 (supports 256 hosts: 192.168.1.1 to 192.168.1.255).
  • Subnet mask: 255.255.255.0.

2. Why Subnets Matter

  • Same subnet: Devices communicate directly using MAC addresses (Layer 2).
  • Different subnet: Packets are sent to a gateway (router) for routing.

Practical Tip:
Place your database and backend in the same subnet to avoid router congestion. Switches handle intra-subnet traffic faster than routers!


IP Packet Anatomy

An IP packet has two parts:

  1. Header (20–60 bytes): Contains metadata like source/destination IP, Time-to-Live (TTL), and protocol type.
  2. Data (up to 65KB): The actual payload (e.g., HTTP request).

Critical Header Fields

  • TTL: Prevents infinite loops. Each router decrements TTL by 1. If TTL hits 0, the packet is dropped, and an ICMP message is sent back.
  • Protocol: Indicates the payload type (e.g., 6 = TCP, 17 = UDP).

Fragmentation and MTU

  • MTU (Maximum Transmission Unit): Maximum packet size a network can handle (usually 1500 bytes).
  • Fragmentation: If a packet exceeds MTU, it’s split into smaller fragments.
    • Set the Don’t Fragment (DF) flag to avoid this (useful for debugging).

Example:

# Ping with DF flag to test MTU
ping -M do -s 1472 google.com  # 1472 bytes + 28-byte header = 1500 MTU

If the packet is too large, you’ll get:
Frag needed and DF set (mtu 1500).


ICMP: The Internet’s Diagnostic Tool

The Internet Control Message Protocol (ICMP) operates at Layer 3. Key uses:

  • Ping: Checks if a host is reachable.
  • Traceroute: Maps the path packets take to a destination.
  • Error Reporting: e.g., Host Unreachable or TTL Expired.

Example:

# Ping a server
ping google.com

# Trace the route to a host
traceroute google.com

Note: Firewalls often block ICMP, causing “silent failures.” Always enable ICMP for diagnostics!


Practical Takeaways

  1. IP Addresses: Ensure your backend and database share a subnet to reduce latency.
  2. TTL: Use traceroute to debug routing issues.
  3. Fragmentation: Avoid it by setting MTU correctly (default 1500 bytes).
  4. ICMP: Unblock it in firewalls for reliable network diagnostics.

Key Commands

Command Purpose
ping <IP> Check host reachability
traceroute <IP> Trace packet path
netstat -rn View routing table
ifconfig / ip addr Check your IP and subnet

Next Up: Dive into TCP/UDP and how they build on IP for reliable communication! 🚀