Skip to content

API Gateways — Managing and Securing APIs in Modern Microservices

An API Gateway is a centralized entry point that receives, processes, secures, and routes API requests to backend services. Instead of clients communicating directly with multiple microservices, they communicate with a single API Gateway that handles authentication, authorization, routing, rate limiting, request transformation, logging, monitoring, caching, and API versioning. API Gateways are a core component of modern microservices, Kubernetes, cloud-native applications, and DevOps platforms. Every DevOps Engineer, Platform Engineer, SRE, Cloud Engineer, and Cloud Architect should understand API Gateway architecture and operation.


Learning Path

Networking Mastery → Module 13: DevOps Networking → Lesson 9

Difficulty: Advanced

Reading Time: 230 Minutes

Course Progress

Course: Networking Mastery

Module: DevOps Networking

Lesson: 9 of 10


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand API Gateway architecture
  • Learn API request routing
  • Configure authentication and authorization
  • Implement rate limiting
  • Understand API versioning
  • Troubleshoot API Gateway issues
  • Design production-ready API platforms

Prerequisites

Complete:

Basic understanding of:

  • Microservices
  • JSON
  • OAuth
  • JSON Web Token (JWT)

Why Do We Need an API Gateway?

Imagine a client application communicating directly with:

Authentication Service

Product Service

Order Service

Payment Service

Notification Service

Problems:

  • Multiple Endpoints
  • Complex Authentication
  • No Centralized Security
  • Difficult Monitoring

An API Gateway simplifies communication.


What is an API Gateway?

An API Gateway acts as:

Single

Entry

Point

For

All

API

Requests

Clients communicate with the gateway instead of individual services.


API Gateway Architecture

Client


API Gateway


Authentication Service


Product Service


Order Service


Payment Service

The gateway routes each request to the correct backend service.


Request Flow

Client


API Gateway


Backend Service


Response


Client

The client remains unaware of the internal service architecture.


API Gateway Responsibilities

An API Gateway provides:

  • Request Routing
  • Authentication
  • Authorization
  • Rate Limiting
  • Request Validation
  • Request Transformation
  • Response Transformation
  • Logging
  • Monitoring
  • API Versioning

Request Routing

Example:

/api/users


User Service
/api/orders


Order Service
/api/payments


Payment Service

The gateway routes requests based on URL patterns.


Authentication

Before forwarding requests:

Client


Authentication


Backend

Common methods:

  • OAuth 2.0
  • OpenID Connect (OIDC)
  • JWT
  • API Keys
  • Basic Authentication

Authorization

Authentication verifies:

Who

You

Are

Authorization determines:

What

You

Can

Access

Example:

Admin


All APIs
User


Limited APIs

JWT Validation

Typical workflow:

Client


JWT Token


API Gateway


Backend

The gateway validates the token before forwarding the request.


API Keys

Clients receive:

API

Key

Every request includes:

X-API-Key

The gateway verifies the key before processing the request.


Rate Limiting

Protect APIs.

Example:

100 Requests

Per Minute

Per Client

Excess requests receive:

HTTP

429

Too Many Requests

Request Validation

Gateway validates:

  • Headers
  • Query Parameters
  • JSON Schema
  • Required Fields
  • Request Size

Invalid requests are rejected immediately.


Request Transformation

Incoming request:

{
  "username": "john"
}

Gateway transforms it into the format expected by the backend service if required.

Useful for:

  • Legacy Systems
  • API Version Compatibility

Response Transformation

Backend returns:

{
  "first_name": "John",
  "last_name": "Doe"
}

Gateway transforms it into the client-facing response format when necessary.


API Versioning

Example:

/v1/products
/v2/products

Older clients continue working while new versions are introduced.


Caching

Frequently requested responses:

Client


Gateway Cache


Response

Benefits:

  • Faster APIs
  • Reduced Backend Load
  • Lower Latency

Load Balancing

Multiple backend services:

API Gateway


Service A


Service B


Service C

Traffic is distributed automatically.


Service Discovery

Rather than hardcoding IP addresses:

Gateway


Service Registry


Backend Service

The gateway dynamically discovers service locations.


Circuit Breaker

If a backend fails:

Gateway


Circuit Open


Fallback Response

Prevents cascading failures.


Logging

Gateway logs:

  • Client IP
  • Request Path
  • Status Code
  • Response Time
  • User Identity

Useful for:

  • Auditing
  • Troubleshooting
  • Analytics

Monitoring

Monitor:

  • Requests Per Second
  • Error Rate
  • Latency
  • Response Time
  • Backend Health

Common integrations:

  • Prometheus
  • Grafana
  • Datadog
  • Cloud Monitoring

API Gateway in Kubernetes

Architecture:

Internet


Load Balancer


API Gateway


Services


Pods

The gateway communicates with Kubernetes Services rather than individual Pods.


API Gateway in Microservices

Mobile App


API Gateway


Authentication


Orders


Inventory


Payments


Notifications

Clients interact with one endpoint while the gateway coordinates backend communication.


Popular API Gateways

