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

Firewalls and VPN

Rules that block unwanted traffic, and a private tunnel across the public internet.

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

01Quick Summary

A firewall is a gatekeeper that allows or blocks network traffic using rules. A VPN (Virtual Private Network) is an encrypted tunnel that carries your traffic safely across a network you do not trust.

  • A firewall looks at each packet or connection (who sent it, where it goes, which port and protocol) and says allow or deny.
  • Rules are checked in order, and a good firewall ends with default deny: if nothing says yes, the answer is no.
  • A VPN wraps your traffic in encryption and sends it to a VPN server, so people on the path in between cannot read it.
  • Firewalls control who may connect. VPNs protect the path. They are used together.

02Concept: What is it?

Firewalls: rules at the gate

A firewall sits between networks (the internet and your servers, or one subnet and another) and checks traffic against a list of rules. A rule usually names a source, a destination, a port, a protocol (TCP, UDP) and an action (allow or deny).

A firewall in the path
🌍Internetall kinds of traffic
→↓packets
🧱Firewallrules, top to bottom
→↓allowed only
πŸ–₯️Serversports 443 only
Only traffic that matches an allow rule gets through. Everything else is dropped.

Stateless and stateful

A stateless firewall judges every packet on its own, so you must write rules for replies too. A stateful firewall remembers connections. If it allowed an outgoing request, it automatically allows the matching reply. Most modern firewalls and cloud security groups are stateful.

VPN: a private tunnel over a public road

