📌 Reverse Proxy vs Load Balancer vs API Gateway

Core Idea:
All 3 sit between Client ↔ Server, but each solves a different infrastructure problem.


⚡ Quick Responsibility (Interview Revision)

Reverse Proxy

FeaturePurpose
SSL TerminationRemove encryption overhead
CachingServe repeated responses
CompressionReduce payload size
SecurityHide backend + filter attacks
RoutingForward requests

Load Balancer

FeaturePurpose
Traffic DistributionSpread requests
Health ChecksDetect failures
FailoverSkip unhealthy servers
Horizontal Scaling (ASG)Add more machines

API Gateway

FeaturePurpose
AuthenticationJWT / API keys
AuthorizationAccess control
Rate LimitingProtect APIs
Versioning/v1, /v2
TransformationJSON ↔ XML
AnalyticsAPI metrics

🏗️ How They Work Together (Real Architecture)

User
 ↓
CDN  → Global delivery
 ↓
API Gateway → API policies
 ↓
Load Balancer  → Traffic distribution
 ↓
Service Instances
 ↓
Reverse Proxy → Optimization & protection
 ↓
Application

🎯 Decision Rule

Need SSL / Cache / Security?
→ Reverse Proxy
 
Need Multiple Servers?
→ Load Balancer
 
Need Auth / Rate Limiting / Public APIs?
→ API Gateway

1️⃣ Reverse Proxy → Protect + Optimize Servers

What is it?

A server-side proxy that sits in front of backend servers.

Client
 ↓
Reverse Proxy
 ↓
Backend

Users never directly access backend servers.

Why Do We Need It?

Backend servers should focus only on business logic.

Move infrastructure concerns outside:

  • SSL termination
  • Caching
  • Compression
  • Security filtering
  • Static file serving

Responsibilities

FeaturePurpose
SSL TerminationRemove encryption overhead
CachingServe repeated responses
CompressionReduce payload size
SecurityHide backend + filter attacks
RoutingForward requests

Example Flow

User
 ↓
Reverse Proxy (NGINX)
 ↓
Application Server

2️⃣ Load Balancer → Scale + High Availability

What is it?

A specialized reverse proxy that distributes traffic across multiple servers.

Client
 ↓
Load Balancer
 ↓
Multiple Servers

Why Do We Need It?

A single server eventually hits limits:

  • CPU
  • Memory
  • Network connections Solution:
Add more servers
↓
Distribute traffic

Responsibilities

FeaturePurpose
Traffic DistributionSpread requests
Health ChecksDetect failures
FailoverSkip unhealthy servers
Horizontal ScalingAdd more machines

Common Algorithms

Round Robin

Req1 → A
Req2 → B
Req3 → C

Least Connections

Send traffic to least busy server

Weighted Routing

Powerful servers → Receive more traffic

IP Hash

Same user → Same server

Layer 4 vs Layer 7

Layer 4 (NLB)Layer 7 (ALB)
TCP/IPHTTP
FasterSmarter
No URL awarenessReads URL & Headers

Examples:

  • Layer 4 → TCP balancing
  • Layer 7 → /users → Cluster A

3️⃣ API Gateway → Manage APIs

What is it?

An API-aware reverse proxy.

Client
 ↓
API Gateway
 ↓
Microservices

Why Do We Need It?

Microservices duplicate infrastructure logic:

  • Authentication
  • Rate limiting
  • Monitoring
  • Transformation

Centralize everything.

Responsibilities

FeaturePurpose
AuthenticationJWT / API keys
AuthorizationAccess control
Rate LimitingProtect APIs
Versioning/v1, /v2
TransformationJSON ↔ XML
AnalyticsAPI metrics

Example

/api/users
     ↓
API Gateway
     ↓
User Service

📊 Comparison Table

FeatureReverse ProxyLoad BalancerAPI Gateway
Main GoalProtectScaleManage APIs
Multiple ServersOptionalYesUsually
SSL✅✅✅
Caching✅SometimesSometimes
Health Check❌✅✅
Traffic DistributionBasicAdvancedAPI-based
Authentication❌❌✅
Rate Limiting❌❌✅
Versioning❌❌✅