SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
  • OSI Model
  • IP Address
  • TCP vs UDP
  • HTTP / HTTPS
  • DNS
  • Proxy vs Reverse Proxy
  • Checksums
  • HTTP/1.1 vs HTTP/2 vs HTTP/3
  • Cookies and CORS
  • Subnets, CIDR and NAT
  • Firewalls and VPN
  • Ports and Sockets
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Foundations/Networking

Subnets, CIDR and NAT

Carve a network into pieces, write address ranges the short way, and share one public IP.

⏱ 14 min readIntermediateHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

A subnet is a smaller slice of a bigger network. CIDR is the short way to write an address range, such as 10.0.1.0/24. NAT lets many private machines share one public IP address.

  • An IPv4 address has 32 bits. The number after the slash says how many of those bits name the network. The rest name the host.
  • /24 means 24 network bits and 8 host bits, so 256 addresses (254 usable in classic IPv4).
  • Three ranges are private and never routed on the public internet: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16.
  • A NAT gateway rewrites private addresses to a public one, so private servers can reach out without being reachable from outside.

02Concept: What is it?

Addresses, masks and CIDR

An IPv4 address like 10.0.1.77 is really a 32-bit number written as four bytes. To split it into a network part and a host part, we use a prefix length. In CIDR notation (Classless Inter-Domain Routing) we write the address, a slash, and the number of network bits: 10.0.1.0/24.

The maths for 10.0.1.77/24
mask      = first 24 bits set to 1   -> 255.255.255.0
network   = address AND mask         -> 10.0.1.0
broadcast = network OR (NOT mask)    -> 10.0.1.255
addresses = 2^(32 - 24)              -> 256
usable    = 256 - 2                  -> 254   (10.0.1.1 to 10.0.1.254)

The smaller the host part, the smaller the subnet. A /16 has 65,536 addresses, a /24 has 256, a /28 has only 16. Every extra network bit halves the size.

One network cut into subnets
🌐10.0.0.0/1665,536 addresses
→↓split
🟢10.0.1.0/24public subnet
🔒10.0.2.0/24private app subnet
🗄️10.0.3.0/24private database subnet
A /16 can be divided into 256 subnets of /24. Each can have its own role and its own rules.

Why NAT exists

IPv4 has only about 4.3 billion addresses, far fewer than the number of devices. Private addresses can be reused by everyone, because they are never routed on the public internet. NAT (Network Address Translation) sits at the edge and swaps the private source address for a public one on the way out. It keeps a table so the reply finds its way back to the right machine. When it also swaps port numbers (which is what most home and cloud gateways do), it is called PAT (Port Address Translation).

Many private hosts, one public address
🖥️10.0.2.11
🖥️10.0.2.12
→↓private
🚪NAT gatewaypublic IP + ports
→↓public
🌍Internet
The gateway remembers which port belongs to which private host.
🧠NAT is not a firewall
NAT happens to block unsolicited incoming traffic, because there is no table entry for it. That is a side effect, not a security design. Use proper firewall rules as well.

03Real-Life Example

🍵An Office Building with One Street Address
A big office building has one street address for the whole world, but inside it has floors and room numbers. Each floor is a subnet (reception, engineering, finance). The mail room at the entrance is the NAT gateway. When someone inside sends a parcel, the mail room writes the building's street address on it and notes who sent it. When the reply arrives, the mail room looks in its notebook and delivers it to the right desk. A parcel with no matching note is thrown away.
Office building
Network
The street address of the building
The public IP address
A floor of the building
A subnet
Room numbers on that floor
Host addresses inside the subnet
How many rooms a floor has
The prefix length (/24 = 256)
The mail room notebook
The NAT table
Internal room numbers nobody outside knows
Private IP addresses
A parcel nobody asked for
An unsolicited packet, dropped by NAT
A reply finds its way home
📤Send10.0.2.11:50112
→↓rewrite
🚪Gateway203.0.113.10:40001
→↓reply
📥Look up40001 is 10.0.2.11

04Why Do We Need It?

