An API Gateway serves as the single entry point for client requests in a microservices architecture, routing each request to the appropriate backend service while centralizing cross-cutting concerns.
Key Points: • Request routing directs incoming traffic to the correct microservice based on the request path or other attributes, so clients don't need to know internal service topology. • Load balancing spreads requests across multiple instances of a service, improving both performance and resilience. • Authentication and authorization are enforced centrally, so individual services don't each need to reimplement the same security checks. • Rate limiting protects backend services from being overwhelmed by excessive traffic from a single client or a traffic spike. • Caching and logging at the gateway reduce redundant backend load and give a single place to observe all incoming traffic.
Example: Spring Cloud Gateway configured with a JWT authentication filter, a Redis-backed rate limiter, and route definitions for /orders/** and /payments/** demonstrates all of these responsibilities working together in one component.
Code Example:
spring:
cloud:
gateway:
routes:
- id: order-service
uri: lb://order-service
predicates:
- Path=/orders/**
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20Interview Tip: A concise interview answer is:
"The API Gateway is the single entry point that routes requests to the right service, and I'd use it to centralize authentication, load balancing, rate limiting, caching, and logging so those concerns don't need to be duplicated inside every individual microservice."