Routing in the cloud: CIDR, route tables, NAT, security groups vs NACLs, peering, BGP/anycast, MTU

Route table, then stateful security group, then stateless NACL — in that order, both directions. Peering is not transitive and NAT gateways are zonal.

Read the briefing
About 3 min to read 7 drills

What to remember

  1. Security groups are stateful; NACLs are not. A hanging connection after the SYN is usually a NACL missing the ephemeral return range.
  2. VPC peering is strictly pairwise. A→B and B→C does not give A→C. Transit gateway or per-pair peerings.
  3. One NAT gateway per zone, routed per zone. A shared NAT turns one zone's outage into everyone's egress outage, and costs cross-zone traffic.
  4. Public subnet just means a default route to the internet gateway. Nothing about the addresses. NAT is outbound only.
  5. Reference security groups, not CIDRs. 'app may reach db on 5432' survives IP changes; hard-coded ranges rot.

The big picture

Cloud networking is classic IP routing wearing new names: CIDR blocks and subnets, route tables with a default route, NAT for private egress, stateful security groups versus stateless NACLs, peering and transit gateways for connecting networks, and BGP and anycast at the edge. Interviewers use it to check that you can predict where a packet goes and why a return packet is dropped, which is most of cloud connectivity debugging.

The mental model

Every packet asks three questions in order. Where does the route table send me (longest prefix wins, local route first)? Am I allowed through the filters on the way (security group: stateful, allow-only; NACL: stateless, ordered rules, remember the return path)? Do I need my address rewritten to get there (NAT for private-to-public, and the reverse translation on the way back)? Debug in that order.

In depth

CIDR, subnets, and route tables

A VPC is a CIDR block (say 10.20.0.0/16, 65,536 addresses); subnets carve it into smaller blocks, each in one availability zone, each with a route table. Plan CIDRs for growth and for peering: overlapping ranges cannot be peered, and Kubernetes pod networks eat addresses fast (a /16 per cluster is common with VPC-native CNIs). Route tables are longest-prefix-match: the local route covers the VPC CIDR, more specific routes go to peering connections or gateways, and 0.0.0.0/0 decides egress: an internet gateway for public subnets, a NAT gateway or instance for private ones. "Public subnet" means nothing more than "its route table sends default traffic to an internet gateway and instances get public IPs".

NAT, security groups, and NACLs

Private instances reach the internet through NAT: the gateway rewrites source IP and port (SNAT) and keeps a translation table to map replies back. Consequences: NAT gateways are per-zone (cross-zone traffic costs money and creates a zonal dependency), they have connection limits per destination (port exhaustion appears as ErrorPortAllocation), and inbound connections to private instances need a load balancer or DNAT, not NAT.

Security groups are stateful allow-lists attached to instances/ENIs: allow inbound 443 and the reply is allowed automatically; they can reference other security groups, which is the right way to express "only the app tier may talk to the database". Network ACLs are stateless, ordered allow/deny lists on subnets: an inbound allow on 443 does nothing for the reply unless outbound ephemeral ports (1024–65535) are allowed too; this asymmetry is the source of most NACL incidents. Rule of thumb: security groups for policy, NACLs only for coarse subnet-level blocks.

Peering, transit, BGP, anycast, and MTU

VPC peering connects two VPCs one-to-one, non-transitive (A–B and B–C does not give A–C), requires non-overlapping CIDRs and routes on both sides plus security-group allowances. A transit gateway (or hub VPC) is the hub-and-spoke answer for many VPCs and on-prem links. BGP is how the internet and your datacenter exchange routes: an autonomous system announces prefixes, peers choose paths by policy (local preference, AS path length); your cloud VPN or Direct Connect uses BGP to learn routes dynamically. Anycast announces the same prefix from many locations so clients reach the nearest instance; it is how DNS roots and CDNs work, and it means a failure in one location shifts traffic by withdrawing the announcement. MTU across tunnels (VPNs, peering over VPN, overlays) is smaller than 1500; without working PMTU discovery, large packets disappear, which is the same "small works, large fails" signature seen on hosts.

Myths and facts

Myth: Security groups need explicit rules for return traffic.

Fact: Security groups are stateful; replies to allowed traffic are permitted. NACLs are the stateless ones that need ephemeral-port allowances.

Myth: VPC peering is transitive.

Fact: It is strictly pairwise; hub-and-spoke needs a transit gateway or per-pair peerings.

Myth: A NAT gateway lets the internet reach my private instances.