🗺️Organise the network
Subnets let you group machines by job (web, app, database) and treat each group differently.
🛡️Limit the blast radius
Put databases in a private subnet with no route from the internet, so an attacker cannot reach them directly.
♻️Save public addresses
NAT lets thousands of machines share a few public IPs.
🔀Simple routing
Routers keep one rule per range instead of one rule per address.
🧩Plan for growth
Good CIDR planning leaves room to add machines and networks without renumbering.
🔗Connect networks
Ranges that do not overlap can be joined (peering, VPN). Overlapping ranges cannot.
CIDR describes a block of addresses, subnets split it up, and NAT lets the private ones reach the outside world.

05Where Do We Use It?

☁️Cloud networks
AWS VPCs, Azure VNets and Google VPCs are all built from a CIDR block cut into subnets.
🏠Home routers
Your router uses NAT so your phone, laptop and TV share one public address.
🏢Company networks
Offices use private ranges, with different subnets for teams, servers and guests.
☸️Kubernetes
Pods and services get addresses from ranges that you must plan so they do not clash with others.
🔐Security rules
Firewall and security group rules often name a CIDR range as the allowed source.
🌐Peering and VPNs
Joining two networks needs non-overlapping CIDR blocks.

06Types / Parts / How It Works

Common prefix sizes

CIDRMaskTotal addressesUsable hosts (classic)Typical use
/8255.0.0.016,777,21616,777,214A huge private block such as 10.0.0.0/8
/16255.255.0.065,53665,534A whole cloud network (VPC)
/24255.255.255.0256254One subnet for a tier or an availability zone
/28255.255.255.2401614A tiny subnet (cloud providers reserve a few, so fewer are free)
/32255.255.255.25511 (a single address)One exact host in a rule

The private ranges (RFC 1918)

RangeAddressesOften used for
10.0.0.0/8About 16.7 millionBig companies and cloud networks
172.16.0.0/12About 1 million (172.16.x.x to 172.31.x.x)Medium networks, some container setups
192.168.0.0/1665,536Home and small office networks

Public and private subnets

A typical three-tier layout
Public subnet
⚖️Load balancerpublic IP
🚪NAT gatewayoutbound only
Private app subnet
🖥️App serversprivate IPs
Private data subnet
🗄️Databaseno internet route
Reachable from internet Private, outbound via NAT Fully isolated
Only the load balancer is reachable from the internet. Private subnets reach out through a NAT gateway.

Kinds of NAT

KindWhat it doesExample
Source NAT / PATRewrites the source of outgoing traffic, using ports to tell hosts apartHome router, cloud NAT gateway
Destination NAT / port forwardingSends incoming traffic on a public port to one private hostForward port 443 to an internal server
Static (one-to-one) NATMaps one private address to one public addressAn internet-facing instance with a fixed public IP
VisualizerSubnet calculator, subnet splitter and NAT tablePick an address and move the prefix slider: watch the cyan network bits and amber host bits change, and the usable host count halve with every extra bit. Then split 10.0.0.0/16 into smaller subnets, and send a reply or an unsolicited packet to the NAT table.
Pick an IP address
10.0.1.77 in binary: cyan = network part (24 bits), amber = host part (8 bits)
00001010000000000000000101001101
Network address
10.0.1.0
first address
Broadcast
10.0.1.255
last address
Usable hosts
254
256 minus 2
First / last host
10.0.1.1 – 10.0.1.254
range you can assign
Private range?
yes
10.0.0.0/8
10.0.1.77/24 sits in the network 10.0.1.0. The first 24 bits are the network part, the last 8 bits pick the host (254 usable).
256 subnets of 256 addresses each (254 usable). First 8 shown, with example roles (illustrative):
10.0.0.0/2410.0.0.1 – 10.0.0.254public
10.0.1.0/2410.0.1.1 – 10.0.1.254private app
10.0.2.0/2410.0.2.1 – 10.0.2.254private database
10.0.3.0/2410.0.3.1 – 10.0.3.254spare
10.0.4.0/2410.0.4.1 – 10.0.4.254spare
10.0.5.0/2410.0.5.1 – 10.0.5.254spare
10.0.6.0/2410.0.6.1 – 10.0.6.254spare
10.0.7.0/2410.0.7.1 – 10.0.7.254spare
A packet arrives from the internet
NAT table (port translation): every host uses source port 50112, but the world sees one public IP with different ports.
10.0.2.11:50112⇄203.0.113.10:40001
10.0.2.12:50112⇄203.0.113.10:40002
10.0.2.13:50112⇄203.0.113.10:40003
Packet to 203.0.113.10:40002: the table has an entry, so the gateway forwards it to 10.0.2.12:50112.
Public IPs used
1
203.0.113.10
Private hosts
3
share it
Table entries
3
one per connection

