Blocking Government-Listed IPs at the Edge of a Global Service
System DesignModel answerLast updated
Design a fleet-wide system that rejects connections from IP addresses a regulator flags as prohibited, using only the regulator's per-address lookup API as the source of truth.
A national regulator requires your online service to turn away traffic from any IP address on its prohibited list. The regulator exposes an API: you submit an IP address, and it replies whether that address is prohibited. You cannot modify this API, and it remains the authoritative record.
Design the system that enforces this rule across your entire fleet of servers.
Treat this as a conversation. Interviewers in this style of round usually look at two things beyond the architecture itself:
- Whether you verify your assumptions before building on them (IP versions, available infrastructure, API behavior) instead of silently inventing numbers.
- Whether you lead the discussion, starting with a simple design and refining it in steps, each step motivated by a concrete weakness.
You are given very little up front. Traffic volume, API limits, fleet size, and the required freshness of ban data are all things you must ask about or state as labeled assumptions.
Part 1: Scope and a baseline. Settle the scope and requirements, then present the simplest compliant design and explain where it fails.
Part 2: Enforcement at scale. Rework the design so each connection is checked quickly at your edge without calling the regulator on every attempt. Explain where the check runs, what data structure stores the bans, and how ban data reaches every enforcement point.
Part 3: Freshness, failure, and evidence. Describe how newly added and removed bans propagate within the freshness target, what happens to connections that are already open when an address becomes banned, how the system behaves when the regulator's API or your own sync pipeline is down, and how you can prove compliance afterward.
Questions worth raising early: Is IPv6 in scope? Does the API support only single lookups, or also bulk export or change feeds? What are its rate limits, latency, and availability? Are bans single addresses or CIDR ranges? How quickly must changes take effect? Which traffic and protocols are covered? If the API is down and an address is unknown, should you block or allow? Will you need records of refused connections?
Hints
Hint 1
Start by listing what you must ask: IP versions, single addresses versus CIDR ranges, whether the API offers bulk or delta export, rate limits, and the freshness target. Label every number you then use as asked or assumed.
Hint 2
A per-connection call to the regulator couples your latency and availability to theirs and will hit rate limits. Move the decision to a local, in-memory structure on each edge node that supports longest-prefix matching for both IPv4 and IPv6, and fill it asynchronously.
Hint 3
Treat fail-open versus fail-closed as an explicit policy to be approved by the owner of the compliance obligation, and don't forget three things: connections already established, an audit trail of refusals, and alerting on data staleness.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Scope first (ask, don't assume)
I would open with questions and record the answers as requirements:
- IP versions: IPv4 only, or IPv4 and IPv6? I'll design for both. A prefix-based structure handles both at little extra cost.
- Granularity: single addresses or CIDR ranges? I'll support prefixes, since single addresses are just /32 and /128.
- API capabilities: single lookup only, or also a full snapshot and/or a change feed? Rate limits, latency, SLA?
- Freshness: how soon must an added ban be enforced, and how soon must a lifted ban stop being enforced? Often these differ: blocking late is a compliance breach, while unblocking late is a customer-impact issue.
- Coverage: all inbound traffic or only certain products/protocols (HTTPS, TCP, UDP, WebSocket)?
- Unknown state: if the API is unreachable and an address has never been checked, block or allow?
- Evidence: do we need audit records of refusals?
Non-functional requirements I would then state as assumptions, clearly marked: a freshness target (say, 15 minutes for additions), added decision latency under a millisecond, and no dependency of the data path on the regulator's availability.
2. Baseline and where it breaks
Baseline: at accept time, each server calls the regulator's API with the client IP and drops the connection if banned.
This is compliant but fragile:
- Latency: every new connection pays a network round trip to a third party.
- Rate limits: peak connection rates will exceed any reasonable quota.
- Availability coupling: if the API is slow or down, either we reject everyone (outage) or allow everyone (violation).
- Cost and abuse: an attacker can spray connections from many addresses to exhaust our quota.
A per-IP cache with TTL helps with repeat visitors but not with new addresses, and it still depends on the API on the cold path.
3. Scalable design
Principle: the connection path reads only local memory. Everything involving the regulator happens off the hot path.
Components
- Ingestor (central, small, replicated): pulls ban data from the regulator. If a bulk export or delta feed exists, use it. If only per-address lookup exists, then we cannot enumerate bans, so the design becomes a hybrid: an on-demand check for addresses never seen, with results cached, plus the asynchronous refresh described below. I would raise this limitation explicitly with the interviewer, since it changes the guarantee we can give for first-time addresses.
- Ban store and compiler: normalizes entries, merges overlapping prefixes, and produces a versioned snapshot (for example, version number, checksum, timestamp). Snapshots are published to object storage or a config distribution service, with small deltas between versions.
- Edge enforcement: each load balancer, proxy, or host loads the snapshot into memory and checks every connection locally.
- Audit and monitoring pipeline: receives refusal events and sync-health metrics.
Where the check runs
At the earliest point where the real client address is known: the L4/L7 load balancer or edge proxy. If traffic passes through CDNs or proxies, use the PROXY protocol or trusted forwarding headers, and only trust them from known upstreams. Checking an internal hop address is a classic mistake that makes the whole control useless.
Data structure
A binary (or compressed radix) trie keyed on address bits, with separate roots for IPv4 and IPv6, supports longest-prefix match in O(address length), which is at most 32 or 128 steps. Alternatives: sorted non-overlapping ranges with binary search (compact and cache-friendly, O(log n)), plus a hash set for exact /32 and /128 entries. Rough estimate under an assumption of 5 million entries: at around 50 bytes per entry, that is about 250 MB for a naive trie, but a compressed trie or sorted range array of 16-32 bytes per entry fits within 100-160 MB. This is acceptable per node, and if not, a Bloom filter in front of the structure removes most negative lookups cheaply (with a confirmed lookup on a positive). These numbers are assumptions that I would replace once the real list size is known.
Distribution
- Publish snapshots as immutable, versioned artifacts. Nodes poll (or receive push notifications), download the delta or full snapshot, verify the checksum, build the new structure on the side, and swap an atomic pointer. Readers never see a half-built state.
- Keep the previous version for rollback. Stagger rollouts or use a canary check so a corrupted snapshot (for instance, one that bans everything) cannot take down the fleet. Add a sanity guard, such as rejecting a snapshot whose size changes by more than a configured percentage without human approval.
4. Freshness
End-to-end delay = regulator update to ingestor poll + compile + publish + node fetch + swap. With a 1-minute poll and 1-minute node refresh, we have a worst case of a few minutes. I would measure it by stamping each snapshot with the source-observed time and having each node export now - snapshot_source_time as a staleness gauge, alerting when it exceeds the freshness target.
Lifted bans propagate the same way. If the lifted-ban deadline is looser than the added-ban deadline, I can use a slower cadence for removals only if the owner agrees.
5. Existing connections
A snapshot swap affects only new connections by default. For long-lived connections (WebSockets, streaming, persistent TCP), on each swap, compute the newly added prefixes and either scan active connection tables or have each node periodically re-evaluate open connections, closing those that now match. Cost is proportional to the number of open connections times the number of changes, so run it as a background sweep rather than inline.
6. Failure modes
- Regulator API down: the fleet keeps enforcing the last known snapshot. No impact on the data path. Alert on ingest staleness.
- Pipeline or distribution down: nodes continue with their loaded version, and staleness alerts fire.
- New node or cold start with no snapshot: this is the real fail-open versus fail-closed decision. Fail-closed is safest for compliance but causes outages; fail-open risks violations. This is a policy decision for legal and the business owner, not an engineering default. A reasonable pattern is to bake a recent snapshot into the image and block readiness until a snapshot is loaded, so the question rarely arises.
- Unknown address when only per-address lookup exists: apply the approved policy, with a short timeout and a negative cache.
7. Proof of compliance
Log each refusal with timestamp, client IP (mind privacy rules for retention), matched prefix, snapshot version, and node ID. Keep snapshot versions and their hashes archived so we can later show which list was in force at any moment. Dashboards track refusal counts, staleness, and snapshot version skew across the fleet.
Common mistakes
- Calling the regulator per connection.
- Assuming IPv4 only without asking.
- Checking the proxy's address instead of the real client address.
- Non-atomic updates leading to partial state.
- Picking fail-open or fail-closed implicitly instead of getting it approved.
- Ignoring connections already open.
- Quoting fleet size or traffic numbers as facts when they were never given.