An API Gateway is a server (or service) that acts as a single entry point for all client requests to your backend services (like microservices, databases, legacy systems, etc.). Think of it as a traffic manager or doorman for APIsβit handles incoming API calls, routes them to the correct backend services, applies policies, and sends the response back to the client.
πͺ Simple Analogy:
If your backend has 20 different microservices, instead of exposing all 20 endpoints directly to users (which is messy and insecure), you expose one API Gateway. The API Gateway figures out where to forward the request, handles security, and combines responses if needed.
βοΈ What an API Gateway does (Responsibilities)
| Feature | Description |
|---|---|
| Request Routing | Forwards incoming requests to the correct backend service |
| Authentication & Authorization | Validates tokens (e.g., OAuth, JWT), API keys, and ensures the client is allowed to access |
| Rate Limiting & Throttling | Prevents abuse by limiting how many requests a client can make |
| Load Balancing | Distributes requests among multiple backend instances |
| Caching | Stores responses to reduce load and improve performance |
| Request & Response Transformation | Modifies request or response format (e.g., from JSON to XML) |
| Monitoring & Analytics | Tracks API usage, logs requests, and helps in debugging |
| Security | Protects backend services (e.g., SSL termination, IP whitelisting, data masking) |
| API Versioning | Allows clients to access different versions of APIs |
π Why is API Gateway used?
- Single entry point β Simplifies communication between clients and multiple backend services.
- Centralized security β You donβt have to implement auth, logging, rate-limiting in each service.
- Decouples clients from services β Backend services can evolve (change URLs, merge, split) without impacting clients directly.
- Improves performance β Through caching, request batching, and load balancing.
ποΈ Example Workflow (API Gateway flow)
Client β API Gateway β Microservice 1
β Microservice 2
β Database
β External APIs
- Client sends a request to /user-profile
- API Gateway authenticates the client and routes the request to UserService
- UserService fetches data and sends it back to API Gateway
- API Gateway returns the response to the client
π Popular API Gateway Tools
| Tool | Type |
|---|---|
| Apigee | Managed API Gateway (by Google) |
| AWS API Gateway | Managed (AWS) |
| Kong | Open-source / Enterprise |
| Nginx | Can be configured as a gateway/reverse proxy |
| Zuul (Netflix) | Java-based |
| Istio | Service mesh with gateway capabilities |
| Azure API Management | Managed (Azure) |
β‘ API Gateway vs Load Balancer vs Reverse Proxy (Quick view)
| Feature | API Gateway | Load Balancer | Reverse Proxy |
|---|---|---|---|
| Focus | API management | Distribute load | Forward requests |
| Handles auth, rate limiting, etc.? | β Yes | β No | β No |
| Understands API-level routing? | β Yes | β No | β Limited |
| Example | Apigee, AWS API Gateway | Nginx LB, AWS ELB | Nginx, HAProxy |
π In short
API Gateway = Centralized manager and router for APIs, handling security, routing, throttling, analytics, and more β simplifying client-to-microservice interactions.
π’ Typical Microservices Architecture (without API Gateway)
Clients β Multiple microservices
Problems:
- Each microservice exposes its own endpoint
- Clients have to know URLs of all services
-
Each microservice has to implement:
- Authentication
- Rate limiting
- Caching
- Monitoring β Duplication, Complexity, Hard to maintain
β With API Gateway (Recommended Approach)
Clients β API Gateway β Microservices
Now clients interact only with API Gateway. The gateway handles the βmessy detailsβ β clean, secure, and scalable!
π Typical Company Use Case Example (E-commerce)
Architecture:
Mobile/Web App β API Gateway β
β UserService
β ProductService
β OrderService
β PaymentService
β¨ Responsibilities of API Gateway in this setup
| Function | Example |
|---|---|
| Routing | /users/123 β UserService/products/567 β ProductService |
| Authentication | Validates JWT token before forwarding request |
| Rate Limiting | Limits users to 1000 requests/day |
| Caching | Caches /products list responses for 5 min |
| Logging & Monitoring | Logs API usage for analytics/debugging |
| Transformations | Converts XML backend response to JSON if client expects JSON |
| Aggregation | Combines responses from multiple microservices into one |
π¦ How does it work in practice? (Example Flow)
π Scenario: User opens the app to view their profile and orders.
Without API Gateway
- Client sends request to UserService β Authenticate
- Client sends request to OrderService β Authenticate again βοΈ Too many direct calls & duplicate logic
With API Gateway
Client β API Gateway
| Step | What happens |
|---|---|
| 1 | Client sends GET /my-dashboard |
| 2 | API Gateway authenticates JWT token |
| 3 | API Gateway routes request: β Calls UserService /users/123β Calls OrderService /orders/user/123 |
| 4 | API Gateway aggregates both responses |
| 5 | API Gateway sends single combined response to client |
β Cleaner β Secure β Faster (fewer round-trips)
π Benefits for companies
| Benefit | Why it matters |
|---|---|
| Simplified client communication | Clients donβt need to know 10+ microservice URLs |
| Centralized security | Easier to enforce auth, rate limits at one place |
| Flexible backend evolution | Backend services can change without breaking clients |
| Faster delivery | Common API tasks handled by gateway β less work for dev teams |
| Improved monitoring | Company can analyze API usage patterns easily |
π₯ Real-World Examples
| Company | Gateway Used | Example |
|---|---|---|
| Netflix | Zuul, then migrated to Spring Cloud Gateway | Routes billions of API requests/day |
| Amazon | Custom internal gateway (like AWS API Gateway) | Exposes services to mobile apps securely |
| Airbnb | Nginx + custom gateway | Centralized API routing & auth |
| Apigee | API management for internal & external developers |
β οΈ Things companies consider when choosing API Gateway
- Open source vs Managed service (e.g., Kong vs Apigee)
- Cost Managed gateways (like Apigee) can be expensive
- Performance & Scalability High-traffic systems need performant gateways
- Extensibility Custom plugins, custom auth policies, etc.
- Integration with existing infra Does it work with Kubernetes, cloud provider, etc.?
β Summary
An API Gateway makes microservices architectures scalable, secure, and maintainable β By centralizing routing, security, and API management, companies reduce complexity for clients and backend teams.
Perfect β letβs see how API Gateway + Kubernetes works together! This is very relevant because modern companies deploy their microservices in Kubernetes clusters. Iβll explain step-by-step, show architecture, and share real-world examples. Letβs go π
π οΈ Kubernetes Microservices Setup (without API Gateway)
Clients β Exposed Kubernetes Services (via Ingress)
Problems:
- Each microservice (deployed as a Pod) needs its own Ingress rule
- Exposes too many services to outside world
- No centralized auth, rate limiting, or monitoring
β With API Gateway (Recommended)
We deploy API Gateway inside Kubernetes as a Pod (or multiple Pods) API Gateway sits in front of all microservices
ποΈ Architecture Diagram
Client
β
βΌ
[API Gateway Pod(s)] (Ingress + Gateway Pod exposed to internet)
β
βββ UserService (K8s Pod + ClusterIP Service)
βββ ProductService (K8s Pod + ClusterIP Service)
βββ OrderService (K8s Pod + ClusterIP Service)
βββ PaymentService (K8s Pod + ClusterIP Service)
βοΈ How does this work? (Step-by-step)
| Step | Description |
|---|---|
| 1 | API Gateway (like Kong, Nginx, Ambassador, Istio Ingress Gateway) is deployed inside Kubernetes |
| 2 | API Gateway exposes one public endpoint using Kubernetes Ingress or LoadBalancer |
| 3 | All external traffic hits API Gateway Pod first |
| 4 | Gateway authenticates client, applies rate limits, logs request |
| 5 | Gateway forwards request internally to Kubernetes services (via ClusterIP) |
| 6 | Gateway aggregates/compresses/caches response (optional) |
| 7 | Gateway returns response to client |
π Real-world Example (E-commerce app on Kubernetes)
| Component | Deployment |
|---|---|
| API Gateway | Kong Gateway deployed as K8s Deployment Exposed via LoadBalancer Service |
| UserService | Deployed as user-deployment.yaml with ClusterIP Service |
| ProductService | Deployed as product-deployment.yaml with ClusterIP Service |
| API Routing | Kong routes /users, /products, /orders internally to services |
| Security | Kong uses JWT auth plugin to authenticate users |
| Rate Limiting | Kong rate limits /products API to 100 req/min |
π Popular API Gateways with Kubernetes support
| API Gateway | How it integrates with K8s |
|---|---|
| Kong Gateway | Helm charts, CRDs, native K8s Ingress |
| NGINX | NGINX Ingress Controller (acts as API Gateway + Ingress) |
| Istio Gateway | Envoy proxy based, advanced traffic management |
| Ambassador | Built specifically for K8s, CRDs for API management |
| AWS API Gateway | Can route to services in Amazon EKS (Kubernetes) |
π Benefits of API Gateway in Kubernetes
| Benefit | Why it matters |
|---|---|
| Single public entry point | Only API Gateway is exposed β rest of services private |
| Centralized security | No need to add auth/rate limit on each service |
| Service discovery | Automatically routes to K8s services (ClusterIP) |
| Scalability | Gateway scales horizontally with Kubernetes deployments |
| Resiliency | Circuit breakers, retries, timeouts centralized |
| Developer-friendly | Use Kubernetes CRDs to configure routes/plugins |
β οΈ Things to keep in mind
- Resource usage β API Gateway Pod should scale with traffic
- TLS termination β Let Gateway handle HTTPS
- RBAC & Secrets β Secure API Gateway configs
- Observability β Enable logging + monitoring on Gateway
- Multi-cluster? β Use global API Gateway (Istio or Kong + Kuma)
β Summary
API Gateway + Kubernetes = Scalable, Secure, Centralized Microservices Entry Point
You deploy API Gateway inside K8s β expose ONE public endpoint β route/manage ALL microservices traffic from there. This is how modern companies (Netflix, Airbnb, Stripe, etc.) build robust platforms.
π₯ 1οΈβ£ NGINX as API Gateway (Step-by-Step)
β Good for small-medium setups β Fast, lightweight, easy to understand β Works even outside Kubernetes (on VM/Server)
π οΈ Setup Example
-
You have 2 microservices:
- UserService (
localhost:5001) - ProductService (
localhost:5002)
- UserService (
-
You want API Gateway to expose:
/usersβ forwards to UserService/productsβ forwards to ProductService
βοΈ Step 1 β Install Nginx
sudo apt update
sudo apt install nginx
βοΈ Step 2 β Configure Nginx as reverse proxy
Open Nginx config:
sudo nano /etc/nginx/sites-available/api-gateway
Paste this π
server {
listen 80;
location /users/ {
proxy_pass http://localhost:5001/;
}
location /products/ {
proxy_pass http://localhost:5002/;
}
}
βοΈ Step 3 β Enable config & restart Nginx
sudo ln -s /etc/nginx/sites-available/api-gateway /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
βοΈ Step 4 β Test
curl http://localhost/users/ # Routes to UserService
curl http://localhost/products/ # Routes to ProductService
π Done β Nginx is now your basic API Gateway!
β¨ Optional Enhancements
| Feature | Config snippet |
|---|---|
| Auth | auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd; |
| Rate limiting | limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s; |
| Caching | proxy_cache my_cache; |
| TLS/HTTPS | listen 443 ssl; ssl_certificate /path/to/cert; |
π₯ 2οΈβ£ Kong Gateway (Step-by-Step)
β Production-grade API Gateway β Advanced features (auth, rate-limit, analytics, plugins) β Works great inside Kubernetes too
π Kong Gateway β Outside Kubernetes (quick demo)
βοΈ Step 1 β Install Kong (Docker)
docker network create kong-net
docker run -d --name kong-database \
--network=kong-net \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
-e "POSTGRES_PASSWORD=kong" \
postgres:13
docker run --rm --network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
kong:3.6 kong migrations bootstrap
docker run -d --name kong \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
-p 8000:8000 -p 8001:8001 \
kong:3.6
βοΈ Step 2 β Configure Routes & Services
β
Expose UserService (on localhost:5001)
curl -i -X POST http://localhost:8001/services/ \
--data "name=users-service" \
--data "url=http://host.docker.internal:5001"
curl -i -X POST http://localhost:8001/services/users-service/routes \
--data "paths[]=/users"
β
Expose ProductService (on localhost:5002)
curl -i -X POST http://localhost:8001/services/ \
--data "name=products-service" \
--data "url=http://host.docker.internal:5002"
curl -i -X POST http://localhost:8001/services/products-service/routes \
--data "paths[]=/products"
βοΈ **Step 3 β Test
curl http://localhost:8000/users # Routed to UserService
curl http://localhost:8000/products # Routed to ProductService
π Kong Gateway is live!
β¨ Optional Features (1-liner plugins)
| Feature | Command |
|---|---|
| JWT Auth | curl -X POST http://localhost:8001/services/users-service/plugins --data "name=jwt" |
| Rate limiting | curl -X POST http://localhost:8001/services/users-service/plugins --data "name=rate-limiting" --data "config.minute=5" |
| Monitoring | curl -X POST http://localhost:8001/plugins/ --data "name=prometheus" |
π Bonus: Kong Gateway in Kubernetes
- Install via Helm chart
- Expose via Kubernetes LoadBalancer
- Manage routes via KongIngress CRDs
-
β Summary
| Gateway | Pros | Cons |
|---|---|---|
| NGINX | Simple, Fast, Lightweight, Easy | No built-in auth/rate-limit/analytics (need manual config) |
| Kong | Enterprise-grade, Plugins, Analytics, API management UI | Heavier, Needs DB (Postgres), Slightly more complex |
π₯ 1οΈβ£ NGINX as API Gateway (Step-by-Step)
β Good for small-medium setups β Fast, lightweight, easy to understand β Works even outside Kubernetes (on VM/Server)
π οΈ Setup Example
-
You have 2 microservices:
- UserService (
localhost:5001) - ProductService (
localhost:5002)
- UserService (
-
You want API Gateway to expose:
/usersβ forwards to UserService/productsβ forwards to ProductService
βοΈ Step 1 β Install Nginx
sudo apt update
sudo apt install nginx
βοΈ Step 2 β Configure Nginx as reverse proxy
Open Nginx config:
sudo nano /etc/nginx/sites-available/api-gateway
Paste this π
server {
listen 80;
location /users/ {
proxy_pass http://localhost:5001/;
}
location /products/ {
proxy_pass http://localhost:5002/;
}
}
βοΈ Step 3 β Enable config & restart Nginx
sudo ln -s /etc/nginx/sites-available/api-gateway /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
βοΈ Step 4 β Test
curl http://localhost/users/ # Routes to UserService
curl http://localhost/products/ # Routes to ProductService
π Done β Nginx is now your basic API Gateway!
β¨ Optional Enhancements
| Feature | Config snippet |
|---|---|
| Auth | auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd; |
| Rate limiting | limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s; |
| Caching | proxy_cache my_cache; |
| TLS/HTTPS | listen 443 ssl; ssl_certificate /path/to/cert; |
π₯ 2οΈβ£ Kong Gateway (Step-by-Step)
β Production-grade API Gateway β Advanced features (auth, rate-limit, analytics, plugins) β Works great inside Kubernetes too
π Kong Gateway β Outside Kubernetes (quick demo)
βοΈ Step 1 β Install Kong (Docker)
docker network create kong-net
docker run -d --name kong-database \
--network=kong-net \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
-e "POSTGRES_PASSWORD=kong" \
postgres:13
docker run --rm --network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
kong:3.6 kong migrations bootstrap
docker run -d --name kong \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
-p 8000:8000 -p 8001:8001 \
kong:3.6
βοΈ Step 2 β Configure Routes & Services
β
Expose UserService (on localhost:5001)
curl -i -X POST http://localhost:8001/services/ \
--data "name=users-service" \
--data "url=http://host.docker.internal:5001"
curl -i -X POST http://localhost:8001/services/users-service/routes \
--data "paths[]=/users"
β
Expose ProductService (on localhost:5002)
curl -i -X POST http://localhost:8001/services/ \
--data "name=products-service" \
--data "url=http://host.docker.internal:5002"
curl -i -X POST http://localhost:8001/services/products-service/routes \
--data "paths[]=/products"
βοΈ **Step 3 β Test
curl http://localhost:8000/users # Routed to UserService
curl http://localhost:8000/products # Routed to ProductService
π Kong Gateway is live!
β¨ Optional Features (1-liner plugins)
| Feature | Command |
|---|---|
| JWT Auth | curl -X POST http://localhost:8001/services/users-service/plugins --data "name=jwt" |
| Rate limiting | curl -X POST http://localhost:8001/services/users-service/plugins --data "name=rate-limiting" --data "config.minute=5" |
| Monitoring | curl -X POST http://localhost:8001/plugins/ --data "name=prometheus" |
π Bonus: Kong Gateway in Kubernetes
- Install via Helm chart
- Expose via Kubernetes LoadBalancer
-
Manage routes via KongIngress CRDs
β Summary
| Gateway | Pros | Cons |
|---|---|---|
| NGINX | Simple, Fast, Lightweight, Easy | No built-in auth/rate-limit/analytics (need manual config) |
| Kong | Enterprise-grade, Plugins, Analytics, API management UI | Heavier, Needs DB (Postgres), Slightly more complex |
π API Gateway vs Ingress Controller β Clear Difference
| Β | API Gateway | Ingress Controller |
|---|---|---|
| Primary Purpose | Manage API traffic (L7, HTTP/REST/gRPC) Security + Routing + API management |
Expose Kubernetes services to external users (mainly L7 HTTP/S routes) |
| Scope | API management for internal + external clients (auth, rate limiting, analytics) | Ingress traffic only (external β K8s services) No deep API features |
| Typical Usage | Centralize microservices traffic management | Just expose K8s services to outside world |
| Features | Routing, auth, rate limiting, caching, logging, transformations, API versioning | Basic routing (hostname/path based), TLS termination |
| Authentication | Built-in (JWT, OAuth2, API key plugins) | Usually none (or very minimal via annotations) |
| Analytics & Monitoring | Built-in (dashboards, usage stats, logging) | None (need external Prometheus/Grafana setup) |
| Examples | Kong, Apigee, AWS API Gateway, Istio Gateway, Ambassador | NGINX Ingress Controller, Traefik, HAProxy Ingress |
| Traffic Direction | External + Internal (client β gateway β internal services) | Only external β K8s services |
| Cost/Complexity | Heavier, more features, more resource usage | Lightweight, simple to configure |
| Granularity | API-level control (limit /users API to 100 req/sec) | Service-level routing (route foo.com β svc A) |
ποΈ Architecture Diagrams
π Ingress Controller (simple)
Client
β
[Ingress Controller]
β
βββ΄ββββββββββββββ
Svc A (ClusterIP) Svc B (ClusterIP)
- Maps host/path β Kubernetes services
- No auth/rate limit/analytics built-in
π API Gateway (richer)
Client
β
[API Gateway]
β
βββ΄ββββββββββββββ
UserSvc ProductSvc OrderSvc
- Traffic control, auth, rate limiting
- Can call multiple services, do response transformations
- Often handles internal AND external traffic
β Real-world Analogy (Simple)
| Scenario | Which to use? |
|---|---|
| I just want to expose my frontend app (React/Angular) or service API to internet β no API policies | Ingress Controller |
| I want to expose multiple microservices APIs with security, API versioning, rate limits | API Gateway |
β‘ Quick thumb rule
Ingress Controller = Gate to the cluster API Gateway = Manager of API traffic
π Real Company Usage Example
| Company | How they use |
|---|---|
| Netflix | API Gateway (Zuul/Envoy) to manage all microservice APIs (with auth, rate limit, fallback) |
| Small startup | NGINX Ingress Controller to expose frontend + backend app |
| Stripe | API Gateway to manage secure APIs for developers |
β Summary
| Use case | Use Ingress if | Use API Gateway if |
|---|---|---|
| Simple route | Just expose 1β2 services | Need smart API routing |
| Security | Handled elsewhere | Want built-in auth/rate-limit |
| Analytics | Not required | Want usage stats/logging |
| Complexity | Keep things simple | Handle complex API traffic |
π’ Final Note (Very Important)
π Many companies combine both! Ingress Controller β forwards traffic β API Gateway
Example (Best practice):
Client
β
[NGINX Ingress Controller (TLS termination)]
β
[API Gateway (Kong)]
β
[Microservices (ClusterIP)]