What Is a Firewall - the Mechanism, the Types, and When It Won't Save You

A firewall is a policy engine that sits on a traffic path and decides, per packet or per connection, whether to allow it through or drop it. That is the whole idea: what is a firewall if not a configurable choke point you deliberately put in front of something you don't fully trust.

It exists because reachability is the precondition for most attacks. If an attacker cannot open a connection to your database port, a vulnerability in that database is a much smaller problem. A firewall is how you stop handing out that reachability by default.

The interesting part is not the allow/drop decision. It's how much context the firewall keeps while deciding, because that single variable explains every type of firewall, every performance tradeoff, and every failure mode in this article. Detailed view of fiber optic cables connected to a server rack, showcasing modern technology.

ConceptFirewall (packet filter / stateful firewall / NGFW / WAF)
Where it sitsNetwork and application boundary - sits on the path between two trust zones, from kernel netfilter up to a reverse proxy
In one sentenceA firewall enforces a rule set on traffic crossing a boundary, so that only the connections you explicitly intended are even possible.

The 60-Second Answer

What it does: evaluates traffic crossing a boundary against an ordered rule set and allows, drops, or rejects it.

The problem it solves: it removes default reachability, so a flaw in a service behind it is only exploitable by whoever you let reach it.

The cost it adds: every rule is now a thing that can be wrong. You get an ordered, stateful config in the request path that nobody owns after the person who wrote it leaves, plus a new class of outage where the correct answer is "the traffic was silently dropped".

The judgement: a firewall limits who can reach what. It does not make the thing they reach safe.

How It Actually Works

1. The rule set is ordered, and the first match wins

Evaluation order is the real config

A firewall holds a list of rules, each matching on traffic properties - source address, destination address, protocol, destination port - and each carrying an action. Traffic is tested top to bottom and the first matching rule decides. At the end of the list sits a default policy, which in any sane configuration is deny.

# conceptual shape, not a specific tool's syntax
allow  tcp  any        -> web:443
allow  tcp  app_subnet  -> db:5432
deny   all   any        -> any        # default policy

The design is ordered rather than set-based because you need exceptions to be expressible: a narrow allow above a broad deny, or a narrow deny above a broad allow.

⚠️ In production — Ordering is where real outages come from. A broad allow accidentally placed above a specific deny makes the deny dead code, and nothing warns you - the config loads fine and the test traffic passes. Review rule sets by reading them as a sequence, not as a list of individual rules, and treat "where in the order does this go" as part of every change.


2. A packet filter decides on each packet with no memory

Cheap, stateless, and awkward for replies

The earliest and simplest type of firewall looks only at the headers of the packet in front of it. It is fast and uses a fixed, tiny amount of memory per packet, because it keeps nothing between packets.

That statelessness creates an immediate problem: if you allow outbound traffic to a web server, the reply packets come back from a high, unpredictable source port. With no memory of the outbound request, the only way to let replies in is a broad rule permitting inbound traffic to the whole ephemeral port range.

That rule is the thing you were trying to avoid writing.

⚠️ In production — Stateless filters are still worth having, but mostly in front of the stateful one, for traffic you want discarded without spending any state on it - spoofed source addresses, traffic to ports you don't run anything on. If you use a stateless filter as your only firewall, the ephemeral-port hole is almost always wider than the operator believes.


3. A stateful firewall tracks connections, so replies are implicit

One table entry per flow

A stateful firewall keeps a connection tracking table. When it allows a new outbound connection, it records the flow, and subsequent packets belonging to that flow are matched against the table rather than re-evaluated against the rule set.

This is why modern rule sets read so cleanly. You write one rule for the direction you initiate in, and the return path is handled by the state machine. It also means the firewall can reason about things a single packet cannot express, such as a packet that claims to belong to an established connection that was never established.

This is the default behaviour of essentially every firewall you will meet today - host firewalls, cloud security groups, network appliances.

⚠️ In production — The connection table is finite, and that makes it a resource. Under a flood of half-open connections, or simply far more concurrent flows than the box was sized for, the table fills and new legitimate connections get dropped while existing ones are fine. The symptom is maddening: the service is up, old clients work, new clients time out. Also watch idle timeouts - a long-lived connection that goes quiet can have its state evicted, after which both ends think they have a connection and neither gets an error until they try to use it.