Fact: NAT is outbound; inbound needs a load balancer, DNAT, or a public IP.

Myth: Public subnet means the subnet has a public IP range.

Fact: It means the route table sends default traffic to an internet gateway; the addresses are still private VPC addresses with public IPs mapped 1:1.

Myth: If two hosts can ping, TCP will work.

Fact: Ping proves ICMP and routing; NACL ephemeral rules, security groups, and MTU can still break TCP.

How pros do it

  • Plan CIDRs with growth and peering in mind; reserve space for Kubernetes pod and service ranges.
  • Express policy with security groups referencing security groups; keep NACLs minimal and always allow ephemeral return ports.
  • Deploy one NAT gateway per zone and route each private subnet to its own zone's gateway; alert on port allocation errors.
  • Use a transit gateway (or equivalent) for more than a handful of VPCs; document routes in code.
  • Set and test MTU across tunnels; enable PMTU discovery or clamp MSS at the tunnel endpoints.

Questions people ask

Security groups versus NACLs — what is the difference?

Stateful versus stateless, then the consequences. A security group attaches to the instance or ENI, is allow-only, and lets the reply back automatically; a NACL attaches to the subnet, evaluates numbered rules in order with explicit denies, and sees every packet alone, so the return path to ephemeral ports 1024–65535 must be allowed explicitly. Finish with the design rule: security groups referencing security groups for policy, NACLs only for coarse subnet-level blocks.

What makes a subnet public, and how does a private instance reach the internet?

Public means only that the subnet's route table sends 0.0.0.0/0 to an internet gateway and instances carry public IPs; the addresses are still VPC addresses. A private instance goes out through a NAT gateway: security group egress, the route table's default route to the NAT, source translated to the NAT's public IP, out via the internet gateway, and the reply mapped back through the NAT's translation table. Say 'NAT is outbound only; inbound needs a load balancer, DNAT or a public IP' before they ask.

Inbound works but the connection hangs after the SYN. NACL or security group?

Almost always the NACL: the inbound rule allowed the SYN, but outbound to ephemeral ports is not allowed, so the SYN-ACK is dropped. A security group would have allowed the reply automatically. Say 'hangs, not refused' — a drop times out, a rejection is instant — and you have shown you know what the packet capture would look like.

How do I debug 'works from subnet A, times out from subnet B'?

Diff, in order: the two subnets' route tables, then their NACLs, then the instances' security groups, then MTU if a tunnel is involved; the difference is the answer. Naming the order — route first, filters second, translation and MTU last — is what they are listening for.

Why can't VPC A reach VPC C through B?

Peering is non-transitive: B will not forward A's traffic. Give the two fixes — a direct A–C peering, or a transit gateway with routes in every attached VPC — and the cost trade: peering for a few VPCs, transit once you have many or an on-prem link.

Our NAT gateway shows ErrorPortAllocation. Why?

Too many concurrent connections from the gateway to one destination exhaust its source ports — roughly 55,000 per NAT IP per destination. Say the fixes in order of speed: add IPs or gateways, spread destinations, and pool or reuse connections in the clients so the flow count drops.

How does anycast failover work?

Each site announces the same prefix via BGP and routers forward to the topologically nearest one; when a site withdraws its announcement — by hand or by health-check-driven automation — routes converge to the next-best site in seconds, with no DNS change and no client action. Add that this is how root DNS and CDN edges work.

Further reading

Drills · 7

Answer in your head, or out loud, before you reveal. Mark each one: "Again" brings it back, "Got it" counts on your desk.

1 Pick the strongest answer

Classic one: a web server's security group allows inbound 443 from anywhere and its subnet's NACL allows inbound 443, yet clients' TLS handshakes hang and never complete. What is missing?

  1. A route to the NAT gateway. Inbound public traffic uses the internet gateway route; NAT is outbound.
  2. An inbound security group rule for port 80. The handshake on 443 is what fails; 80 is irrelevant.
  3. An outbound security group rule for 443. Security groups are stateful; replies are allowed without an outbound rule.
  4. An outbound NACL rule allowing TCP 1024–65535 (ephemeral ports): NACLs are stateless, so the SYN-ACK to the client's ephemeral port is dropped. Security groups would have allowed the reply automatically; NACLs need the return path spelled out.
In short

Add an outbound NACL rule for ports 1024–65535; NACLs are stateless, so the reply to the client is dropped without it.

Why

