Microservice Patterns
Gatekeeper
Put a dedicated, minimal-privilege host in front of trusted services to validate and sanitise every request, so attacks and junk are rejected cheaply at the edge and the services holding secrets are never directly exposed.
What you will learn
- Explain the gatekeeper's role and why it holds no secrets.
- See how cheap edge validation protects backend capacity.
- Decide what to validate at the gatekeeper and what stays in the service.
- Avoid making the gatekeeper a bottleneck or a single point of failure.
Before you start
Overview
A bank's vault staff never stand at the front door. Security at the entrance checks IDs and bags; only cleared visitors reach the people with the keys — and the guard at the door has no keys to steal.
A gatekeeper is a separate host or proxy that receives all public traffic. It authenticates, validates and sanitises requests — token signatures, schemas, sizes, allowed methods — and forwards only clean requests over a private network to the trusted host that has the credentials and the data. If the gatekeeper is compromised, the attacker still has no secrets.
Run it
Customers buy while bots send 1,200 invalid requests per second. Compare Purchases failed and Backend busy without (A) and with (B) a gatekeeper.
What the gatekeeper checks
| Check | Example |
|---|---|
| Authentication | Verify JWT signature, issuer, audience and expiry; reject missing tokens. |
| Shape | JSON schema, maximum body size, allowed content types and methods. |
| Sanitisation | Strip unexpected headers, normalise paths, reject path traversal. |
| Abuse | Rate limits per client, IP reputation, WAF rules for common injections. |
Business authorisation ("may this user refund this order?") stays in the service: it needs domain data the gatekeeper should not have. The backend still validates its inputs — the gatekeeper is one layer of defence in depth, not the only one.
Example
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://login.example.com/ # verify tokens locally with the issuer's public keys
cloud:
gateway:
server:
webflux:
routes:
- id: payments
uri: http://payments.internal:8080 # reachable only from the gateway's subnet
predicates: [ "Path=/api/payments/**", "Method=POST,GET" ]
filters:
- name: RequestSize
args: { maxSize: 64KB }
- RemoveRequestHeader=X-Internal-UserTrade-offs
| Benefits | Costs | |
|---|---|---|
| Security | Smaller attack surface; secrets live only on private hosts. | Another component to harden, patch and monitor. |
| Capacity | Invalid traffic is dropped cheaply before it reaches expensive services. | The gatekeeper must scale with all traffic, including attacks. |
| Latency | Validation is fast when done locally (no calls to auth servers). | One more network hop for every request. |
Production considerations
- Run several gatekeeper instances across zones; it must not be a single point of failure.
- Allow traffic to trusted hosts only from the gatekeeper (security groups, network policies, mTLS).
- Log rejected requests with reasons — they are your first attack signal.
- Keep the gatekeeper stateless and free of business logic.