API Gateway
An API Gateway is the single front door for Orders and sibling services: routing, TLS termination, auth validation, rate limiting, and request rewriting before traffic hits Spri…
Introduction
An API Gateway is the single front door for Orders and sibling services: routing, TLS termination, auth validation, rate limiting, and request rewriting before traffic hits Spring Boot pods.
Spring Cloud Gateway is the reactive JVM-native option; Kong, NGINX, and cloud gateways are common alternatives. The gateway applies cross-cutting policy once so each microservice stays focused.
Orders routes might strip prefixes, load-balance via service discovery, and enforce Redis token buckets for abuse control.
This lesson places TechLearningPRO Orders behind a gateway with Boot 3-friendly patterns — still no secrets in source.
Purpose of this lesson
Route and protect Orders traffic through an API gateway with predicates, filters, and rate limits.
Understanding the topic
Gateway = edge policy + routing; services = business logic.
- routes: id, uri, predicates, filters.
- predicates: path, method, header matches.
- filters: StripPrefix, auth, rate limit, retry.
- discovery:
lb://orders-servicewith a registry. - defense: volumetric limits at the edge, business authz in the app.
Step-by-step explanation
- Define a gateway route for
/api/v1/orders/**to the Orders service. - Add StripPrefix and a Redis request rate limiter filter.
- Forward
Authorizationto the downstream resource server. - Verify health/actuator paths are not accidentally public through the gateway.
Informative example
Spring Cloud Gateway routes for Orders and catalog.
# application.yml (gateway)spring:cloud:gateway:routes:- id: ordersuri: lb://orders-servicepredicates:- Path=/api/v1/orders/**filters:- StripPrefix=2- name: RequestRateLimiterargs:redis-rate-limiter.replenishRate: 50redis-rate-limiter.burstCapacity: 100- id: cataloguri: lb://catalog-servicepredicates:- Path=/api/v1/products/**filters:- StripPrefix=2# Redis used by RequestRateLimiterspring:data:redis:host: ${REDIS_HOST:localhost}port: ${REDIS_PORT:6379}
Real-world use
Public scrapers hammered every Orders pod until rate limits lived only in-app — too late, CPU was already gone. Moving RequestRateLimiter to the gateway absorbed bursts in Redis while Orders pods spent cycles on real checkouts.
Security implications
The gateway is a high-value control point.
- Terminate TLS at the edge; use mTLS inside the mesh when required.
- Do not strip auth headers before Orders.
- Redis and gateway admin credentials via env placeholders only.
Best practices
- Edge for volumetric abuse; services for object-level authz.
- Keep route config in version control with env for secrets/hosts.
- Correlate gateway request ids with Orders logs/traces.
Common mistakes
- Exposing internal actuator routes through a catch-all path.
- Doing all authorization only at the gateway and trusting the mesh blindly inside.
- Hard-coding service hostnames instead of discovery-friendly
lb://URIs.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhat belongs in an API gateway vs the Orders service?+
Answer
2IntermediateQuestionWhy rate-limit at the gateway?+
Answer
3IntermediateQuestionWhat does lb://orders-service mean?+
Answer
Hands-on exercise
Lab. Configure a gateway route to a local Orders service with StripPrefix. Add a low Redis rate limit and prove that exceeding it returns 429 while a direct-to-service call (if exposed) still works — then lock down direct exposure.
Summary
An API gateway fronts TechLearningPRO Orders with routing, rate limits, and shared edge policy so Boot services stay focused on domain behavior.