Design Country IP Blocking
Understanding the Problem
🌍 What is country IP blocking? Many services have to refuse traffic from certain places or sources: sanctions law can forbid serving users in some countries, licensing can limit content to some regions, and security teams ban IPs that attack the service. An IP blocking system checks every incoming request's source address against a map of IP ranges to countries and against a denylist of banned addresses, and rejects the request before it reaches the application.
This question comes in a few versions: "block requests from banned countries," "given an external service that tells you whether an IP is banned, design the lookup," "build an API that maps an IP to a country using a table of IP ranges," and "design a distributed denylist." They're one system: an in-memory lookup at the edge, fed by data that has to be distributed and kept fresh. We'll cover each version.
Functional Requirements
Core Requirements
- Requests from blocked countries should be rejected at the edge, with block rules configurable per product.
- Requests from banned IPs and IP ranges (CIDRs) should be rejected. Security teams and automated systems should be able to add and remove entries.
- Internal services should be able to call an IP lookup API: given an IP, return its country, region and network owner, and whether it's blocked and why.
- The system should use an external IP-reputation service to find banned IPs we don't know about yet.
Below the line (out of scope)
- DDoS mitigation at the network layer (that's scrubbing centers and anycast, a different problem).
- Per-user rate limiting (see /system-design/rate-limiter).
- Building the geolocation data itself. We license a commercial geo-IP database.
Non-Functional Requirements
Ask your interviewer for numbers. Ours: the edge handles 2 million requests per second across about 2,000 edge servers in 50 regions.
Core Requirements
- The check must add almost no latency: well under 1 ms at p99, since it runs on every request.
- Denylist changes should reach every edge server within 10 seconds, worldwide.
- The check must be highly available. An outage of the blocking system's control plane must not take down traffic or silently stop blocking.
- Lookups must be correct for both IPv4 and IPv6.
- The external reputation service (slow and rate-limited) must never be on the critical path of a request.
Below the line (out of scope)
- Perfect geolocation. Geo-IP data is right at the country level for the vast majority of addresses, not all of them.
- Stopping determined users behind VPNs (we'll discuss what helps).
Back-of-the-envelope on the data. A commercial geo-IP database maps on the order of several million IP ranges (IPv4 and IPv6) to locations. Our denylist might hold 10 million individual IPs and 100,000 CIDR ranges. Stored compactly, an IPv4 range is a start, an end and a country code, about 10 bytes, so 5M ranges is about 50 MB. A hash set of 10M IPv4 addresses is somewhere around 100-200 MB depending on the implementation. All of it fits in memory on every edge server.
On the request side: 2M requests per second over 2,000 servers is about 1,000 lookups per second per server. A network call per lookup would add milliseconds and a dependency; an in-memory lookup takes well under a microsecond.
Lead with this: the lookup must be local and in memory on every edge server. That turns the problem from "build a fast lookup service" into "distribute a few hundred MB of data to 2,000 machines and keep it fresh," which is where the real design work is.