Use-Cases of API Gateway
An API Gateway serves as the single entry point for all client requests in a microservices architecture. Here are the four primary use cases:
1. Caching
Purpose: Temporarily stores responses from backend services to speed up repeated requests for the same data.
How it works:
- Client sends request to API Gateway
- API Gateway checks Cache (Redis)
- If cached → return cached response
- If not cached → forward to backend service (Order History Service, Order Service)
- Store response in cache
- Return response to client
Benefits:
- Reduced latency for repeated requests
- Lower load on backend services
- Integrated CDC (Change Data Capture) for cache invalidation
2. Aggregation
Purpose: Combines data from multiple backend services into a single response to streamline client-side processing.
How it works:
- Client sends single request to API Gateway
- API Gateway fans out to multiple services (Service 1, Service 2, Service 3)
- Collects responses from all services
- Combines into unified response
- Returns aggregated data to client
Benefits:
- Reduces number of client requests
- Simplifies client logic
- Optimizes network usage
3. Request Routing
Purpose: Directs incoming requests to the correct backend service based on the request path or other attributes.
How it works:
- Client request arrives at API Gateway
- API Gateway inspects request path/attributes
- Routes to appropriate service:
/catalogue/*→ Catalogue Service/discount/*→ Discount Service/ordering/*→ Ordering Service
Benefits:
- Clean separation of concerns
- Dynamic routing capabilities
- Path-based or header-based routing
4. Authentication and Authorization
Purpose: Validates user credentials and permissions before allowing access to backend services.
How it works:
- Service 1 sends request with token to API Gateway
- API Gateway forwards token to Auth Server
- Auth Server validates token and returns permissions
- If valid → API Gateway forwards request to backend
- Response flows back through API Gateway to client
Flow:
Client → Request with Token → API Gateway
↓
Auth Server
(Request Token → Response Token)
↓
Backend Services
Benefits:
- Centralized security
- Single point of authentication
- Consistent authorization policies
Sources
Arhitectura/api gateway.jpeg- Credit: @learnwithrockybhatia