4. Application-layer firewalls terminate and inspect the payload

Proxying, not forwarding

To make a decision based on anything above the transport header - an HTTP path, a hostname, a SQL-shaped string in a parameter - the firewall has to reassemble the byte stream and parse it as a protocol. At that point it is a proxy: it terminates the client's connection, inspects, and opens its own connection onward.

This is the layer where a WAF (web application firewall) lives, and it's a genuinely different thing from a network firewall. A network firewall answers "may this host talk to that port". A WAF answers "is this specific request acceptable to this application".

Because it terminates TLS to see inside, it becomes part of your certificate and key management story as well as your security story.

⚠️ In production — Payload inspection means the firewall must parse attacker-controlled input with a parser that may not agree with your application's parser. Where the two disagree, you get both false positives that block real users and bypasses that slip past the rule. Every WAF deployment needs a monitoring-only period with real traffic before you turn blocking on, and the tuning never fully ends.


5. Identity-based rules replace addresses with labels

Rules that survive autoscaling

In an environment where instances come and go, IP-address rules rot. Cloud security groups, Kubernetes network policies, and service meshes all solve this the same way: rules reference a label or group identity, and the control plane expands that into the concrete addresses currently matching, then pushes it down to the enforcement points.

# the rule names a role, not an address
from: app-tier
to:   db-tier
port: 5432

The mechanism underneath is still packet matching. What changed is who maintains the address list - a controller instead of a human.

⚠️ In production — You have now made the control plane part of your availability story. Label changes propagate asynchronously, so there is a window where a newly started instance is not yet in anyone's allow list and its traffic is dropped. Startup failures that disappear on retry are frequently this. Also: a group identity is only as trustworthy as the thing that assigns it - if anything can tag itself into the privileged group, the rule is decoration.


6. Where you place it decides what it can actually protect

Perimeter, segment, host

A firewall only sees traffic that crosses it. Put one at the internet edge and it controls what the outside world can reach, but says nothing about one internal service reaching another. Put rules at each segment boundary and you contain lateral movement between tiers. Put them on each host and you control traffic at its finest granularity, including between workloads on the same subnet.

The "zero trust" framing is mostly an argument about this placement question: that the perimeter firewall is necessary but a single hard shell around a flat interior means one compromised host gets to talk to everything.

These layers are complementary, not competing. Edge rules are coarse and cheap to maintain; host rules are precise and expensive.

⚠️ In production — The failure mode here is a perimeter that looks strong and an interior that is flat. Audits pass because the edge rule set is tidy, and then a compromised build agent reaches the production database because nothing between them ever said no. If you only add one layer beyond the edge, make it the boundary in front of your datastores.


7. Egress filtering constrains what a compromised host can do next

The direction most people leave open

Nearly everyone writes careful inbound rules and leaves outbound wide open. But the steps after an initial compromise - pulling down a second-stage payload, reaching a command-and-control endpoint, copying data out - are all outbound connections.

Restricting egress to the destinations a workload genuinely needs turns those steps into failures. For a service whose dependencies are a known list of internal services and a payment API, this is a short, stable rule set and real containment.

Egress rules also surface dependencies you didn't know you had, which is uncomfortable and useful in roughly equal measure.

⚠️ In production — Egress filtering is where the correlation between "tight rules" and "3am pages" is strongest. Package registries, OS update servers, and telemetry endpoints sit behind CDNs with address sets that change without telling you, so address-based egress rules break on someone else's schedule. Filter egress by hostname where you can, start in log-only mode to discover the real dependency list, and expect to maintain it forever.


8. The rule set is a living artifact, or it is a liability

Rules accumulate; nothing removes them

Firewall configs grow monotonically. Someone adds a temporary allow for a migration, the migration finishes, the rule stays. Over a few years the set contains rules nobody can explain, referencing addresses that no longer exist, and the one rule that is actually load-bearing is indistinguishable from the forty that are not.

