KatInterview

API Gateway Traffic Filtering Across Multiple Regions

System DesignModel answer
MediumModel answerSystem Designsystem-designnetworkingdistributed-systems

Last updated

Design a system that blocks untrusted IP addresses at an API gateway, handles rule versioning and expiration, and replicates enforcement state across data centers.

API Gateway Traffic Filtering Across Multiple Regions

Your team operates a set of API gateways deployed across several data centers. Security analysts flag IP addresses that should be denied access. Your job is to design the enforcement layer that sits inside each gateway, and then extend it so that rule changes propagate reliably to every region.

Background

Detection (deciding which IPs are bad) and enforcement (acting on that decision at the gateway) are separate concerns. Focus on enforcement. You may assume a control plane already produces rule updates; your design should consume them.

What to Design

  1. Single-gateway enforcement — how a gateway decides, on every inbound request, whether the source IP is blocked. Include how you extract the real client address, where rules live in memory, and how you handle expiring rules.

  2. Rule management — how rules are created, versioned, and removed. Address both individual addresses and network ranges (including IPv6). Explain how a false positive gets resolved quickly.

  3. Multi-region propagation — how a new block or an urgent unblock reaches every data center within an acceptable window. Describe what a gateway does when the control plane is unreachable or its local rule set is stale.

Clarifying Questions to Address

Before diving in, state the assumptions you are making about:

  • Who can create or revoke a block, and whether rules carry a scope or expiration.
  • Whether rules cover exact IPs, CIDR ranges, or both, and whether IPv6 is in scope.
  • What propagation latency is acceptable for normal blocks versus urgent unblocks.
  • The desired gateway behavior under control-plane failure.

Constraints

  • Rule volume and request throughput are unspecified — pick reasonable defaults and state them.
  • Blocking a shared IP (e.g., a corporate NAT or a Tor exit node) may affect legitimate users; accuracy and false-positive cost matter.
  • Treat block and unblock as asymmetric operations: an unblock may need faster or more reliable delivery than a new block.

Expected Output

A structured prose answer covering your assumptions, the data structures and APIs you would use, the propagation mechanism, failure-mode behavior, and the trade-offs you considered. No code is required, but diagrams described in words, latency estimates, and concrete technology choices are all encouraged.

Hints

Hint 1

Start by separating two things that look similar but aren't: the IP you see on the TCP socket versus the real client IP forwarded through load balancers. Getting this wrong means you block your own infrastructure. Walk through X-Forwarded-For parsing and why you trust only the rightmost address added by a component you control.

Hint 2

Think about block vs. unblock as two different SLAs, not one. A new block can tolerate a few seconds of propagation lag; an unblock of a falsely flagged IP may need near-instant delivery. This asymmetry should drive your replication design — consider a push channel (pub/sub or change-data-capture) for urgent operations on top of a periodic full-sync for correctness.

Hint 3

A radix trie (Patricia trie) lets you match both exact IPs and CIDR prefixes in O(address length) time, which is effectively O(1) for IPv4 and O(1) for IPv6. Pair this with a versioned snapshot so a gateway can swap in a new rule set atomically rather than applying partial updates mid-request.