Load balancer security
medium 26 minA load balancer is the front gate of a system.
Video lesson · Chapter 1 of 5
Which side of the proxy can read the request
The gate guard: load balancer security in 75 seconds
1:15Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
All public traffic meets the load balancer first. That makes it a good place for checks that should apply to every request: encryption, allowed addresses, request limits, known bad patterns and the size of a message.
Load balancer security means two things. The balancer protects the servers behind it, and the balancer itself must be protected: its keys, its settings and its capacity.
Why it exists
Checks in one place are easier to keep correct than the same checks in fifty services. A bad request that is refused at the gate costs the servers nothing.
The gate is not enough alone. It does not know whether this user may see that invoice, it can be wrong about what is harmful, and a flood bigger than its network link never reaches its rules. So we use defence in depth: several layers that each stop part of the trouble.
A short history
May 2000
RFC 2827 asks networks to drop packets whose source address cannot be true. Forged addresses are what make many floods possible.
August 2007
RFC 4987 gathers the known defences against SYN floods, including SYN cookies.
April 2012
RFC 6585 defines status 429, Too Many Requests.
December 2015
Let's Encrypt opens its public beta. Free, automatic certificates make HTTPS normal.
August 2018
TLS 1.3 is published as RFC 8446.
March 2021
RFC 8996 says TLS 1.0 and 1.1 must not be used.
Words to know
- TLS
- Transport Layer Security: encryption and proof of identity for a connection. SSL is the old name of its broken ancestors.
- TLS termination
- The encrypted connection ends at the balancer, which can read the request.
- mTLS
- Mutual TLS: both sides show a certificate.
- WAF
- Web application firewall: rules that inspect HTTP requests.
- DDoS
- Distributed denial of service: many sources try to make a service unavailable.
- SYN flood
- Many half-open TCP connections, meant to fill memory.
- Rate limit
- How many requests a client may make in a time.
- False positive
- A good request that is blocked by mistake.
How it works
Step through the idea, or press Play.
At the connection. Address rules, connection limits, SYN protection
Understand it
Start here
The castle guard story
Imagine a castle with a moat and a drawbridge. The gate is where everyone must pass, so that is where the guard stands. A wise castle also has a barrier on the road far outside.
The moat slows crowds, the gate checks, the guard inspects, the list decides and the sign limits.
Overview
What it protects against
Threats in words
| Threat | What it tries | Main defence |
|---|---|---|
| SQL injection | Make input be read as a database command | Prepared statements in the service. The firewall is a second net |
| Cross-site scripting | Make a page run someone else's script | Escape output. Content Security Policy. Firewall second |
| Path traversal | Reach files outside the allowed folder | Safe file handling, clean paths |
| Command injection | Make input be run by a shell | Do not call a shell with input |
| Cross-site request forgery | Use a logged-in browser without consent | Tokens and SameSite cookies. A firewall helps little |
| Person in the middle | Read or change traffic | TLS with checked certificates |
| Floods | Use up bandwidth, memory or workers | Upstream filtering, limits, spare capacity |
| Bots and scrapers | Take data or try stolen passwords | Rate limits, bot detection, multi-factor login |
Defence in depth
Jobs of the gate
End TLS
Encrypt the public side and hold the certificates.
Firewall
Inspect requests for known bad patterns.
Absorb floods
SYN protection, timeouts and limits.
Limit rates
Per client, key or endpoint.
Address rules
Allow lists and block lists.
Clean headers
Remove or replace headers that only we may set.
TLS
Three places TLS can end
| Choice | Between balancer and server | Can inspect? | Notes |
|---|---|---|---|
| Termination | Plain HTTP | Yes | Least server work, certificates in one place, inside traffic readable |
| Passthrough | The client's own TLS | No | Balancer holds no key, servers do the work, no path rules or firewall |
| Re-encryption (bridging) | A second TLS connection | Yes | Inside traffic encrypted, more processing, two sets of certificates |
The three choices
Re-encryption
Good TLS habits
- 1
Offer TLS 1.3 and 1.2 only
TLS 1.0 and 1.1 must not be used (RFC 8996). TLS 1.3 also needs one round trip less for a new connection than TLS 1.2.
- 2
Use a maintained cipher list
Take the provider's current policy or your standard, and do not invent one.
- 3
Renew automatically
Automate certificates and alert well before the end date. Short lifetimes limit the harm of a lost key.
- 4
Guard the keys
Keep them in a key store or hardware module with few readers. Never in code or images.
- 5
Redirect to HTTPS
And send HSTS once every part of the site works over HTTPS.
- 6
Check the inside too
With re-encryption, verify the server's certificate. Use mTLS when servers must be sure who calls.
Order
Which check can come first?
A check can only use what can already be read. Before TLS ends, the balancer knows the connection's address and perhaps the requested host name. Headers, paths and API keys come later.
A request through the gate
WAF
Web application firewall
A WAF reads HTTP requests, so TLS must end before it. Rules look at the path, query, headers and body. Some describe what is bad (a block pattern). Others describe what is allowed (methods, sizes, formats) and refuse the rest.
What a rule can do
| Action | Effect | Use |
|---|---|---|
| Block | Refuse, often with 403 | A rule you trust |
| Challenge | Ask the browser or person to prove itself | Suspicious but not certain |
| Count or log | Let it pass and record it | Testing a new rule |
| Rate limit | Slow or refuse repeated requests | Login and search pages |
endpoint /ordersmethods GET, POSTbody JSON, at most 32 KBpath already in its normal formfields item: integer, quantity: 1 to 20otherwise count first, then blockThe OWASP CRS is a free set of generic rules for ModSecurity and compatible firewalls. Broad patterns also match honest text: a book titled "Select from the Union" looks a little like a database command.
Why good rules still block good people
WAF in practice
From counting to blocking
The life of a rule
Rolling out a rule
- 1
Count
Record matches for days, including a busy one.
- 2
Read
Look at samples and write narrow exceptions.
- 3
Challenge or block
Start with one endpoint or a few percent.
- 4
Watch
Compare errors, sign-ups and support messages.
Floods
Denial of service
| Kind | Examples | Protection |
|---|---|---|
| Network, layer 3 | UDP and ICMP floods, amplification | Upstream scrubbing, anycast networks, drop what you do not serve |
| Transport, layer 4 | SYN floods | SYN cookies, connection limits, short handshake timeouts |
| Application, layer 7 | HTTP floods, very slow requests, costly API calls | Rate limits, header and body timeouts, caching, challenges, bot detection |
When local rules cannot help
SYN cookies
Limits
Three rate limiting ways
| Way | Rule | Watch out |
|---|---|---|
| Fixed window | 100 requests per minute, resets on the minute | 200 can pass around the reset |
| Sliding window | 100 in any 60 seconds | More memory, or an approximation |
| Token bucket | Bursts up to the bucket, then a steady refill | Choose both numbers |
Most requests a token bucket can pass
Answer with 429 and, if you can, a Retry-After header. Check your product: NGINX's limit_req answers 503 unless you set another status. An API gateway often holds limits per key or customer.
The gate itself
Protect the balancer
Do
- Separate the admin network from the public one
- Change settings through reviewed code
- Update promptly
- Let servers accept traffic only from the balancer
- Run more than one, in different zones
- Alert on odd traffic, login failures, blocks and certificate dates
Do not
- Expose the admin page to the internet
- Share one administrator login
- Leave servers reachable around the gate
- Write secrets or whole bodies to logs
- Block countries or Tor without a reason
- Treat a reputation list as always right
Address reputation lists can block known bad sources, but addresses are shared and change hands. Give every block an end date and a way to ask for review.
Best practice
The short list
- 1
Modern TLS
1.3 and 1.2, current policy.
- 2
Defence in depth
Edge network, flood protection, firewall, balancer, servers. Each checks something different.
- 3
Monitor and alert
Traffic shape, failed logins, refusals by rule, and good requests that were blocked.
- 4
Automatic certificates
Renew early and alert on failure.
- 5
Reputation with care
Feeds as one signal, with an end date.
Summary
The whole topic in one minute.
Put the checks in an order that is possible. Address rules and connection limits come first, then TLS ends, and only then can the balancer read headers, apply a firewall and limit by user or endpoint.
Choose where TLS ends, keep keys safe and renew certificates automatically. Start firewall rules in count mode. Plan for floods larger than your link with a provider in front. Keep authorisation and input checks in the services, because the gate is one layer, not all of them.
- The balancer is a gate. Services still check permission and input.
- Order matters: connection checks, TLS, limits, firewall, forward.
- Termination, passthrough and re-encryption differ in who can read.
- Test firewall rules in count mode.
- A flood larger than your link must be stopped upstream.