CMD Guide
HomeSystem DesignNetworking Fundamentals

IP Addressing, Subnets & CIDR

What an IP address is

An IP address is the network-layer identity of a host — the "where" that routing uses to move a packet across networks. Two versions coexist:

Public vs private addresses

Certain IPv4 ranges are reserved as private — routable only inside a local network, never on the public internet. A NAT gateway translates many private hosts behind one public address. Knowing these ranges on sight is genuinely useful when reading any cloud VPC config:

RangeCIDRSizeTypical use
10.0.0.0 – 10.255.255.25510.0.0.0/8~16.7MLarge private networks / cloud VPCs
172.16.0.0 – 172.31.255.255172.16.0.0/12~1MMid-size networks
192.168.0.0 – 192.168.255.255192.168.0.0/16~65KHome / small office
127.0.0.0 – 127.255.255.255127.0.0.0/8~16.7M (all local)Loopback (localhost) — special-use, not RFC 1918 private

One category note: the three RFC 1918 ranges are what "private" formally means; loopback is a separate reserved block (RFC 1122's special-use space) that never leaves the host at all.

CIDR — how address blocks are sized

CIDR (Classless Inter-Domain Routing) notation appends a prefix length: 10.0.1.0/24. The number is how many leading bits are fixed (the network part); the remaining bits are host addresses. The rule of thumb:

Each step down in prefix length doubles the block. A /24 holds 2(32−24) = 256 addresses; a /16 holds 65,536.
CIDRHost bitsAddressesMnemonic
/3201A single host
/248256One "class C" subnet
/161665,536A large subnet / VPC
/82416,777,216A huge block

This is the math behind sizing a cloud subnet, writing a firewall/security-group rule, or reading a routing table. When you allocate 10.0.0.0/16 to a VPC and carve /24s out of it for each availability zone, you are just spending host bits.

Takeaways

NAT is not infinite

A NAT gateway keeps a connection table keyed by the 4-tuple the far side sees: (source IP, source port, destination IP, destination port). With one public IP, all hosts behind it share the same ~64,512 ephemeral source ports per destination — because the destination is part of the key, the same translated source port can be reused toward a different (dst-ip, dst-port) pair. If 2,000 pods all call the same downstream API without connection reuse, the port table for that one destination fills and new connections are refused even though bandwidth is plentiful. The exhaustion math:

ScenarioEphemeral portsDestinationsConcurrent connections possible
One public IP, one downstream API64,5121~64K
One public IP, 100 downstream APIs64,512 per destination100up to ~6.4M total (64,512 per (dst-ip, dst-port) pair)
10,000 pods calling 1 API64,5121~64K, easily exhausted

One nuance: this assumes the NAT allocates ports per destination pair — Linux netfilter SNAT and cloud NAT gateways behave this way for TCP (AWS documents its NAT Gateway as supporting up to ~55,000 simultaneous connections to each unique destination per IPv4 address). An endpoint-independent NAT that reserves one global mapping per internal socket instead has a single shared ~64K budget across all destinations — know which one you are behind before doing capacity math.

If 10,000 containers each keep 10 idle connections to the same database, that is 100,000 entries against the 64K-per-destination limit. NAT exhaustion produces confusing errors: new outbound connections time out while inbound traffic and established flows keep working. The fix is connection pooling, shorter idle timeouts, or assigning more public IPs. NAT also hides the original source IP from servers, complicates logging/rate-limiting, and breaks protocols that embed IP addresses (some IPsec modes, FTP active mode).

Trade-offs: private addresses and NAT

Worked split: a VPC into availability zones

Allocate 10.0.0.0/16 to a VPC. That spans 10.0.0.010.0.255.255 (65,536 addresses). Subnet count is pure bit math: a /16 has 16 host bits; each /24 consumes 8 of them, so you can carve 2(24−16) = 256 distinct /24 subnets — not 254. (The classic −2 is for usable hosts inside one subnet, never for how many subnets fit in a prefix.)

AZCIDRTotal addressesUsable on AWS
AZ-a10.0.0.0/24256251
AZ-b10.0.1.0/24256251
AZ-c10.0.2.0/24256251

After carving three /24s you still have 256 − 3 = 253 free /24 slots in the VPC. Inside each AWS /24, usable hosts are 256 − 5 = 251 (network, broadcast, and three AWS-reserved addresses) — that host reservation does not shrink the subnet count. The third octet increments by one for each /24, which is the usual convention.

CIDR aggregation example

Suppose a router sees four adjacent subnets:

These four blocks share the first 22 bits (10.0.0.x through 10.0.3.x), so they can be summarized as one route: 10.0.0.0/22. Aggregation reduces routing-table size and keeps upstream routers from tracking every small subnet. The rule: the aggregate must cover exactly the contiguous blocks you intend. For instance, 10.0.0.0/24 and 10.0.2.0/24 do not sit together on a power-of-two boundary, so the smallest single prefix covering both is 10.0.0.0/22 — which also pulls in 10.0.1.0/24 and 10.0.3.0/24, blocks that may belong to someone else. (A /23 like 10.0.0.0/23 spans only 10.0.0.0/24 and 10.0.1.0/24, so it cannot summarize 10.0.2.0/24 at all.) When several routes overlap, a router forwards using longest-prefix match — the most specific route (the largest prefix length) covering the destination wins, so a /24 entry always takes precedence over a /22 that happens to contain it.

Drills — do the math before an interviewer makes you

CIDR is one of the few system-design topics you may be asked to compute live. Work each one before opening the answer.

  1. You hold 10.0.0.0/16 and need 6 subnets of at least 8,000 hosts each — what prefix, and how many fit?
    Show answer

    8,000 usable hosts needs 13 host bits: 213 = 8,192 total, and 8,192 − 5 = 8,187 ≥ 8,000 even after AWS's five reserved addresses. 13 host bits means a /19. A /16 holds 2(19−16) = 8 distinct /19s, so all 6 fit — with 2 spare.

  2. What is the smallest single prefix covering both 10.0.4.0/24 and 10.0.7.0/24?
    Show answer

    Compare the third octets in binary: 4 = 00000100, 7 = 00000111. They agree on the octet's first six bits and first differ at its seventh bit — i.e. the addresses share their first 22 bits, a /22 boundary. The answer is 10.0.4.0/22 (third octets 4–7) — which also pulls in 10.0.5.0/24 and 10.0.6.0/24, so make sure those belong to you before advertising the aggregate.

  3. A router holds both 10.0.4.0/22 and 10.0.5.0/24. A packet arrives for 10.0.5.9 — which route wins, and why?
    Show answer

    The /24 wins by longest-prefix match: when several routes cover the same destination, the router forwards using the most specific one (the largest prefix length). The forwarding rule in one line: match all candidate routes, pick the longest prefix, forward there.

Private vs public address table

PropertyPrivate addressPublic address
Routable on the internetNoYes
ScopeInside an organization or VPCGlobally unique
Cost / scarcityFree and abundantLimited (IPv4) or unlimited (IPv6)
Security postureHidden from direct internet reachDirectly reachable; needs firewall
Common examples10.x.x.x, 172.16–31.x.x, 192.168.x.xAny routable IPv4/IPv6 assigned by an ISP or cloud provider

Re-authored for this guide, with concepts and diagrams adapted from Karan Pratap Singh’s System Design (course, MIT licence) and the System Design Primer (CC BY 4.0). Diagrams © their respective authors.

🤖 Don't fully get this? Learn it with Claude

Stuck on IP Addressing, Subnets & CIDR? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **IP Addressing, Subnets & CIDR** (System Design) and want to truly understand it. Explain IP Addressing, Subnets & CIDR from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **IP Addressing, Subnets & CIDR** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **IP Addressing, Subnets & CIDR** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **IP Addressing, Subnets & CIDR** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes