bigonotes
System DesignNetworking

IP Addressing and Routing

How IP addressing, CIDR subnets, NAT, IPv6, ICMP and routing move packets between networks, and why it shapes where servers go.

9 min readLast updated

The network layer (layer 3) gets a packet from one machine to another, even when they sit on different networks on opposite sides of the world. Its protocol is IP, the Internet Protocol. You rarely write code at this layer, but it decides which of your machines the internet can reach, which is a security decision you make in every design.

IP: addressing and routing#

IP gives every machine an address and moves packets toward it.

  • Every device has an IP address, such as 203.0.113.7 (IPv4) or 2001:db8::1 (IPv6).
  • Data is split into packets, each carrying a source and destination address.
  • Routers live at this layer. Each one reads a packet's destination address and forwards it to the next router that is closer to the destination. No single router knows the whole path.
  • IP is best effort: packets can be lost, duplicated or arrive out of order. Fixing that is the transport layer's job (TCP). See TCP and UDP.
  • You might expect those fixes to make TCP slow. They don't, because TCP uses a sliding window: the sender keeps many unacknowledged segments in flight at once, rather than waiting for each acknowledgement before sending the next (stop-and-wait).

IPv4 addresses#

An IPv4 address is 32 bits, written as four 8-bit numbers (octets) separated by dots. Each octet runs from 0 to 255, so 172.16.254.1 is one address. In binary that address is 10101100.00010000.11111110.00000001.

32 bits give 2322^{32}, about 4.3 billion, addresses. That is fewer than the devices online today, and the free pool has run out. Three things keep IPv4 working: NAT, CIDR and the move to IPv6. Each one has a section below.

The old address classes#

Early IPv4 split the space into fixed classes. You still see the names, but routing stopped using them in 1993 when CIDR replaced them.

Class First octet Default prefix Usable hosts per network Use
A 0–127 /8 16,777,214 Very large networks
B 128–191 /16 65,534 Medium networks
C 192–223 /24 254 Small networks
D 224–239 None None Multicast
E 240–255 None None Reserved, experimental

Classes wasted space. A company that needed 300 addresses got a class B with 65,534 of them, because a class C held only 254.

CIDR and subnet math#

CIDR (classless inter-domain routing) is what replaced the classes. It lets a network have any prefix length, so a company that needs 300 addresses can get a block close to that size. You write it as an address plus a slash and the number of network bits: 192.168.1.0/24.

  • Network bits (the prefix) identify the network. All devices on it share them, like a street name.
  • Host bits (the rest) identify one device on that network, like a house number.

For a prefix of length nn, the block holds 232−n2^{32-n} addresses. Two of them are reserved: the first is the network address and the last is the broadcast address. So the usable host count is 232−n−22^{32-n} - 2. Here are the block sizes you'll see most:

CIDR Host bits Addresses Usable hosts Example range
/16 16 65,536 65,534 10.1.0.0 – 10.1.255.255
/24 8 256 254 192.168.1.0 – 192.168.1.255
/26 6 64 62 192.168.1.0 – 192.168.1.63
/28 4 16 14 192.168.1.16 – 192.168.1.31
/32 0 1 One host A single address

To see the formula in action, take 192.168.1.10/24:

  1. The first 24 bits, 192.168.1, are the network. The network address is 192.168.1.0.
  2. The last 8 bits, 10, pick the host.
  3. The broadcast address is 192.168.1.255.
  4. Usable hosts are 192.168.1.1 to 192.168.1.254: 28−2=2542^8 - 2 = 254.

Python's standard library does this math for you:

import ipaddress

net = ipaddress.ip_network("192.168.1.0/24")
print(net.num_addresses)           # 256
print(net.network_address)         # 192.168.1.0
print(net.broadcast_address)       # 192.168.1.255
print(len(list(net.hosts())))      # 254 usable hosts
print(list(net.subnets(new_prefix=26)))  # four /26 blocks

Cloud providers reserve extra addresses in each subnet. AWS reserves five per subnet: the first four and the last one. A /24 subnet in a VPC gives you 251 usable addresses, not 254.

