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.
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.
| 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 |
- 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.
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
| 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) |
| 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 |
API Gateway = Centralized manager and router for APIs, handling security, routing, throttling, analytics, and more — simplifying client-to-microservice interactions.
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
Clients → API Gateway → Microservices
Now clients interact only with API Gateway. The gateway handles the "messy details" — clean, secure, and scalable!
Mobile/Web App → API Gateway →
→ UserService
→ ProductService
→ OrderService
→ PaymentService
| 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 |
🛒 Scenario: User opens the app to view their profile and orders.
- Client sends request to UserService → Authenticate
- Client sends request to OrderService → Authenticate again ✖️ Too many direct calls & duplicate logic
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)
| 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 |
| 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 |
- 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.?
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 🚀
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
We deploy API Gateway inside Kubernetes as a Pod (or multiple Pods) API Gateway sits in front of all microservices
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)
| 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 |
| 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 |
| 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) |
| 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 |
- 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)
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.
✅ Good for small-medium setups ✅ Fast, lightweight, easy to understand ✅ Works even outside Kubernetes (on VM/Server)
-
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
sudo apt update
sudo apt install nginxOpen Nginx config:
sudo nano /etc/nginx/sites-available/api-gatewayPaste this 👇
server {
listen 80;
location /users/ {
proxy_pass http://localhost:5001/;
}
location /products/ {
proxy_pass http://localhost:5002/;
}
}sudo ln -s /etc/nginx/sites-available/api-gateway /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginxcurl http://localhost/users/ # Routes to UserService
curl http://localhost/products/ # Routes to ProductService🎉 Done — Nginx is now your basic API Gateway!
| 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; |
✅ Production-grade API Gateway ✅ Advanced features (auth, rate-limit, analytics, plugins) ✅ Works great inside Kubernetes too
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✅ 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"curl http://localhost:8000/users # Routed to UserService
curl http://localhost:8000/products # Routed to ProductService🎉 Kong Gateway is live!
| 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" |
- Install via Helm chart
- Expose via Kubernetes LoadBalancer
- Manage routes via KongIngress CRDs
| 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 |
✅ Good for small-medium setups ✅ Fast, lightweight, easy to understand ✅ Works even outside Kubernetes (on VM/Server)
-
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
sudo apt update
sudo apt install nginxOpen Nginx config:
sudo nano /etc/nginx/sites-available/api-gatewayPaste this 👇
server {
listen 80;
location /users/ {
proxy_pass http://localhost:5001/;
}
location /products/ {
proxy_pass http://localhost:5002/;
}
}sudo ln -s /etc/nginx/sites-available/api-gateway /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginxcurl http://localhost/users/ # Routes to UserService
curl http://localhost/products/ # Routes to ProductService🎉 Done — Nginx is now your basic API Gateway!
| 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; |
✅ Production-grade API Gateway ✅ Advanced features (auth, rate-limit, analytics, plugins) ✅ Works great inside Kubernetes too
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✅ 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"curl http://localhost:8000/users # Routed to UserService
curl http://localhost:8000/products # Routed to ProductService🎉 Kong Gateway is live!
| 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" |
- Install via Helm chart
- Expose via Kubernetes LoadBalancer
- Manage routes via KongIngress CRDs
| 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 | 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) |
Client
│
[Ingress Controller]
│
┌─┴─────────────┐
Svc A (ClusterIP) Svc B (ClusterIP)
- Maps host/path → Kubernetes services
- No auth/rate limit/analytics built-in
Client
│
[API Gateway]
│
┌─┴─────────────┐
UserSvc ProductSvc OrderSvc
- Traffic control, auth, rate limiting
- Can call multiple services, do response transformations
- Often handles internal AND external traffic
| 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 |
Ingress Controller = Gate to the cluster API Gateway = Manager of API traffic
| 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 |
| 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 |
👉 Many companies combine both! Ingress Controller → forwards traffic → API Gateway
Example (Best practice):
Client
│
[NGINX Ingress Controller (TLS termination)]
│
[API Gateway (Kong)]
│
[Microservices (ClusterIP)]