The fix is unglamorous: rules live in version control, changes go through review, each rule carries a comment saying who needs it and why, and the firewall's logs of allowed traffic tell you which rules are still carrying real flows.

A rule with no traffic against it for a year is either dead or it's your last line of defence, and the comment is the only thing that tells you which.

⚠️ In production — Nobody deletes a firewall rule voluntarily, because removing one has an asymmetric payoff: no visible upside, and a possible outage you'll be blamed for. Build the audit into a recurring, scheduled task with named owners, or it will not happen. And log your denies - a firewall with no deny logging gives you no way to distinguish "nothing is attacking us" from "we are not looking".

Two professionals discussing project plans at whiteboard in office setting. ## Where You Meet It
  • A three-tier web application in a cloud VPC. The load balancer accepts 443 from the internet; the app tier's security group accepts traffic only from the load balancer's group; the database's group accepts 5432 only from the app tier's group and from nothing else. The firewall here is doing one specific job: making the database unreachable from the internet even if someone misconfigures its authentication.
  • A Kubernetes cluster where pod-to-pod traffic is unrestricted by default. Adding network policies gives each namespace a default-deny for ingress plus explicit allows for the services that legitimately call it. The mechanism is identity-based rules compiled into per-node packet filters, and the benefit is that a compromised pod in one namespace cannot enumerate services in another.
  • A public API behind a reverse proxy with a WAF in front of the application. The network firewall has already decided that only 443 from anywhere is permitted; the WAF is making a different decision - rate limits per client, blocking request shapes that match known injection patterns, rejecting oversized bodies before they reach the application's parser. Two firewalls, two layers, two entirely different rule sets.
  • An internal batch-processing fleet with no inbound exposure at all, where the only firewall rules that matter are egress. The workloads may reach an internal object store, an internal queue, and the OS package mirror. Nothing else. When one of those hosts was compromised through a dependency, the payload's download step failed, and the deny log was the detection signal.
  • A corporate network where developer laptops run host firewalls with a default-deny inbound. There is no perimeter to rely on because the laptops are on cafe wifi half the week. The firewall has moved onto the endpoint because that is the only boundary that still travels with the machine.

When You Actually Need It

  • You run a service that listens on a port and some set of clients should not be able to reach it. This is the base case and it is almost always true.
  • You have a datastore, admin interface, or internal API whose authentication you would not want to be the only thing standing between it and the internet.
  • Your interior is flat - any host can open a connection to any other host - and you can name at least one blast-radius scenario you'd like to contain. Start with a boundary in front of the datastores.
  • You can enumerate a workload's outbound dependencies. That enumerability is the signal that egress filtering will be cheap to maintain and will genuinely constrain post-compromise activity.
  • Instances are ephemeral and your existing address-based rules are already drifting out of date. That is the trigger for moving to identity- or label-based rules.
  • A compliance or customer-security review requires demonstrable network segmentation. The rule set is the artifact, so build it to be readable.

When You Don't

  • Don't deploy a firewall appliance in front of a cloud workload that already has security groups. You will be maintaining two rule sets that must agree, and the cases where they silently disagree are exactly the cases that cause a 3am page. Use the platform's native enforcement until you have a requirement it cannot express.
  • A firewall is not a substitute for authentication and authorisation on the service itself. If your reasoning is "it's only reachable internally so it doesn't need auth", the firewall has become the single point of failure for your entire security model. Put real auth on the service; keep the firewall as the second layer.
  • Don't add a WAF to buy time on a known vulnerability in your own code, except as an explicitly temporary measure with a deadline. The virtual-patch rule is a guess about the exploit's shape, it will be bypassed by a variant, and shipping it reduces the urgency of the real fix. Patch the code; use the WAF rule as a few-day bridge, in writing.
  • Below a certain scale, segmentation is not worth the operational cost. Concretely: a single application on a handful of machines with one datastore, run by a team of three or four people who all understand the whole topology. A default-deny inbound host firewall plus a tight rule for the datastore is the correct and complete answer. Per-service microsegmentation here costs you real engineering time and buys containment for lateral movement you don't have room for.
  • Don't start egress filtering in blocking mode on a system whose dependencies you cannot list. You will take an outage within days when a CDN rotates addresses. Run in log-only mode first, build the dependency list from what you observe, then enforce.
  • Don't use a firewall to solve application-level abuse like credential stuffing or scraping. IP-based blocking against a distributed, rotating source set means you are chasing addresses while blocking legitimate users behind shared NATs. The answer is at the application layer: per-account rate limits, proof of work, better auth signals.
  • Don't deep-inspect traffic you already control both ends of just because the appliance can. Terminating internal TLS to inspect it means adding a decryption point, keys to manage, and latency, for visibility into traffic you could have logged at the application. Spend that effort on the untrusted edge instead. Detailed view of network cables plugged into a server rack in a data center.

