7-layer reference model for network communication โ know L4 and L7 for interviews
#
Layer
Visual
Protocol
System Design Relevance
7
Application
HTTP, gRPC, DNS, SMTP
L7 Load Balancers (ALB), API Gateways, WAF
6
Presentation
TLS/SSL, JSON, Protobuf
Encryption, serialization
5
Session
WebSocket, RPC
Connection management
4
Transport
TCP, UDP, QUIC
L4 Load Balancers (NLB), port-based routing
3
Network
IP, ICMP
Routing, subnets, VPCs, BGP
2
Data Link
Ethernet, ARP
MAC addresses, switches
1
Physical
Fiber, copper
Data center hardware
Why it matters:L4 LB routes by IP+port (fast, no content inspection). L7 LB routes by URL/headers/cookies (smart, can do path-based routing). Most system design discussions happen at L4 and L7.
TCP / UDP
Transport layer โ TCP (reliable, ordered) vs UDP (fast, unreliable)
Guarantees: TCP guarantees ordered, reliable, duplicate-free delivery via sequence numbers, ACKs, and retransmission. UDP guarantees nothing โ but that's why it's fast (no overhead).
TCP: Banking, HTTPS, DB connections, email. Ports: 80/443/3306/5432/6379/9092
UDP: Live video, gaming, DNS, VoIP, QUIC (HTTP/3). Ports: 53/123/443(QUIC)
Real-world:Fortnite/Valorant use UDP for player positions (few lost packets OK, low latency critical). QUIC (HTTP/3) โ UDP-based with built-in TLS 1.3, 0-RTT resumption. Used by Chrome, YouTube, Cloudflare.
HTTP / HTTPS
Application layer โ HTTP (plaintext) vs HTTPS (TLS encrypted)
HTTP โ Plaintext
HTTPS โ TLS Encrypted
โธ How HTTPS Works โ TLS Handshake
Method
Purpose
Idempotent
Safe
Cacheable
GET
Retrieve
โ
โ
โ
POST
Create
โ
โ
โ
PUT
Replace
โ
โ
โ
PATCH
Partial update
โ
โ
โ
DELETE
Remove
โ
โ
โ
HTTPS Guarantees:Confidentiality (AES-256 encryption) ยท Integrity (SHA-256 HMAC, tamper-proof) ยท Authenticity (CA-signed certificate proves server identity) ยท Forward secrecy (ECDHE โ past sessions safe even if key leaked).
2xx โ Success
200
OK
201
Created
202
Accepted (async)
204
No Content
206
Partial Content (streaming)
3xx โ Redirect
301
Moved Permanently
302
Found (temp redirect)
304
Not Modified (cache)
307
Temp Redirect (keep method)
308
Perm Redirect (keep method)
4xx โ Client Error
400
Bad Request
401
Unauthorized
403
Forbidden
404
Not Found
405
Method Not Allowed
408
Request Timeout
409
Conflict
410
Gone (deleted)
413
Payload Too Large
415
Unsupported Media Type
422
Unprocessable Entity
429
Too Many Requests
451
Unavailable For Legal Reasons
5xx โ Server Error
500
Internal Server Error
501
Not Implemented
502
Bad Gateway
503
Service Unavailable
504
Gateway Timeout
HTTP/2: Binary framing, multiplexing, header compression (HPACK), server push. HTTP/3: QUIC-based (UDP), no head-of-line blocking, 0-RTT.
HTTP/1.1 vs 2 vs 3:HTTP/1.1 โ one request per TCP connection (or pipelining, rarely used). HTTP/2 โ multiplexed streams over single TCP, but TCP head-of-line blocking remains (1 lost packet stalls all streams). HTTP/3 (QUIC) โ each stream independent over UDP, lost packet only stalls its own stream. 0-RTT resumption โ reconnect without handshake (TLS 1.3 session tickets). Adopted by Chrome, YouTube, Cloudflare, Meta.
DNS
Translates domains โ IP addresses. The internet's phone book โ hierarchical, cached, eventually consistent.
โธ DNS Resolution โ Full Lookup Path
Record
Purpose
Example
TTL Guidance
A / AAAA
Domain โ IPv4 / IPv6
example.com โ 93.184.216.34
300s (failover) to 86400s (stable)
CNAME
Alias to another domain
www โ example.com
Can't coexist with other records at apex
ALIAS / ANAME
CNAME at zone apex
example.com โ cdn.provider.com
Provider-specific (Route 53, Cloudflare)
MX
Mail routing (priority)
10 mail.example.com
3600s typical
TXT
Verification, SPF, DKIM
v=spf1 include:_spf.google.com
3600s
SRV
Service discovery (port + weight)
_http._tcp.example.com 8080
Used by Consul, K8s
NS
Delegate to nameservers
ns1.example.com
86400s (rarely changes)
CAA
Which CAs can issue certs
0 issue "letsencrypt.org"
Security: restrict cert issuance
TTL strategy:Low TTL (60s) = fast failover, higher query load, good for active-passive DR. High TTL (86400s) = less load, slow propagation, good for stable records. Pre-lower TTL before migrations (drop to 60s 24h before cutover).
Real-world:Route 53 โ GeoDNS + health checks for multi-region failover. Cloudflare โ 1.1.1.1 resolves in ~11ms globally. Anycast โ same IP announced from multiple PoPs (nearest wins). DNSSEC โ cryptographic chain of trust preventing DNS spoofing.
Anti-patterns:High TTL before migration โ users stuck on old IP for hours. CNAME at apex โ breaks MX/NS records. No health checks โ DNS routes to dead servers. Relying on DNS for sub-second failover โ TTL caching prevents it.
Private ranges (RFC 1918):10.0.0.0/8 ยท 172.16.0.0/12 ยท 192.168.0.0/16. Used inside VPCs; not routable on public internet. Plan CIDR carefully โ VPC peering requires non-overlapping ranges.
VPC design: Use /16 per VPC, split into /24 subnets per AZ. Separate public (LB), private (app), and isolated (DB) subnets. Leave room for growth โ you can't resize a VPC CIDR easily.
Anti-patterns:Overlapping CIDRs โ can't peer VPCs. Too small VPC โ run out of IPs when scaling. /16 subnets โ waste addresses, broadcast domain too large. Using 172.17.0.0/16 โ conflicts with Docker default bridge.
Key Ports Cheat Sheet
Standard ports you'll meet in any architecture diagram โ ports < 1024 are privileged (need root)
Port
Service
Protocol
Security Notes
22
SSH
TCP
Key-based auth only, disable password login
53
DNS
UDP/TCP
UDP first, TCP for > 512B or zone transfers
80
HTTP
TCP
Redirect to 443, never serve sensitive data
443
HTTPS
TCP
TLS 1.3, HSTS header, cert pinning for mobile
3306
MySQL
TCP
Private subnet only, never expose publicly
5432
PostgreSQL
TCP
SSL mode = require, restrict to app CIDR
6379
Redis
TCP
No auth by default โ always set requirepass + ACL
9092
Kafka
TCP
9093 for TLS, SASL for auth
9200
Elasticsearch
TCP
Never expose publicly (data exfil risk)
27017
MongoDB
TCP
Enable auth, bind to private IP only
8080/8443
App servers
TCP
Non-privileged, behind LB on 80/443
2379/2380
etcd
TCP
Client/peer ports, mTLS required
Rule of thumb: Run apps on 8080/8443 (non-privileged) and let a load balancer terminate 80/443. Only expose ports that must be public. Database ports should never be reachable from the internet.
Common breaches:Open Redis (6379) โ cryptominer injection. Open Elasticsearch โ data exfiltration. Open MongoDB โ ransomware. Always scan with nmap or cloud security tools.
Firewalls โ Security Groups vs NACLs
Two layers of network filtering in every cloud VPC โ defense in depth
โธ VPC Network Security โ Layered Filtering
Aspect
Security Group
NACL
Scope
Instance / ENI (can reference other SGs)
Entire subnet
State
Stateful โ return traffic auto-allowed
Stateless โ must allow both directions explicitly
Rules
Allow only (implicit deny all)
Allow + Deny (explicit deny possible)
Evaluation
All rules evaluated (most permissive wins)
Numbered, first match wins
Use case
Fine-grained: "app SG can talk to DB SG on 5432"
Coarse: "block this CIDR range entirely"
Limits
~60 rules per SG, 5 SGs per ENI
20 rules per NACL (soft limit)
Best practice:Least-privilege allow-list per port. Default deny everything. Reference SG-to-SG instead of CIDR (survives IP changes). Use NACLs as a coarse "block this CIDR" knife for known-bad ranges.
K8s equivalent:NetworkPolicy โ pod-level firewall (Calico, Cilium). Default deny all ingress/egress, then allow specific label selectors. Service Mesh (Istio) adds L7 policies (allow GET /api but deny POST).
Anti-patterns:0.0.0.0/0 on DB port โ database exposed to internet. Single SG for everything โ no isolation between tiers. Overly permissive egress โ allows data exfiltration. No logging โ can't detect unauthorized access.
Zero Trust Networking
"Never trust, always verify" โ no implicit trust for internal traffic. Every request authenticated, authorized, encrypted.
โธ Zero Trust โ Every Hop Verified
Component
Purpose
Tools
Service Identity
Cryptographic identity per workload
SPIFFE/SPIRE, K8s ServiceAccount, AWS IAM Roles
mTLS
Mutual authentication + encryption
Istio, Linkerd, Consul Connect, Cilium
Policy Engine
Fine-grained authorization (who can call what)
OPA/Rego, Istio AuthorizationPolicy, Cedar
Cert Management
Auto-rotate short-lived certificates
cert-manager, Vault PKI, SPIRE
BeyondCorp Proxy
Identity-aware access for humans
Cloudflare Access, Google IAP, Zscaler
Observability
Audit all access decisions
Envoy access logs, OPA decision logs
Implementation:Istio/Linkerd โ inject sidecar proxy, auto-mTLS between all pods. SPIFFE โ universal workload identity (x509 SVIDs). OPA โ "svc-a can call svc-b GET /api/orders but not DELETE". Short-lived certs (1h) โ compromised cert expires quickly.
Real-world:Google BeyondCorp โ no VPN, all access through identity-aware proxy. Netflix โ mTLS everywhere via custom CA. Airbnb โ SPIFFE for service identity. Cloudflare โ Access replaces VPN for employee access.
Anti-patterns:VPN = trusted โ once inside VPN, full access (flat network). IP-based allow-lists โ IPs change, can be spoofed. Long-lived certs โ compromised cert valid for years. No east-west encryption โ internal traffic sniffable.
DDoS Defense
Layers of mitigation from edge to origin โ absorb volumetric attacks, filter application-layer floods
API gateway rate-limit, token bucket, signed requests
Kong, Apigee, AWS API Gateway
โธ DDoS Attack Types & Signatures
Volumetric (L3/L4)
SYN flood: exhaust connection table
UDP amplification: DNS/NTP/memcached reflection
ICMP flood: saturate bandwidth
Scale: 1-3 Tbps (record attacks)
Defense: absorb at edge, can't filter at app
Application (L7)
HTTP flood: legitimate-looking requests at scale
Slowloris: hold connections open slowly
Cache-busting: unique URLs bypass CDN
Scale: 10K-10M RPS
Defense: WAF, rate-limit, CAPTCHA, behavioral
Defense-in-depth:Edge (Cloudflare/Shield) absorbs volumetric โ WAF filters L7 โ Rate limiting per IP/path โ Bot management challenges suspicious โ App gracefully degrades under remaining load. Each layer reduces attack surface for the next.
Preparation:Always-on protection (not on-demand โ too slow to activate). Runbook for escalation. Load test your own infrastructure to know breaking points. Separate static assets on CDN (attackers can't exhaust origin for static content).
Anti-patterns:No edge protection โ origin directly exposed. On-demand only โ takes 10+ min to activate during attack. Single-region โ no geographic distribution to absorb. Exposing origin IP โ attackers bypass CDN directly.
Real-world:Cloudflare โ mitigated 71M RPS attack (2023). AWS Shield Advanced โ auto-mitigates with DDoS Response Team. Google Cloud Armor โ adaptive protection with ML. GitHub โ survived 1.35 Tbps memcached amplification (2018).