The bit maths is real: mask = the first N bits set to 1, network = IP AND mask, broadcast = network OR (NOT mask). Classic IPv4 reserves the network and broadcast addresses, and cloud providers reserve a few more per subnet (check your provider). The public address and ports in the NAT table are made-up examples.

07How to Use It / Steps / Implementation

Planning an address space

  1. 1
    Pick a private block
    Choose a range like 10.0.0.0/16 for each network. Give every environment (production, staging, other regions) its own non-overlapping block.
  2. 2
    Think about future links
    If you may peer with another network or a VPN, check that the ranges do not overlap. Renumbering later is painful.
  3. 3
    Cut it into subnets
    Make one subnet per tier and per availability zone, for example public, app and data, in each zone.
  4. 4
    Size generously
    Leave room to grow. Remember that cloud providers reserve a few addresses per subnet, and that some services (load balancers, Kubernetes) use many.
  5. 5
    Route on purpose
    Only the public subnet has a route to the internet gateway. Private subnets send outbound traffic through a NAT gateway.
  6. 6
    Add rules by range
    Write firewall rules with CIDR blocks, such as allow 10.0.2.0/24 to port 5432.
  7. 7
    Watch NAT limits
    A NAT gateway has a limit on simultaneous connections per destination. Many connections to one target can exhaust ports.
Checking if an address is inside a block (what a router does)
address 10.0.2.77 in 10.0.2.0/24 ?
  mask    = 255.255.255.0
  77 AND 0 stays 77 in the host part, network part = 10.0.2
  (10.0.2.77 AND mask) == 10.0.2.0  ->  yes, it is inside
💡Quick mental maths
Block size = 2 to the power (32 minus prefix). A /24 is 256, a /25 is 128, a /26 is 64, a /27 is 32. Subnets of that size start at multiples of the size, so 10.0.1.64/26 is valid but 10.0.1.70/26 is not.
⚠️Overlapping ranges
Two networks that both use 10.0.0.0/16 cannot be peered or joined by VPN without extra tricks. Plan unique ranges early.

08Key Terms to Know

TermMeaning in easy words
SubnetA smaller network carved out of a larger address range
CIDRClassless Inter-Domain Routing: writing a range as address/prefix, like 10.0.0.0/16
Prefix lengthThe number of leading bits that name the network (the number after the slash)
Subnet maskThe same idea written as an address, such as 255.255.255.0 for /24
Network addressThe first address of a subnet, which names the subnet itself
Broadcast addressThe last address of a classic IPv4 subnet, which reaches every host in it
Private addressAn address from 10/8, 172.16/12 or 192.168/16, not routed on the public internet
NATNetwork Address Translation: rewriting addresses as traffic crosses a boundary
PATPort Address Translation: NAT that also rewrites ports so many hosts share one address
NAT gatewayA managed device or service that gives private subnets outbound internet access
Public / private subnetA subnet with, or without, a route to an internet gateway

09Pros, Cons and Trade-offs

✅ Pros
  • Clear structure for large networks
  • Private subnets keep sensitive systems off the internet
  • NAT saves scarce public IPv4 addresses
  • Rules by range are short and easy to read
  • Routing tables stay small
⚠️ Cons
  • Bad planning leads to overlapping or exhausted ranges
  • NAT hides the real client address from some tools
  • NAT breaks some protocols that carry addresses inside the data
  • NAT gateways have connection limits and a cost
  • More subnets mean more route tables and rules to manage