CIDR also lets routers aggregate routes. One entry for 10.0.0.0/8 covers every smaller block inside it, which keeps internet routing tables small.

Public vs private IPs#

CIDR tells you how big a block is. Whether the addresses in it are public or private decides who can reach a machine.

Public IP Private IP
Reachable from Anywhere on the internet Only inside its own network
Assigned by Regional internet registries, through ISPs and cloud providers You or your cloud network
Ranges Everything not reserved 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
Analogy Your street address, which the postal service can find A room number that only means something inside your building

Two laptops in different homes can both be 192.168.1.5 without conflict, because private addresses only mean something within their own network. When a private machine talks to the internet, a router performs NAT (network address translation), swapping in a public address on the way out.

How NAT tracks connections#

The trick is in how the router keeps track of who's talking. Most home and office routers do port address translation: many private devices share one public IP, and the router tells their connections apart by port number.

  1. A laptop at 192.168.1.5 opens a connection from its port 52000 to a website.
  2. The router rewrites the source to its public IP, 203.0.113.7, and picks a free public port, say 1050.
  3. It records the mapping in its NAT table.
  4. The website replies to 203.0.113.7:1050.
  5. The router looks up port 1050, rewrites the destination back to 192.168.1.5:52000, and forwards the reply.

With a second laptop on the same network, the NAT table looks like this:

Private source Public source (after NAT) Destination
192.168.1.5:52000 203.0.113.7:1050 198.51.100.20:443
192.168.1.10:52000 203.0.113.7:1060 198.51.100.20:443

Both devices used the same private port, but the router gave them different public ports, so replies reach the right device. A side effect: an outside machine cannot start a connection to a device behind NAT, because the router has no mapping for it. In the cloud, a NAT gateway does the same job for private subnets that need outbound internet access.

In system design: this split is how you protect your backend. Give the load balancer a public IP so users can reach it. Keep app servers and databases on private IPs inside a private network (a VPC in the cloud). The internet can't even attempt to connect to your database directly.

Only the load balancer has a public IP; app servers and the database sit on private IPs inside the private network

Special address ranges#

Private ranges are one kind of reserved block, but there are a few more. They never appear as normal public addresses, and you'll meet them often when you debug networks.

Range Purpose Notes
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 Private networks Not routed on the public internet
127.0.0.0/8 Loopback 127.0.0.1 is localhost; traffic never leaves the machine
169.254.0.0/16 Link-local The OS picks one when DHCP fails. Also used for cloud instance metadata (169.254.169.254)
224.0.0.0/4 Multicast One sender, many subscribed receivers
255.255.255.255 Local broadcast Every host on the local segment
240.0.0.0/4 Reserved Former class E; not assigned

A 169.254.x.x address on your laptop usually means it could not reach a DHCP server. Multicast sends one stream to every receiver that has joined a group, which saves bandwidth for things like IPTV inside a network. It is widely used on local networks but rarely across the public internet.

IPv6 basics#

NAT and CIDR stretch the IPv4 space, but IPv6 is the long-term answer to the address shortage. Its addresses are 128 bits, which gives 21282^{128} addresses: enough to give every device a globally unique address.

  • An address is eight groups of four hex digits, separated by colons: 2001:0db8:0000:0042:0000:8a2e:0370:7334.
  • You may drop leading zeros in a group: 2001:db8:0:42:0:8a2e:370:7334.
  • You may replace one run of all-zero groups with ::: 2001:db8::1 is short for 2001:db8:0:0:0:0:0:1.
  • The loopback address is ::1.
  • A typical subnet is a /64, which leaves 64 bits for hosts.

Because addresses are plentiful, IPv6 networks usually do not need NAT. Devices still sit behind firewalls, so "has a public address" does not mean "open to the internet". Most networks today run IPv4 and IPv6 side by side (dual stack).

ICMP and ARP#

Two helper protocols work alongside IP. You rarely use them directly, but your tools depend on them.

Protocol Job Where you see it
ICMP Carries error and diagnostic messages, such as "destination unreachable" and "time exceeded" ping sends ICMP echo requests. traceroute uses "time exceeded" replies to find each hop.
ARP On a local network, finds the hardware (MAC) address for an IPv4 address Your machine asks "who has 192.168.1.1?" before it can send a frame to the router