Examples:

  • Kong
  • NGINX
  • Traefik
  • Envoy Gateway
  • Apache APISIX
  • AWS API Gateway
  • Azure API Management
  • Google API Gateway

Production Architecture

Users


CDN


Load Balancer


API Gateway


Microservices


Database

This architecture provides:

  • Security
  • Scalability
  • High Availability
  • Centralized API Management

Security Best Practices

  • Enforce HTTPS.
  • Validate JWT tokens.
  • Apply Role-Based Access Control (RBAC).
  • Enable rate limiting.
  • Validate request payloads.
  • Log every API request.
  • Protect sensitive endpoints.
  • Keep gateway software updated.

Troubleshooting API Gateway

Verify API.

curl https://api.company.com/users

Inspect headers.

curl -I https://api.company.com

Verify DNS.

dig api.company.com

Check TLS.

openssl s_client -connect api.company.com:443

Review gateway logs and backend service logs.


Common Problems

Problem Possible Cause
401 Unauthorized Invalid Authentication
403 Forbidden Authorization Failure
404 Not Found Incorrect Route
429 Too Many Requests Rate Limit Exceeded
502 Bad Gateway Backend Unavailable
504 Gateway Timeout Backend Response Too Slow

CLI Examples

Test API.

curl https://api.company.com/users

Send JWT.

curl \
-H "Authorization: Bearer TOKEN" \
https://api.company.com/users

Inspect headers.

curl -I https://api.company.com

Verify DNS.

dig api.company.com

Hands-on Lab

Task 1

Deploy an API Gateway.

Configure routing for:

  • User Service
  • Product Service
  • Order Service

Task 2

Enable JWT authentication.

Verify authenticated and unauthenticated requests.


Task 3

Configure rate limiting.

Generate repeated API requests.

Observe:

429 Too Many Requests

Task 4

Enable request logging.

Review gateway logs.


Task 5

Configure API versioning.

Create:

/v1


/v2

Verify both versions function correctly.


Task 6

Deploy the API Gateway in Kubernetes.

Expose it through a LoadBalancer Service.


Task 7

Simulate a backend service failure.

Observe gateway behavior and implement a fallback response if supported.


Task 8

Draw the following architecture:

Client


CDN


Load Balancer


API Gateway


Authentication


Microservices


Database

Explain how requests are authenticated, routed, monitored, and returned to the client.


API Gateway vs Reverse Proxy

API Gateway Reverse Proxy
API Management Web Traffic Routing
Authentication Basic Routing
Rate Limiting TLS Termination
API Versioning URL Routing
Request Validation Caching & Compression

API Gateway vs Load Balancer

API Gateway Load Balancer
Understands APIs Distributes Traffic
JWT Validation Health Checks
Request Transformation Session Persistence
API Security High Availability
API Policies Traffic Distribution

Common Mistakes

❌ Exposing microservices directly.

✅ Route requests through the API Gateway.


❌ Disabling authentication.

✅ Enforce authentication for protected APIs.


❌ Ignoring rate limits.

✅ Apply throttling to prevent abuse.


❌ Hardcoding backend endpoints.

✅ Use service discovery.


❌ Not monitoring API performance.

✅ Track latency, errors, and request volume.


Interview Questions

Beginner

  1. What is an API Gateway?
  2. Why do we use API Gateways?
  3. What is rate limiting?
  4. What is JWT authentication?

Intermediate

  1. Compare an API Gateway and a Reverse Proxy.
  2. Explain API versioning.
  3. How does an API Gateway improve security?
  4. What is request transformation?

Architect Level

  1. Design an API platform for a large microservices architecture.
  2. Explain how API Gateways integrate with Kubernetes.
  3. How would you troubleshoot intermittent API failures through an API Gateway?

Summary

In this lesson, you learned:

  • API Gateway Architecture
  • Request Routing
  • Authentication
  • Authorization
  • JWT Validation
  • API Keys
  • Rate Limiting
  • Request Transformation
  • API Versioning
  • Production API Management

API Gateways provide a secure, centralised layer for managing communication between clients and backend services. By combining routing, authentication, authorization, monitoring, rate limiting, and service discovery, they simplify microservices architectures while improving security, scalability, and operational visibility.


Key Takeaways

  • API Gateways provide a single entry point for API traffic.
  • They centralize authentication, authorization, and routing.
  • Features such as rate limiting, request validation, and API versioning improve reliability and maintainability.
  • API Gateways integrate seamlessly with Kubernetes and microservices.
  • Monitor API performance continuously to ensure reliability.
  • Protect backend services by exposing only the API Gateway to clients.

What's Next?

Service Discovery

In the next lesson, you'll learn about Service Discovery.

You'll explore:

  • Service Discovery Fundamentals
  • Dynamic Service Registration
  • Service Registry
  • DNS-Based Discovery
  • Client-Side vs Server-Side Discovery
  • Kubernetes Service Discovery
  • Production Service Discovery Best Practices

By the end of the lesson, you'll understand how distributed applications automatically locate and communicate with services in modern cloud-native environments.