Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 60

    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…

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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-service with a registry.
    • defense: volumetric limits at the edge, business authz in the app.

    Step-by-step explanation

    1. Define a gateway route for /api/v1/orders/** to the Orders service.
    2. Add StripPrefix and a Redis request rate limiter filter.
    3. Forward Authorization to the downstream resource server.
    4. Verify health/actuator paths are not accidentally public through the gateway.

    Informative example

    Spring Cloud Gateway routes for Orders and catalog.

    bash
    # application.yml (gateway)
    spring:
    cloud:
    gateway:
    routes:
    - id: orders
    uri: lb://orders-service
    predicates:
    - Path=/api/v1/orders/**
    filters:
    - StripPrefix=2
    - name: RequestRateLimiter
    args:
    redis-rate-limiter.replenishRate: 50
    redis-rate-limiter.burstCapacity: 100
    - id: catalog
    uri: lb://catalog-service
    predicates:
    - Path=/api/v1/products/**
    filters:
    - StripPrefix=2
    # Redis used by RequestRateLimiter
    spring:
    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.

    3 questions
    1BeginnerQuestionWhat belongs in an API gateway vs the Orders service?+

    Answer

    Gateway: routing, TLS, coarse auth, rate limits. Service: business rules and object-level authorization.
    2IntermediateQuestionWhy rate-limit at the gateway?+

    Answer

    Volumetric abuse should be stopped before it saturates application CPU and connection pools.
    3IntermediateQuestionWhat does lb://orders-service mean?+

    Answer

    A load-balanced URI resolved via service discovery to instances of the Orders service.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.