IPv6 replaces ARP with the Neighbor Discovery protocol, which runs over ICMPv6. Many firewalls block ICMP, so a host that ignores ping may still be up and serving traffic.

Routing and why location matters#

Routing ties the address blocks from earlier back to where your servers sit. Internet routers know which blocks of addresses belong to which networks. For example, Apple has historically owned the whole 17.0.0.0/8 block. Routing works on these blocks rather than individual addresses, which keeps routing tables manageable.

This is the basis for techniques that send a user to the nearest server:

  • Anycast: the same IP address is announced from many locations, and the network delivers each user's packets to the closest one. Large DNS providers and CDNs use this.
  • Geo-aware DNS: the DNS server returns a different IP depending on where the user is.

Both shorten the physical distance a request travels, which is the biggest single factor in network latency. See Multi-Region Latency and Failures.

Key takeaways#

  • IP moves packets between networks on a best-effort basis. Routers forward them hop by hop.
  • Public IPs are reachable from the internet. Private IPs only work inside their own network, and NAT bridges the two.
  • Expose only the load balancer publicly. Keep app servers and databases on private IPs.
  • Anycast and geo-aware DNS use routing to send users to a nearby server.
  • A /n block holds 232−n2^{32-n} addresses, minus 2 for network and broadcast: a /24 has 254 usable hosts.
  • NAT lets many private devices share one public IP by mapping each connection to a different public port.
  • Know the special ranges: private, loopback 127.0.0.0/8, link-local 169.254.0.0/16 and multicast 224.0.0.0/4.
  • IPv6 uses 128-bit addresses written in hex, shortened with ::, and usually needs no NAT.
Revision notes · 2 min

Core idea#

The network layer (layer 3) moves a packet from one machine to another across networks. Its protocol is IP. It decides which of your machines the internet can reach.

Key points#

  • Every device has an IP address, IPv4 or IPv6.
  • Data travels in packets. Each packet carries a source and a destination address.
  • Routers forward each packet hop by hop. No single router knows the whole path.
  • IP is best effort. Packets can be lost, duplicated or arrive out of order. TCP fixes this with a sliding window, not stop-and-wait.
  • Routing works on address blocks, not single addresses. This keeps routing tables manageable.

Addressing#

  • IPv4 is 32 bits in four octets: about 4.3 billion addresses, now exhausted.
  • The old classes (A /8, B /16, C /24) wasted space. CIDR replaced them.
  • A /n block holds 232−n2^{32-n} addresses. Subtract 2 for network and broadcast. A /24 has 254 usable hosts.
  • AWS reserves five addresses per subnet.
  • IPv6 is 128 bits in hex. Drop leading zeros and replace one zero run with ::. It usually needs no NAT.

Public vs private IPs#

Public IP Private IP
Reachable from Anywhere on the internet Only inside its own network
Assigned by Registries, through ISPs and cloud providers You or your cloud network
Ranges Everything not reserved 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
  • NAT swaps a private address for a public one. It maps each connection to a different public port, so many devices share one IP.
  • Special ranges: loopback 127.0.0.0/8, link-local 169.254.0.0/16 (DHCP failed), multicast 224.0.0.0/4.
  • ICMP carries errors and powers ping and traceroute. ARP finds the MAC address for an IPv4 address.
  • Give the load balancer a public IP.
  • Keep app servers and databases on private IPs inside a VPC.

Sending users to the nearest server#

  • Anycast: many locations announce the same IP. The network delivers each user to the closest one.
  • Geo-aware DNS: the DNS server returns a different IP based on user location.
  • Physical distance is the biggest single factor in network latency.

Pitfalls#

  • A public IP on a database lets the internet attempt connections to it.
  • IP alone does not guarantee delivery or order.

Say it in an interview#

  • "Only the load balancer is public. App servers and the database stay on private IPs."
  • "Anycast or geo-aware DNS routes users to the nearest region."

Found a mistake? Email ask@bigonotes.com