What People Get Wrong

"We have a firewall, so our internal services are secure."

A firewall controls reachability, not what happens once something is reachable. It does nothing against a vulnerability in a service you have deliberately exposed, a compromised credential used from an allowed source, or a malicious request from an allowed client. The only attack it reliably prevents is one that requires a connection you never permitted.

"A next-generation firewall replaces the stack - it does application awareness, so we don't need a WAF or host rules."

The capability overlap is real but the placement is not. An appliance at the edge sees north-south traffic and nothing between two internal services on the same subnet. Feature breadth in one box does not create visibility into paths that don't cross it.

"Firewall rules are a security concern, so the security team owns them."

The rule set encodes your service dependency graph, which means the people who know whether a rule is still needed are the teams that own the services. Centralised ownership reliably produces a set nobody will delete from, which is how configs reach a thousand stale rules. Review stays central; authorship should sit with the service owner.

"Blocking all outbound traffic by default would break everything, so egress filtering isn't practical for us."

It is impractical as a flag flip and quite practical as a staged rollout. Log-only mode on one workload for a week produces the actual dependency list, which is usually far shorter than anyone guessed. The difficulty is the discovery, not the enforcement.

Why It Was Built This Way

The original constraint was that a firewall sits in the data path, so whatever it does, it does to every packet. That single fact produced the entire design. Matching on fixed-offset header fields is something you can do at line rate with almost no memory, which is why the first firewalls were stateless packet filters - not because nobody wanted application awareness, but because reassembling and parsing streams at wire speed was not affordable.

Everything since has been buying context at the price of resources. Stateful tracking bought you implicit return paths and the ability to reject packets that lie about their connection, and the price was a per-flow table - memory proportional to concurrent connections, and therefore a new way to be exhausted. Application-layer inspection bought you decisions based on what the traffic means, and the price was becoming a proxy: terminating connections, holding keys, running an attacker-facing parser, and adding latency that scales with payload size. Identity-based rules bought you configs that survive autoscaling, and the price was a control plane in the availability path with propagation delay you now have to reason about.

What got traded away at every step is determinism. A stateless filter's decision is a pure function of the packet in front of it: you can read the rules and know the answer. A stateful, label-driven, payload-inspecting firewall's decision depends on table contents, control-plane freshness, and a parser's interpretation of attacker-controlled bytes. That is why debugging a modern firewall is less like reading a config and more like investigating a distributed system, and why the most useful instrumentation you can add is not more rules but logs of what was allowed and what was denied, and why. A firewall whose decisions you cannot explain after the fact has traded away the one property that made the simple version trustworthy.

Related Briefs

What to Read Next

  • what is a reverse proxy
  • network segmentation vs zero trust architecture
  • what is a web application firewall (WAF)
  • cloud security groups vs network ACLs

A firewall is the cheapest way to make a large class of attacks impossible rather than merely difficult, and that is worth a lot. But it buys you a constraint on reachability and nothing else - it does not make the thing behind it safe, and every rule you add is a thing that can be silently wrong. Put one in front of anything that listens, keep the rule set small enough that someone can read it, log both the allows and the denies, and spend your remaining effort on the service's own authentication rather than on a thicker boundary.

댓글

이 블로그의 인기 게시물

What Is a Message Queue - and When You Actually Don't Need One

What Is a Bloom Filter - and When You Actually Don't Need One

When to Use GraphQL Over REST API - and When REST Is Still the Right Call