What it is
An IP address identifies a network interface so that packets can be delivered to it. Every IP packet carries a source and a destination address.
- IPv4 addresses are 32 bits, written as four decimal octets:
203.0.113.10. There are about 4.3 billion of them, and the regional internet registries handed out their last free blocks during the 2010s. - IPv6 addresses are 128 bits, written as eight groups of four hexadecimal digits with runs of zeros shortened to
:::2001:db8:1:1::20. There are enough to give every device many globally unique addresses.
A port is a 16-bit number in the TCP or UDP header that selects which program on a machine receives the data. Address and port together form an endpoint; two endpoints plus the protocol (the 5-tuple) identify a connection.
Why it exists
Routers need a destination they can act on without knowing every machine in the world. IP addresses are hierarchical: a prefix identifies a network, the rest an interface inside it. That lets a router hold a table of prefixes instead of billions of individual addresses. Ports exist because one machine runs many programs and many connections at once.
NAT exists because IPv4 ran out. Instead of a public address per device, a household, an office or a whole mobile carrier shares a few public addresses. IPv6 exists to remove that scarcity.
How it works
Prefixes (CIDR). 203.0.113.0/24 means the first 24 bits are the network part, leaving 8 bits for 256 addresses. A /16 holds 65,536 addresses, a /32 a single one. Routers forward each packet to the next hop of the longest prefix that matches its destination, so a specific /24 route overrides a broad default route (0.0.0.0/0). Hosts use the same logic: destinations inside their own prefix are reached directly on the local network; everything else goes to the default gateway.
Public and private. Public addresses are globally unique and routed on the internet. RFC 1918 reserves three private ranges, 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16, for internal networks. Anyone may use them, so they are not unique and are not routed on the internet; ISPs filter them at their edges. Other special ranges include 127.0.0.0/8 (loopback), 169.254.0.0/16 (link-local), 100.64.0.0/10 (shared space for carrier-grade NAT) and the documentation ranges used in this lesson.
NAT (strictly NAPT, or port address translation). A router with one public address keeps a translation table. When a device sends a packet out, the router replaces its private source address and port with the public address and a free port, and records the pair. When a reply arrives for that public port, it looks up the entry and rewrites the destination back. Entries time out after inactivity (often tens of seconds for UDP, minutes to hours for TCP). Because the table only contains flows started from inside, unsolicited inbound packets have nowhere to go and are dropped. Port forwarding adds a static entry; peer-to-peer applications use STUN, TURN and hole punching to work around NAT. Carrier-grade NAT (CGNAT) adds a second layer at the ISP, so many customers share one public address.
Ports. Well-known services listen on fixed ports (443 for HTTPS, 22 for SSH, 53 for DNS, 5432 for PostgreSQL). Clients use ephemeral source ports chosen by the operating system, typically from a high range. A server can hold many connections on port 443 at once because each has a different client address or client port.
IPv6. An ISP delegates a prefix to each customer (commonly a /56 or /48); each local network is a /64, and devices configure their own addresses within it (SLAAC, often with randomised privacy addresses, or DHCPv6). Packets travel end to end with their real addresses, so no translation is needed. Home and enterprise routers still run stateful firewalls that allow replies to outbound traffic and block unsolicited inbound traffic by default. Most networks run dual stack (IPv4 and IPv6 side by side); clients try both and use whichever connects first (Happy Eyeballs). Google's public statistics show close to half of its users reaching it over IPv6 in recent years, with large differences between countries and networks.
When to use it
Address planning matters every time you build a network: cloud VPCs and subnets, Kubernetes pod and service ranges, VPN and office networks. It also matters for application behaviour: who the server thinks the client is, how rate limits and geo-location work, and whether peers can connect to each other directly.
When not to
- Do not identify users by IP address. Many users share one address behind NAT and CGNAT, and one user changes addresses as they move between networks.
- Do not hard-code IP addresses in clients when a DNS name or service discovery can provide indirection.
- Do not rely on NAT as your security boundary. It blocks unsolicited inbound flows by accident, not by policy; use a firewall with explicit rules.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| NAT for IPv4 | Many devices share scarce public addresses | Broken end-to-end reachability, state in the router, complications for peer-to-peer and logging |
| CGNAT at the ISP | ISP serves customers with few IPv4 addresses | Shared addresses across customers, harder abuse tracking, no inbound connections |
| IPv6 | Plentiful global addresses, no translation | Dual-stack operation, new address-planning and firewall work |
| Large private subnets (e.g. a /16 per VPC) | Room to grow | Overlap with other networks you may later need to connect |
Alternatives
- IPv6-only networks with NAT64/DNS64: devices speak only IPv6; a gateway translates to reach IPv4-only servers. Mobile carriers use this widely.
- Overlay networks: Kubernetes CNIs, mesh VPNs and service meshes give workloads their own address spaces, independent of the underlying network.
- Proxies and load balancers: present one public address for many backends, which is NAT-like but at the application or transport layer.
Failure modes
- Overlapping private ranges: two VPCs or a VPC and an office network both use
10.0.0.0/16; they cannot be peered or connected by VPN without renumbering or extra NAT. - NAT port or table exhaustion: a NAT gateway can map only so many simultaneous flows per public address (one per source port, about 64,000 per destination); busy services see connection failures.
- Idle timeouts: NAT or firewall entries expire on long-lived idle connections, which then fail silently. Keepalives prevent it.
- Lost client identity: behind a load balancer or proxy the server sees the proxy's address. Use
X-Forwarded-For,Forwardedor the PROXY protocol, and only trust them from your own proxies. - Subnet too small: autoscaling or Kubernetes runs out of addresses in a subnet sized for today.
In production
Cloud networks are designed as private CIDR blocks per VPC and per subnet, with non-overlapping ranges planned across all environments and regions. Outbound traffic from private subnets leaves through a NAT gateway with a few static public addresses (useful for partners who allow-list IPs). Inbound traffic arrives at load balancers with public addresses and is forwarded to private backends. Services read client identity from trusted forwarding headers. Rate limits key on user or API token rather than on IP alone. New networks increasingly enable IPv6 or dual stack from the start.
Common misconceptions
- "An IP address identifies a person or a device." It identifies an interface at a moment; NAT, CGNAT, mobile networks and privacy addresses break any one-to-one mapping.
- "NAT is a firewall." It drops unmapped inbound packets as a side effect, but it does not inspect or filter by policy.
- "IPv6 devices are exposed to the whole internet." They are addressable, but the default stateful firewall blocks unsolicited inbound traffic.
- "A port is a physical thing." It is a number in the transport header that the operating system uses to deliver data to a socket.
Interview angle
IP details come up indirectly. Be ready to explain why a backend behind a load balancer sees the balancer's IP and how to recover the client's (X-Forwarded-For, PROXY protocol), why per-IP rate limiting is unfair behind CGNAT, why VPC ranges must not overlap if they will ever be connected, and why private subnets reach the internet through a NAT gateway. Knowing that a connection is the 5-tuple helps when discussing load balancer connection limits or NAT port exhaustion.
Model assumptions
This lesson is a scripted walk-through, not a simulation: every packet is placed by hand, and packet headers and the NAT table are labels. Addresses come from the documentation ranges (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32) and the private 192.168.1.0/24. One-way latencies are illustrative (3 ms from devices to the home router, 10 ms to the ISP, 20 ms on to the server). The path between ISP and server is collapsed into one hop. The home router is shown delivering the laptop-to-phone packet; in practice the router's built-in switch forwards it at layer 2 without routing. Real NAT implementations differ in port allocation, timeouts and how they treat inbound packets (most drop them silently).