Skip to content
Lesson 58

Proxies and firewalls

Meet the boxes between the internet and StayHub's code: firewalls, L4 and L7 load balancers, a WAF and an API gateway. Lead the response when one partner's script floods the API with 40,000 requests a minute.

The lesson in text

Click a paragraph to hear it.

0140,000 requests a minute

Tuesday, 10:15. Omar sees StayHub's partner API, /partner/availability, receiving 40,000 requests a minute, all from one IP address. All 300 partners together normally send about 3,600.

Maya has the guest side: search, for ordinary guests, has slowed from 300 milliseconds to 2.5 seconds. The source is TravelNest, a travel agency whose new sync script has a bug.

Karim says: block their IP at the firewall, now. This one is yours to lead; Lina will only check your answer. To answer well, you need to know the boxes that stand between the internet and StayHub's code, and what each one can see.

02Who works the entrance

Think of the entrance of a big hotel. The doorman only sees which door you come through. He can't read your booking, and he doesn't know who you work for.

Reception reads what you ask for and sends you to the right desk. The business desk checks your company badge, and counts how many requests your company has made today. And security checks bags for known dangerous items.

Each of them can only act on what they can see. That's the rule for this whole lesson: put each rule in the first box that can see what the rule needs.

Who works the entrance
At the hotelSeesAt StayHubSees
DoormanWhich door you useNetwork firewall, L4 load balancerIP addresses, ports, connections
ReceptionWhat you ask forL7 load balancer, reverse proxyThe HTTP request: path, headers
Business deskYour company badge, today's countAPI gatewayWho's calling (API key), how often
SecurityBags, for known dangerous itemsWAFKnown attack patterns in requests
Each one can only act on what it sees. Put each rule in the first box that can see what the rule needs.

03The boxes in front of StayHub

A request to StayHub passes several boxes before it reaches any code. First, the CDN. It's a reverse proxy: it receives requests meant for StayHub's servers and passes them on, ending TLS near the guest, serving cached files and soaking up junk floods.

Its mirror image is a forward proxy: it sits in front of your own servers when they call out to the internet, and speaks for them. A reverse proxy speaks for the servers to the outside; a forward proxy speaks for them on the way out.

Next comes the load balancer. An L4 load balancer sees addresses, ports and connections, nothing more: that's the doorman. An L7 load balancer ends TLS and reads the HTTP request, its path and headers: that's reception, and it can send partner requests to a different pool.

