Spring Cloud Gateway: Routing and Filters
Fast recall
Quick revision
Core answer
Whether you understand reactive vs blocking gateways, how predicates and filters compose, and whether you can write a production gateway config with auth, rate limiting, and circuit breaking
Confusing Spring Cloud Gateway with Zuul 1 (blocking, thread-per-request) or trying to deploy it in a traditional servlet container — Gateway requires a reactive runtime
Explain the filter chain execution order (pre-filters run in ascending order, post-filters run in descending), demonstrate a custom GlobalFilter for JWT validation, and discuss how lb:// URIs integrate with Eureka or Spring Cloud LoadBalancer
Interview-ready guide
Interview answer
Why Spring Cloud Gateway replaced Zuul
- Zuul 1 was blocking — one thread per request; thread pools exhausted under load
- Gateway runs on Netty and Project Reactor — non-blocking, a small thread pool handles thousands of concurrent connections
- Critical: Gateway requires a reactive runtime; do not add spring-boot-starter-web alongside webflux or the app fails to start
How routes are defined
- Each route has an id, a URI, predicates (match rules), and filters (transformations)
- Predicates match on path, method, headers, query params, cookies, or time windows
lb://order-servicetriggers Spring Cloud LoadBalancer resolution via the service registryStripPrefix=1removes path segments before forwarding — miscounting silently routes to wrong downstream paths- YAML for static routes; programmatic RouteLocatorBuilder when logic depends on runtime state
Pre-filter and post-filter execution order
- Pre-filters run in ascending order number — lower order runs first
- Post-filters run in descending order — higher order runs first
- The outermost pre-filter is also the outermost post-filter, like nested parentheses
- Auth filter at order -1 runs before logging at order 1 in the pre-phase; in post-phase the logging runs first
GlobalFilter vs GatewayFilter
- GlobalFilter applies to every route automatically — use for auth, request-id injection, tracing setup
- GatewayFilter applies to routes that list it — use for route-specific path rewriting, rate limiting, circuit breaking
- Custom GatewayFilterFactory classes become reusable filter names referenced from YAML
Writing a JWT GlobalFilter
- Read the Authorization header, validate signature, short-circuit with 401 if invalid
- Mutate the request with extracted claims as X-User-Id / X-User-Roles headers for downstream services
- Validation must be non-blocking — JWT signature check is CPU-only; a JDBC lookup in the filter blocks the Netty event loop and destroys throughput
- For I/O-dependent validation, return a Mono and chain with flatMap/switchIfEmpty
The event-loop trap
- Any blocking call inside a filter holds a Netty thread handling hundreds of connections
- Symptoms: throughput collapses, connection queues grow, p99 latency balloons
- Use BlockHound or Reactor checkpoint() in dev to catch accidental blocks
- Prefer WebClient over RestTemplate inside filters
Production essentials
- Always chain with CircuitBreaker filter — one slow backend otherwise blocks the event loop and starves healthy routes
- Rate limit with RedisRateLimiter for distributed fleets; local rate limiters per pod don't coordinate
- Run at least three gateway instances behind a cloud load balancer with readiness probes
- Monitor gateway-internal latency separately from downstream latency — above 10ms means the gateway itself is under resource pressure
The strategic answer
- Spring Cloud Gateway is non-blocking; Netty event loop handles thousands of concurrent connections
- Routes compose predicates and filters; lb:// integrates service discovery
- Pre-filters ascend, post-filters descend — order bugs cause auth to fire after business logic
- Keep the gateway thin — auth, rate limit, route — business logic stays downstream
- Never block inside a filter; the event loop is the throughput
Spring Cloud Gateway: Routing and Filters
Direct Answer
Spring Cloud Gateway is a non-blocking, reactive API gateway built on Spring WebFlux. You define routes using predicates (path, host, header matching) and filters (modify request/response, rate-limit, circuit-break). It replaces Zuul and runs on Netty — not a servlet container.
Common Mistakes & Pitfalls
Confusing Spring Cloud Gateway with Zuul 1 (blocking, thread-per-request) or trying to deploy it in a traditional servlet container — Gateway requires a reactive runtime
Check your understanding
Answer these without looking back at the article.
- How do you implement per-user rate limiting instead of per-IP rate limiting in Spring Cloud Gateway?
- What is the difference between a GlobalFilter and a GatewayFilter, and when would you use each?
- How does Spring Cloud Gateway integrate with Resilience4j for circuit breaking?
Related interview prep categories
2Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.