Stateless versus stateful is the whole question. NACLs see each packet alone, so the reply to a client on port 51234 needs an outbound allow for ephemeral ports; forgetting it produces a SYN that is accepted and a SYN-ACK that vanishes.

The common mistake

Adding an outbound security group rule, or a rule for a different port. Security groups already allow the reply because they are stateful; the missing piece is the NACL's outbound allow for the ephemeral range 1024–65535, plus the mirror image for outbound-initiated traffic.

Read the docs

2 Answer out loud

Walk me through it: a private instance calls an external API. Trace the packet out and the reply back, naming each cloud construct it touches, and tell me what would break it at each hop. Four to six sentences.

In short

A strong answer traces security group, route table, NAT gateway and internet gateway out and the reverse back, and names what breaks at each hop.

What a strong answer does
  • path: Security group egress → subnet route table 0.0.0.0/0 → NAT gateway (SNAT, zonal) → public subnet route → internet gateway; reply: NAT translation table → route back → security group allows as stateful reply; NACLs checked statelessly both ways.
  • failure modes: Missing default route, NAT in a failed zone, NAT port exhaustion, NACL missing ephemeral egress/ingress, security group egress denied, MTU on tunnels.
  • communication: Hop by hop with the failure at each hop, plainly stated.
Why

Tracing both directions with the failure at each hop is what interviewers want; the stateful/stateless contrast and the zonal NAT dependency are the details that show experience.

The common mistake

Tracing only the outbound direction, or saying the packet 'goes through the internet gateway' and stopping. The return path is where the stateful-versus-stateless distinction and the NAT translation table live, and it is where the interviewer's follow-ups go.

3 Pick the strongest answer

VPC A peers with VPC B, and VPC B peers with VPC C. An instance in A cannot reach an instance in C, even though A's route table has a route for C's CIDR pointing at the A–B peering. Why not, and what would you do about it?

  1. The CIDRs overlap. Overlap would have prevented creating the peering, not silently dropped traffic.
  2. A needs a NAT gateway to reach C. Peered traffic is private-to-private and needs no NAT.
  3. VPC peering is non-transitive: B will not forward A's traffic to C. A needs its own peering with C (or all three attach to a transit gateway). Peering connects exactly two VPCs; the route in A points into a connection that only delivers to B.
  4. C's security group blocks A. Possible, but the traffic never reaches C's VPC; the route design is the block.
In short

VPC peering is not transitive; A needs its own peering with C or a transit gateway.

Why

Non-transitivity is the defining property of VPC peering and the reason hub-and-spoke designs use a transit gateway (or a routing VPC with appliances). Interviewers ask this because people draw peering as if it were a network.

Read the docs

4 Read the output

Walk me through it: why does internet egress fail during the zone-b outage while the peering traffic keeps working?

# route table rtb-private-a (subnet 10.20.1.0/24, zone a)
Destination      Target
10.20.0.0/16     local
10.30.0.0/16     pcx-0a1b (peering to VPC B)
0.0.0.0/0        nat-0c3d (NAT gateway in zone b)

# instance i-77 in 10.20.1.0/24 (private, no public IP)
$ curl -sS -m 10 https://api.github.com -o /dev/null -w '%{http_code}\n'
200
$ curl -sS -m 10 http://10.30.4.9:8080/health
ok
# ... zone b suffers an outage
$ curl -sS -m 10 https://api.github.com -o /dev/null -w '%{http_code}\n'
curl: (28) Connection timed out after 10004 milliseconds
000
$ curl -sS -m 10 http://10.30.4.9:8080/health
ok
  1. GitHub is down. The timing with the zone outage and the route table say otherwise.
  2. The default route sends this zone-a subnet's egress through a NAT gateway located in zone b; when zone b fails, egress fails, while peering and local routes never touched the NAT. A cross-zone NAT dependency turns a zonal outage into an egress outage for other zones.
  3. Security groups changed during the outage. Nothing indicates a change; the topology explains it.
  4. The peering connection also failed but cached the response. HTTP health checks are not cached by curl; the peering path is fine.
In short

The default route goes to a NAT gateway in zone b, so a zone-b outage kills internet egress while peering traffic never touches the NAT.

Why

NAT gateways are zonal. Best practice is one per zone with each private subnet routed to its own zone's gateway, so a zone failure takes down only that zone's egress. It also avoids cross-zone data charges. Note the failure shape, too: a timeout, not a refusal — the SYN leaves along a route whose next hop is gone and nothing answers, which is the signature of a missing route or gateway rather than a missing listener.

