📄

Use-Cases of API Gateway

Intermediate 2 min read 300 words

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:

  1. Client sends request to API Gateway
  2. API Gateway checks Cache (Redis)
  3. If cached → return cached response
  4. If not cached → forward to backend service (Order History Service, Order Service)
  5. Store response in cache
  6. 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:

  1. Client sends single request to API Gateway
  2. API Gateway fans out to multiple services (Service 1, Service 2, Service 3)
  3. Collects responses from all services
  4. Combines into unified response
  5. 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:

  1. Client request arrives at API Gateway
  2. API Gateway inspects request path/attributes
  3. 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:

  1. Service 1 sends request with token to API Gateway
  2. API Gateway forwards token to Auth Server
  3. Auth Server validates token and returns permissions
  4. If valid → API Gateway forwards request to backend
  5. 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