0
0
Microservicessystem_design~7 mins

Why API gateways unify service access in Microservices - Why This Architecture

Choose your learning style9 modes available
Problem Statement
When multiple microservices each expose their own endpoints, clients must manage many different URLs, protocols, and authentication methods. This complexity leads to tight coupling, increased client logic, and difficulty in evolving services independently.
Solution
An API gateway acts as a single entry point that routes client requests to the appropriate microservices. It centralizes concerns like authentication, rate limiting, and protocol translation, simplifying client interactions and decoupling clients from internal service details.
Architecture
Clients
Clients
API Gateway
API Gateway
User
Service

This diagram shows clients sending requests to a single API Gateway, which routes them to multiple backend microservices like User, Order, and Payment services.

Trade-offs
✓ Pros
Simplifies client code by providing a single endpoint for all services.
Centralizes cross-cutting concerns such as authentication, logging, and rate limiting.
Enables independent evolution of backend services without impacting clients.
Improves security by hiding internal service details from clients.
✗ Cons
Introduces a single point of failure if the gateway is not highly available.
Can become a performance bottleneck under heavy load without proper scaling.
Adds operational complexity and requires careful management and monitoring.
Use when you have multiple microservices with diverse APIs and want to simplify client interactions and centralize common concerns, especially at scale beyond hundreds of services or clients.
Avoid when your system has only a few services or clients, as the added complexity and latency of a gateway may outweigh benefits.
Real World Examples
Netflix
Uses an API gateway to route client requests to various backend services, enabling device-specific optimizations and centralized authentication.
Amazon
Employs API gateways to unify access to its vast microservices ecosystem, simplifying client integration and enforcing security policies.
Uber
Uses API gateways to manage diverse service endpoints and provide a consistent interface for mobile and web clients.
Code Example
Before using an API gateway, clients must call each service separately, increasing complexity. After applying the API gateway pattern, clients call a single endpoint, and the gateway handles routing and aggregation, simplifying client code.
Microservices
### Before: Clients call services directly
import requests

user_response = requests.get('http://user-service/api/users/123')
order_response = requests.get('http://order-service/api/orders?user=123')

### After: Clients call API Gateway
import requests

gateway_response = requests.get('http://api-gateway/api/user-orders/123')

# API Gateway routes internally to user and order services and aggregates response
OutputSuccess
Alternatives
Client-side Aggregation
Clients directly call multiple services and aggregate responses themselves.
Use when: When clients are simple and few, and you want to avoid adding infrastructure complexity.
Backend for Frontend (BFF)
Separate backend services tailored for each client type instead of a single gateway.
Use when: When different clients require highly customized APIs and logic.
Summary
API gateways prevent client complexity by providing a single unified entry point to multiple microservices.
They centralize common concerns like authentication and rate limiting, improving security and maintainability.
While they add operational overhead, API gateways enable scalable and flexible microservice architectures.