Trade-offs

ChoiceWe gainWe lose
A big subnet (/16)Plenty of room, simpleEverything shares one rule set, and a large blast radius
Many small subnetsFine-grained rules and isolationMore to manage and easy to run out in one subnet
Private subnet + NATServers can reach out but are not reachableNAT cost, a possible bottleneck, and one more thing to fail
Public IP on every serverDirect and simpleA large attack surface
Large address planRoom for growth and peeringNeeds careful records so ranges stay unique
IPv6No shortage, no NAT neededDifferent tools and habits to learn

10Common Problems and Mistakes

Common mistakes

  1. Overlapping ranges. Two networks use the same block, and later they cannot be connected.
  2. Subnets too small. A /28 runs out as soon as a load balancer or autoscaling group grows.
  3. Forgetting the reserved addresses. A /24 does not give 254 free addresses on every cloud, because providers keep a few.
  4. Misaligned blocks. A subnet must start at a multiple of its size. 10.0.1.70/26 is not a valid network.
  5. Databases in a public subnet. One wrong rule and they are on the internet.
  6. One NAT gateway for everything. It is a single point of failure, and cross-zone traffic can cost more.
  7. Treating NAT as security. It is not a firewall. Add explicit rules.

Things to watch

🔌Port exhaustion
Too many connections to the same destination through one NAT address can run out of ports.
🧮Address use by services
Load balancers, Kubernetes pods and managed services can each take many addresses.
🔁Hairpin NAT
A host reaching its own public address through the gateway needs special handling.
🕵️Logs show the NAT address
Servers outside may see only the gateway IP, so tracing a client needs NAT logs.

11Real-World Examples

🏠Your home Wi-Fi
Devices get 192.168.x.x addresses, and the router translates them to your provider-assigned public address.
☁️Cloud VPC
A VPC with public and private subnets per zone is the standard starting layout. Private subnets use a NAT gateway for updates.
☸️Kubernetes clusters
Node, pod and service ranges are each a CIDR block that must not overlap with each other or your other networks.
📶Mobile networks
Many phones sit behind carrier-grade NAT and share public addresses.
🏢Mergers
When two companies join networks, overlapping 10.x ranges are a famous headache.
🌐IPv6 adoption
IPv6 has so many addresses that NAT is not needed, and it is growing.
📝Note
Reserved-address counts, NAT limits and pricing differ between cloud providers. Check the provider documentation before sizing.
Mini case: 3 subnets in a /16
🌍Internet
→↓443
⚖️10.0.1.0/24public: load balancer
→↓8080
🖥️10.0.2.0/24private: apps
→↓5432
🗄️10.0.3.0/24private: database
The load balancer is public. App servers and the database are private.

12Related Topics to Read Next

IP Address→

Subnets and CIDR slice the IP address space.

Firewalls and VPN→

Rules often name a CIDR range, and a VPN joins two address spaces.

OSI Model→

Addressing and routing live at layer 3, and ports at layer 4.

Ports and Sockets→

NAT tables track both addresses and ports.

Proxy vs Reverse Proxy→

A NAT gateway and a proxy both sit in the middle, in very different ways.

What are Load Balancers?→

Load balancers usually live in a public subnet in front of private servers.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • ✔CIDR: address/prefix, such as 10.0.1.0/24. The prefix is the number of network bits.
  • ✔Size: 2^(32 − prefix) addresses. Subtract 2 for usable hosts. Each extra bit halves it.
  • ✔Network / broadcast: first and last address of the block. Not assignable to hosts.
  • ✔Private: 10/8, 172.16/12, 192.168/16. Not routed on the internet.
  • ✔NAT / PAT: many private hosts share one public IP, told apart by ports and a table.
  • ✔Public vs private subnet: a route to the internet gateway, or not. Private ones go out through NAT.
  • ✔Plan: unique, non-overlapping ranges, with room to grow. Cloud providers reserve a few per subnet.
  • ✔One line: “CIDR names the block, subnets slice it, NAT lets the private ones talk out.”
← Previous
Cookies and CORS
Next →
Firewalls and VPN