The boxes between the internet and StayHub's code
  1. 1CDNreverse proxy
    ends TLS near the guest, caches, absorbs junk floods, coarse per-IP limits
  2. 2WAFweb application firewall
    known attack patterns; one layer, not the fix
  3. 3API gatewayL7, knows the caller
    /partner/* only: key check, 50 req/s per key, 429 + Retry-After
  4. 4Load balancerL7
    reads path and headers; partners → their own Search pool
  5. 5Servicesport 8080
    accept connections only from the load balancer
  6. 6Databaseport 5432
    accepts connections only from the services
Firewall rules sit around every tier: each one accepts connections only from the tier in front of it. A forward proxy is the mirror image: it speaks for your own servers when they call out.

A WAF, a web application firewall, reads the decrypted requests and looks for known attack patterns: security checking bags. It's a useful layer, but cleverly disguised attacks slip past it, and rules that are too strict block real guests.

And the network firewall allows or refuses connections by IP address and port. Nothing more. At StayHub, the services accept port 8080 only from the load balancer, and the database accepts port 5432 only from the services.

04Why the firewall is the wrong tool

Back to Karim's idea. A firewall sees one thing here: TravelNest's IP address. Blocking it stops the flood, and also every real booking TravelNest sends. And if their script runs in a shared cloud, that address may be shared with other partners too.

Adding Search servers? That only feeds the bug. StayHub would pay for machines answering the same question six hundred times a second.

And moving to an L4 load balancer because it's faster doesn't help either. An L4 box can't see paths or keys. Speed isn't the problem. Visibility is.

The rule you need is: no single partner may send more than its share. To apply it, a box must know who the partner is, from the API key in each request. That's the business desk.

05The business desk

So you put an API gateway in front of every partner request. It's an L7 proxy that knows who's calling: it checks the API key, and it counts requests per key.

The quota: 50 requests a second per key, with short bursts up to 100. Above that, the gateway answers 429, too many requests, with a Retry-After header that says when to try again.

A per-key quota at the gateway
TravelNest's script670 req/s
Let through (quota)50 req/s
Refused with 429620 req/s
All partners, a normal day60 req/s
HTTP/2 429 Too Many Requestsover the quota
Retry-After: 1come back in a second
40,000 requests a minute ≈ 670 a second. The gateway lets 50 a second through (bursts up to 100) and refuses the rest in about a millisecond each, so guest search never feels it.

TravelNest's script sends about 670 requests a second. The gateway lets 50 through and refuses the other 620, in about a millisecond each. Their real bookings still get through, and the other 299 partners never notice.

Second protection: partner traffic gets its own pool of Search servers. That's a bulkhead, like the watertight walls inside a ship. Even if a quota is set wrong one day, partners can't sink guest search.

And answer with a 429, never a silent drop. A dropped request looks like a network failure, so the partner's code simply retries, and the flood doubles. A 429 with Retry-After tells it to slow down.

06The header anyone can write

Anonymous guests have no key, so public search gets a coarse limit per IP address: 20 requests a second. But behind the CDN, every request arrives from the CDN's own address. The guest's real IP travels in a header called X-Forwarded-For.

Each proxy adds the address it received the request from, at the right end of that list. Whatever is at the left end was written by the client itself, and the client can write anything.

Claude Code's first version read the leftmost value. A scraper simply sent a random address in that header with every request. Each request looked like a new visitor, the limit never fired, and 900 requests a second reached Search.

Reading X-Forwarded-For safely
what the app receives
198.51.100.233203.0.113.57CDN edge
left: written by the scraper, random each time
the client: the first address our proxies didn't add
right: added by our own proxies, skipped
Each proxy appends the address it received the request from. The leftmost entries are whatever the client wrote. Skip the hops your own proxies added, from the right; the first address you didn't add is the client.

The fix: trust that header only when it comes through your own proxies, and read it from the right, skipping the hops your proxies added. The first address you didn't add yourself is the client.

07The decision

Thursday, before switching quotas on, Maya worries. Partners bring 22% of bookings. If a big agency gets 429s during a sale, it may go to a competitor. Why not just scale Search?

Omar answers: scaling for a buggy script means paying for servers that answer the same question six hundred times a second. Your decision: a quota for every key, higher tiers for big partners agreed in advance, and a warning to all 300 partners before it's switched on.

Thursday: quotas or more servers?
Maya: partners = 22% of bookings; just scale Searchrisk: 429s during a sale
Omar: same question 600 times a secondcost without a cap
Decision: a quota for every key50 req/s, burst 100
higher tiers for big partners, agreed aheadannounced to all 300 first
firewall: each tier only from the tier in frontwho may connect at all
Scaling for a buggy script pays for servers that answer the same question hundreds of times a second. A quota per key caps what any one caller can do; tiers agreed in advance keep big partners happy.

And the firewall keeps its own job: each tier accepts connections only from the tier in front of it. The gateway decides how much; the firewall decides who may connect at all.

08Your job as lead

When Claude Code adds a limit or a rule, ask: which box enforces it, and can that box see what the rule needs? Per partner means a key, so the gateway. Per anonymous visitor means an IP address, at the edge.

Check where the client IP comes from: only your own proxies' entries, read from the right. A list of trusted proxies that says "everyone" is a red flag.

Check every diff for firewall changes: there should be none unless you asked. No database port opened to the internet "just for debugging". And over the limit means a 429 with Retry-After, never a silent drop.

Let's recap. A box can only decide on what it sees. Firewalls see addresses and ports, L4 boxes see connections, L7 proxies read requests, a gateway knows who's calling, a WAF looks for known attacks. Put each rule in the first box that can see what it needs. That closes networking. Next: the cloud, and a bill nobody can explain.

Check yourself

0/10 answered
  1. 1What can a network firewall base its decisions on?

  2. 2One partner's buggy script floods the partner API from one IP. Karim says: "Block their IP at the firewall." What's the problem?

  3. 3Which box can enforce "no partner may send more than 50 requests a second"?

  4. 4Karim suggests moving to an L4 load balancer because it's faster. Why doesn't it help with the flood?

  5. 5A partner goes over its quota. What should the gateway do?

  6. 6Why give partner traffic its own pool of Search servers, on top of the quota?

  7. 7StayHub's CDN receives guests' requests for StayHub's servers and passes them on. What kind of proxy is it?

  8. 8Claude Code's per-IP limit reads the leftmost value of X-Forwarded-For. A scraper still gets 900 requests a second through. What do you ask for?

  9. 9To debug a slow query, Claude Code proposes opening the database's port 5432 to the internet "just for today". What do you say?

  10. 10Maya fears 429s will upset big partners during a sale and suggests scaling Search instead of quotas. What's the better decision?

Free account

Save your progress — create a free account

Free. No card. Your finished lessons follow you to any device, and you can ask questions in the community.

Create my free account
Founding members

Every lesson is free while we build. Lock in €9/month for later.

The first 100 members keep the founding price for as long as they stay. Reserving is free: no card, nothing is charged.

See the founding offer

Questions from the community

All questions →