Advanced Filtering with Sets
In this lesson, you will learn how to leverage nftables sets to replace thousands of individual firewall rules with a single, high-performance hash-based lookup.
The Problem with Linear Filtering
When managing a large blocklist—such as preventing traffic from 50,000 known malicious IP addresses—traditional firewall approaches iterate through rules linearly. If you have 50,000 rules, the kernel must check each packet against the list sequentially. This creates an performance penalty, significantly increasing latency and CPU utilization as your blocklist grows.
The Power of Hash Sets
nftables solves this by decoupling the data (the list of IPs) from the policy (the rule that drops those IPs). By using an optimized "set" structure, the kernel performs a constant-time lookup using a hash table. Instead of evaluating 50,000 rules, the kernel performs one single hash-based check to see if an IP exists in your defined set.
Here is how you define and use an anonymous set for high-performance filtering:
# Define a table and a set for malicious IPs
nft add table inet filter
nft add set inet filter blocklist { type ipv4_addr\; flags interval\; }
# Add IPs to the set
nft add element inet filter blocklist { 192.0.2.1, 203.0.113.5 }
# Create a single rule that references the set
nft add rule inet filter input ip saddr @blocklist drop
Why this matters
Beyond simple speed, this approach makes your configuration manageable. You can update the blocklist set dynamically at runtime—adding or removing IPs—without ever reloading your firewall ruleset or disrupting traffic flow. Because the set is stored in the kernel memory as a structured hash map, the lookup time remains nearly identical whether you have ten entries or ten million.
This technique is the gold standard for high-throughput edge routers and security gateways, ensuring that your security posture does not become a bottleneck for your network stack.
Key Takeaway: By offloading large lists into kernel-resident hash sets, you transition from linear rule scanning to near-instantaneous lookups, allowing your firewall to scale indefinitely without performance degradation.