Read the docs

5 Find what's broken

You are reviewing this Terraform. One problem blocks any growth, one is a security hole, one will break return traffic. Which lines, and what do you change?

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "private_a" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.0.0/16"
  availability_zone = "us-east-1a"
}
resource "aws_security_group" "db" {
  vpc_id = aws_vpc.main.id
  ingress {
    from_port   = 5432
    to_port     = 5432
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
resource "aws_network_acl_rule" "app_in" {
  network_acl_id = aws_network_acl.app.id
  rule_number    = 100
  egress         = false
  protocol       = "tcp"
  rule_action    = "allow"
  cidr_block     = "0.0.0.0/0"
  from_port      = 443
  to_port        = 443
}
In short

Lines 6, 15 and 27: the subnet uses the whole VPC range, the database port is open to the world, and the NACL has no outbound rule for return traffic.

What's broken
  • Line 6: The subnet takes the entire VPC CIDR, leaving no room for other subnets or zones.
  • Line 15: The database port is open to the world; security groups should reference the app tier's security group.
  • Line 27: An inbound-only NACL allow on 443 with no matching outbound allow for ephemeral ports (1024–65535) drops every reply; NACLs are stateless.
Why

Three layers of cloud networking mistakes: address planning, policy expressed as IP ranges instead of security-group references, and statelessness forgotten. The VPC CIDR (line 2) and the open egress rule (line 18) are defensible defaults; rule_number (line 26) is fine.

Read the docs

6 Quick recall

Quick one: which is stateless — security groups or network ACLs?

In short

Network ACLs are stateless; security groups are stateful.

Accepted

network ACLs · NACLs · NACL

Why

NACLs are stateless (evaluate every packet, need ephemeral return rules); security groups are stateful.

7 Scenario: what do you do next?

Your RPC edge is anycast: three sites announce the same /24 via BGP. Monitoring shows that clients in one region are getting slow responses and some large getBlock results fail, while small calls succeed. The site nearest that region was moved to a new upstream provider this morning; its BGP session is up and the prefix is announced.

Step 1. First check?

  1. Withdraw the prefix from all three sites and re-announce. That is a global outage to fix a regional problem.
  2. From a vantage point in the affected region, traceroute/mtr to the anycast IP to see which site answers and the path MTU (ping -M do -s 1472), and compare with a healthy region. Anycast means the client's site is chosen by BGP; you must find out which site they hit and whether the new path carries full-size packets.
  3. Blame the client's ISP. Your side changed this morning; check your path first.
  4. Increase RPC timeouts. Large responses are failing structurally, not slowly.

Step 2. The affected region lands on the moved site; ping -M do -s 1472 fails there and works at other sites; the new upstream uses a tunnel with a 1400-byte MTU and filters ICMP. Mitigation?

  1. Keep the site up and add a warning banner. Failing requests are an outage regardless of banners.
  2. Lower the MTU on every RPC server globally. A global change for one site's upstream problem; and servers already respect PMTU when ICMP works.
  3. Ask clients to use smaller responses. Not a control you have, and it hides the fault.
  4. Withdraw the anycast announcement from the affected site so BGP shifts that region to the next-nearest healthy site, then fix MTU (clamp MSS at the site's edge / negotiate with the upstream) and re-announce after testing. Anycast's superpower is withdrawing a bad site; latency rises slightly, correctness returns immediately.

Step 3. Systemic fix?

  1. Health-check-driven BGP announcement (withdraw automatically when synthetic large-payload probes fail from that site), MTU/MSS clamping as standard at every edge, a change checklist for upstream moves that includes PMTU tests, and per-site SLI dashboards. Automates the mitigation you did by hand, prevents the class of fault, and makes the change visible before customers do.
  2. Never change upstream providers. Unrealistic; make changes safe instead.
  3. Stop using anycast; give each region its own IP. Loses the failover property that just saved you; the MTU fault would still exist.
In short

Find which site the region reaches and test the path MTU; withdraw that site's BGP announcement, fix the MTU, then automate withdrawal on probe failure.

Why

Anycast turns "which server did I reach" into a BGP question, and tunnels turn "large responses fail" into an MTU question. The interview shape: locate the site and path, withdraw the bad announcement, fix MTU, then automate withdrawal on synthetic probe failure.

Read the docs

Next lesson TCP: handshake, retransmission, congestion control, and the states that bite
Drill · Routing in the cloud
0:00

Loading…