BackendBench
Coffee

Unravel › Java concepts › Cloud and operations

Spring

API gateways (Spring Cloud Gateway)

A gateway is the single front door of a set of microservices.

Learn these first

At a glance

Mental model

The reception desk of a large office building.

What happens, in order
  1. 1A route is: a predicate (which requests match, for example path /orders/**), a target URI (lb://order-service) and filters (strip a prefix, add a header, rate limit, retry)
  2. 2Routes are declared in YAML or in Java with RouteLocatorBuilder: builder.routes().route("orders", r -> r.path("/orders/**").uri("lb://order-service")).build()
  3. 3A GlobalFilter runs for every route and is the right place for token checks, correlation ids and loggingA GatewayFilter applies to chosen routes.
  4. 4With service discovery, lb://name picks a healthy instance of that service each time
Remember
Watch out
Blocking calls inside gateway filtersTrusting the gateway as the only security layerBusiness logic in the gatewayNo timeouts on routes, so slow services pile up requests
Example (highlighted lines explain it)
@Beanpublic RouteLocator routes(RouteLocatorBuilder builder) {    return builder.routes()        .route("orders", r -> r.path("/api/orders/**")            .filters(f -> f.stripPrefix(1).addRequestHeader("X-Source", "gateway"))            .uri("lb://order-service"))        .build();}
  1. line 2 uses RouteLocator, the core of API gateways (Spring Cloud Gateway)
Read the full explanation (about 1 min)

A gateway is the single front door of a set of microservices. It routes each request to the right service and applies cross-cutting rules (authentication, rate limiting, headers) in one place.

A mental model

The reception desk of a large office building. Visitors say who they want; reception checks ID, hands out a badge and points them to the correct floor, so no department needs its own security.

How it works

A route is: a predicate (which requests match, for example path /orders/**), a target URI (lb://order-service) and filters (strip a prefix, add a header, rate limit, retry).

Routes are declared in YAML or in Java with RouteLocatorBuilder: builder.routes().route("orders", r -> r.path("/orders/**").uri("lb://order-service")).build().

A GlobalFilter runs for every route and is the right place for token checks, correlation ids and logging. A GatewayFilter applies to chosen routes.

With service discovery, lb://name picks a healthy instance of that service each time.

Going deeper

Filters are reactive: they receive the exchange and the chain, and must return a Mono<Void>. Blocking inside a filter blocks the whole gateway, which has few threads.

Do authentication at the edge but also inside services. A gateway bypass (a service reachable directly in the cluster) otherwise exposes everything.

Keep business logic out of the gateway. It becomes a hidden monolith and a single point of failure if it grows rules and database access.

Under the hood

The gateway is a WebFlux application on Netty. A RoutePredicateHandlerMapping evaluates predicates in order, the matching route builds a filter chain (global filters merged with route filters, ordered), and the last filter proxies the request with a reactive HTTP client and streams the response back.

Common mistakes

  • Blocking calls inside gateway filters.
  • Trusting the gateway as the only security layer.
  • Business logic in the gateway.
  • No timeouts on routes, so slow services pile up requests.

How Unravel recognises it

When you paste code, Unravel looks for these signs of api gateways (spring cloud gateway). Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is API gateways (Spring Cloud Gateway)?

A gateway is the single front door of a set of microservices. It routes each request to the right service and applies cross-cutting rules (authentication, rate limiting, headers) in one place.

What are the most common mistakes with API gateways (Spring Cloud Gateway)?

Blocking calls inside gateway filters. Trusting the gateway as the only security layer. Business logic in the gateway. No timeouts on routes, so slow services pile up requests.

How can I see API gateways (Spring Cloud Gateway) in real code?

Paste your own Java into Unravel and it will point out where api gateways (spring cloud gateway) appears, explain how it works at the detail level you choose, and draw the flow of the code around it.