A VPN creates an encrypted tunnel between two points. Packets are encrypted, placed inside new packets, and sent over the public internet. At the other end they are decrypted and delivered. There are two main uses: remote access (a person's laptop joins the company network) and site-to-site (two offices or clouds are joined).

A VPN tunnel
πŸ’»Laptopon cafΓ© Wi-Fi
→↓encrypted tunnel
πŸ”VPN serverdecrypts
→↓private
🏒Company network
Anyone on the cafΓ© network sees only scrambled data going to the VPN server.
🧠Default deny is the safe default
List what is allowed and block the rest. It is far easier to be safe by naming the few things you need than by trying to name everything bad.

03Real-Life Example

🍡The Office Reception and the Armoured Car
An office has a reception desk with a visitor list. The guard checks each person against the list: "Delivery driver, floor 1 only: yes. Stranger asking for the server room: no." If the guard reads the list top to bottom and stops at the first match, the order of the list matters. That guard is the firewall. Now imagine you must carry cash between two offices through a busy city. You use an armoured car with a locked back door. Everyone can see the car, but nobody can see what is inside. That is the VPN.
Office
Network
The guard at reception
The firewall
The visitor list
Firewall rules
Checking the list top to bottom
First matching rule wins
No one gets in unless named
Default deny
Remembering who went in, so they can walk out
Stateful connection tracking
The armoured car
The encrypted VPN tunnel
The depot where the car unloads
The VPN server
Order matters
1️⃣Allow office β†’ SSH
→↓
2️⃣Deny all β†’ SSH
→↓
🏁Default deny
Specific allow first Broad deny after

04Why Do We Need It?

πŸšͺShrink the attack surface
Open only the ports you need. A closed port cannot be attacked.
🧱Separate zones
Stop traffic from the public side reaching the database side, even if a server is hacked.
πŸ”Private paths
A VPN keeps traffic private on Wi-Fi hotels, cafΓ©s and the open internet.
πŸ”—Join networks
Site-to-site VPNs connect offices and clouds as if they were one network.
πŸ“œAudit and compliance
Rules and logs show who can reach what, which auditors often require.
🧭Remote work
Staff can reach internal tools from home without exposing those tools to the world.
A firewall decides who may come in. A VPN protects the road they travel on.

05Where Do We Use It?

☁️Cloud security groups
Per-server allow rules, usually stateful. They are the first line of defence in a cloud network.
🧾Network ACLs
Subnet-level rules, often stateless, with numbered order. Used as a second layer.
🌐Web Application Firewall
A WAF reads HTTP requests and blocks things like SQL injection and bad bots.
πŸ’»Host firewalls
Each server or laptop runs its own firewall (for example iptables, nftables or the OS firewall).
🏒Corporate VPNs
Employees connect to the company network from anywhere.
πŸ”—Site-to-site links
Two offices, or an office and a cloud network, joined over the internet.

06Types / Parts / How It Works

Kinds of firewall

KindLooks atGood forLimit
Packet filter (stateless)Addresses, ports and protocol of each packet aloneFast, simple rulesDoes not know about connections, so replies need their own rules
Stateful firewallThe same, plus the state of the connectionMost servers and cloud networksCannot read the content of web requests
Application firewall / WAFHTTP requests: URLs, headers, bodiesBlocking web attacksNeeds tuning, and can block real users by mistake
Next-generation firewallApplications, users and threatsCompany edgesCostly and complex
Host firewallTraffic to and from one machineDefence in depthMust be managed on every machine

Security groups and network ACLs (cloud example)

Security groupNetwork ACL
Applies toA server or network interfaceA whole subnet
StateStateful: replies are allowed automaticallyStateless: allow the return traffic too
Rule typesAllow rules (everything else is denied)Allow and deny rules, checked in number order
Typical useMain per-server controlExtra guard rail at the subnet edge
A rule set in plain language
1  ALLOW  any            -> web servers   TCP 443
2  ALLOW  office network -> servers       TCP 22   (SSH)
3  DENY   any            -> servers       TCP 22
4  ALLOW  app subnet     -> database      TCP 5432
5  DENY   any            -> database      TCP 5432
-  DEFAULT: DENY everything else

Kinds of VPN

Remote access vs site-to-site
Remote access
πŸ’»One laptop
↓tunnel
πŸ”VPN gateway
Site-to-site
🏒Office A
↔tunnel
☁️Cloud network
Same idea, different ends of the tunnel.
Tunnel modeWhat goes through the tunnelTrade-off
Full tunnelAll traffic from the deviceEverything is protected on the local network, but all of it passes through the VPN, and the VPN operator can see where you go
Split tunnelOnly traffic for the company networkFaster and lighter, but other traffic is not protected by the VPN
VisualizerFirewall rule order and a VPN tunnelTest each packet against the rules. Then move rule C above rule B and test the office SSH packet again: order changes the outcome. Try turning stateful off and testing the reply, and turn default allow on. Finally compare no VPN, split tunnel and full tunnel.
Rules are checked from top to bottom. The first match wins. Use the arrows to change the order.
1ALLOWAllow anyone to port 443 (HTTPS)
2ALLOWAllow the office network to port 22 (SSH)
3DENYDeny everyone else to port 22 (SSH)
4ALLOWAllow the app subnet to port 5432 (database)
5DENYDeny everyone else to port 5432 (database)
Last line, the default policy: DENY everything else
Test a packet
Admin at the office β†’ SSH :22: ALLOWED because rule B matched first: Allow the office network to port 22 (SSH). That is the right outcome.
Wanted traffic allowed
4 / 4
should be all
Unwanted traffic blocked
3 / 3
should be all
VPN on your laptop, on cafΓ© Wi-Fi
Company intranet (10.20.0.5)inside the VPN tunnel to the company network
Public websitestraight out, not through the tunnel
Cloud app (SaaS)straight out, not through the tunnel
Only company traffic goes through the encrypted tunnel. Everything else leaves directly, so the cafΓ© network still sees where your other traffic goes.

A teaching model with five rules and seven packets. Real firewalls match on many more fields (destination, protocol, direction) and cloud security groups are usually allow-only and stateful. A VPN does not make you anonymous and does not protect you from a site you log in to.

07How to Use It / Steps / Implementation

Setting up firewall rules well

  1. 1
    Start from deny
    Block everything by default, then add the few allow rules you need.
  2. 2
    Allow by purpose
    Name the source, the destination, the port and the protocol. Avoid rules like allow all from anywhere.
  3. 3
    Use groups and ranges
    Allow a security group or a CIDR range, such as the app subnet, instead of single IP addresses that change.
  4. 4
    Mind the order
    In ordered rule lists put the specific rules first and the broad ones later. The first match wins.
  5. 5
    Close admin ports
    Keep SSH and database ports off the public internet. Reach them through a VPN or a bastion host, from known ranges only.
  6. 6
    Layer your defences
    Combine cloud rules, host firewalls and a WAF in front of web apps. One layer should not be the only barrier.
  7. 7
    Log and review
    Log denied traffic, review rules on a schedule, and remove old ones.

Choosing VPN settings

  • Prefer modern, well-reviewed protocols (for example WireGuard or IPsec with strong settings) and keep the software updated.
  • Use strong authentication: certificates or single sign-on plus multi-factor, not only a shared password.
  • Use split tunnel when you only need company resources, and full tunnel when you need to protect everything on an untrusted network.
  • Plan non-overlapping address ranges, or the two networks cannot be joined cleanly.
πŸ’‘Test your rules like code
After a change, test from the outside that the blocked things are really blocked, and from the inside that the allowed things still work. A rule that was never tested is a guess.
⚠️A VPN is not a magic shield
It protects the path between you and the VPN server. It does not make you anonymous, it does not stop malware, and it does not protect you after you log in to a website.

08Key Terms to Know

TermMeaning in easy words
FirewallA system that allows or blocks network traffic using rules
RuleA line that says allow or deny for traffic matching a source, destination, port and protocol
Default denyThe final policy: anything not explicitly allowed is blocked
Stateless filterA filter that judges each packet alone, with no memory of connections
Stateful firewallA firewall that tracks connections and automatically allows replies
Security groupA cloud firewall attached to a server, usually stateful and allow-only
Network ACLA subnet-level rule list, often stateless and checked in numbered order
WAFWeb Application Firewall: filters HTTP requests for web attacks
VPNVirtual Private Network: an encrypted tunnel across an untrusted network
Split / full tunnelSending only some traffic, or all traffic, through the VPN
Bastion hostA hardened server used as the one controlled entry point to a private network

09Pros, Cons and Trade-offs

βœ… Pros
  • Blocks most unwanted traffic with simple rules
  • Default deny keeps mistakes safe
  • Stateful tracking keeps rules short
  • VPNs protect traffic on untrusted networks
  • Both can be layered for defence in depth
⚠️ Cons
  • Rules grow messy and old rules are rarely removed
  • A wrong order or a too-wide allow quietly opens a hole
  • A firewall cannot see inside encrypted traffic without extra setup
  • VPNs add latency and a single choke point
  • VPN access can become a big prize if credentials are weak

Trade-offs

ChoiceWe gainWe lose
Default denySafe by defaultYou must add a rule for every real need
Default allowEverything works at firstUnknown traffic gets in
Stateful rulesShort, easy rulesMore memory and connection tracking
Stateless rulesVery fast and simpleYou must write rules for replies and can make mistakes
Full-tunnel VPNEverything is protected on the local networkMore load, slower pages, VPN operator sees your traffic
Split-tunnel VPNFast, only company traffic is carriedThe rest is not protected by the VPN
Zero trust style accessEach request is checked, not just the network edgeMore identity and policy work

10Common Problems and Mistakes

Common mistakes

  1. Allow all from anywhere (0.0.0.0/0). Often added to "make it work" and then forgotten, especially on SSH and database ports.
  2. Wrong rule order. A broad deny placed above a specific allow blocks the traffic you wanted, and the reverse lets bad traffic in.
  3. Default allow. Anything you forgot to think about is open.
  4. Forgetting return traffic. With stateless rules, replies are blocked, and everything seems broken.
  5. Treating the firewall as the only defence. Once inside, an attacker moves freely if there are no other layers.
  6. Old rules left in place. Nobody knows why they exist, so nobody dares delete them.
  7. Overlapping address ranges across a VPN. The two networks cannot be joined without renumbering.

Things to watch

πŸ•³οΈEncrypted traffic
A firewall cannot read HTTPS content. Use a WAF at the point where TLS ends if you need it.
🚧VPN as a bottleneck
All remote users pass through a few gateways. Plan capacity and a backup.
πŸ”‘Stolen VPN credentials
Use multi-factor authentication and short-lived certificates.
πŸ§ͺRule drift
Rules changed by hand in production no longer match the code. Manage them as code.

11Real-World Examples

☁️Three-tier cloud app
The load balancer accepts 443 from anywhere, the app accepts traffic only from the load balancer, and the database accepts traffic only from the app.
🏒Company remote work
Staff connect over a VPN with multi-factor sign-in to reach internal tools that are not on the public internet.
πŸ”—Hybrid cloud
A site-to-site VPN or a private link joins the office network to the cloud network.
πŸ›‘οΈWeb shops
A WAF in front of the site filters common attacks and bad bots before they reach the app.
🧰Bastion hosts
Admins reach private servers by first signing in to one hardened entry server.
🏨Public Wi-Fi
Travellers use a VPN so other people on the same network cannot see where their traffic goes.
πŸ“Note
Rule limits, default behaviours and product names differ by cloud and vendor. Check the documentation for the system you use.
Mini case: layered rules
🌍Internet
→↓443
βš–οΈLoad balancerallow 443 from anywhere
→↓8080
πŸ–₯️Appallow only from the LB
→↓5432
πŸ—„οΈDatabaseallow only from the app
Each tier only accepts traffic from the tier in front of it.

12Related Topics to Read Next

Subnets, CIDR and NAT→

Firewall rules often name a CIDR range, and NAT hides private hosts.

Ports and Sockets→

Rules allow or block traffic by port number.

HTTP / HTTPS→

A WAF works on HTTP, and TLS hides it from basic firewalls.

Proxy vs Reverse Proxy→

A reverse proxy is another gatekeeper in front of servers.

Rate Limiting→

Another way to protect a service from too much or bad traffic.

Authentication vs Authorization→

VPN access still needs a login and the right permissions.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”Firewall: ordered allow and deny rules on source, destination, port and protocol.
  • βœ”Default: deny everything not explicitly allowed.
  • βœ”Order: the first matching rule wins. Specific rules before broad ones.
  • βœ”Stateful: remembers connections, so replies are allowed automatically.
  • βœ”Cloud: security groups (per server, allow-only) plus network ACLs (per subnet).
  • βœ”WAF: filters HTTP attacks at the application layer.
  • βœ”VPN: encrypted tunnel. Remote access or site-to-site. Split or full tunnel.
  • βœ”One line: β€œThe firewall decides who may enter, the VPN protects the road.”
← Previous
Subnets, CIDR and NAT
Next β†’
Ports and Sockets