VibeKoding / Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat / Principles of Gateways and Reverse ProxiesPrinciples of Gateways and Reverse Proxies
VK

Principles of Gateways and Reverse ProxiesPrinciples of Gateways and Reverse Proxies

๐Ÿ“š Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat ๐ŸŒ Dual Bahasa (ID / EN) โšก VibeKoding Native

Ensiklopedia VibeKoding: Principles of Gateways and Reverse Proxies.Ensiklopedia VibeKoding: Principles of Gateways and Reverse Proxies.

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

In high-concurrency internet architectures, how do you route traffic to the right service safely and efficiently? Reverse proxies solve "how to distribute traffic," and API gateways solve "how to process requests." This article uses real-world analogies (reception desk, security system, intelligent routing) to explore the design philosophy and engineering practices of gateways.In high-concurrency internet architectures, how do you route traffic to the right service safely and efficiently? Reverse proxies solve "how to distribute traffic," and API gateways solve "how to process requests." This article uses real-world analogies (reception desk, security system, intelligent routing) to explore the design philosophy and engineering practices of gateways.

------

1. Motivation for the "Gateway"1. Motivation for the "Gateway"

1.1 A Real-World Case: The Architecture Evolution of an E-Commerce Platform1.1 A Real-World Case: The Architecture Evolution of an E-Commerce Platform

An e-commerce platform encountered serious architectural problems during rapid business growth:An e-commerce platform encountered serious architectural problems during rapid business growth:

Scenario:Scenario:

CODE
Phase 1: Directly Exposing Services Client โ†’ Directly calls User Service, Order Service, Payment Service... โ†“ Problem 1: Service IPs are exposed โ€” security risk Problem 2: No unified authentication or rate limiting Problem 3: Adding a new service requires modifying client configuration
โš ๏ธ Catatan Keamanan / Peringatanโš ๏ธ Warning / Security Note

- Security risk: All service IPs are exposed and vulnerable to attacks - Redundant functionality: Every service must implement authentication, rate limiting, and logging - Scaling difficulty: Adding a new service requires changes to all clients - Protocol chaos: Some services use HTTP, others gRPC โ€” clients must adapt to all- Security risk: All service IPs are exposed and vulnerable to attacks - Redundant functionality: Every service must implement authentication, rate limiting, and logging - Scaling difficulty: Adding a new service requires changes to all clients - Protocol chaos: Some services use HTTP, others gRPC โ€” clients must adapt to all

Improved Architecture (with Gateway):Improved Architecture (with Gateway):

CODE
Client โ†’ API Gateway (Nginx/Kong) โ†’ Internal Services โ†“ Unified authentication, rate limiting, routing โ†“ Client only knows the gateway address
๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

- Security: Real service IPs are hidden; only the gateway is exposed externally - Consolidation: Authentication, rate limiting, and logging are handled centrally - Easy scaling: Adding a new service only requires configuring a route on the gateway - Protocol unification: HTTP externally, gRPC internally- Security: Real service IPs are hidden; only the gateway is exposed externally - Consolidation: Authentication, rate limiting, and logging are handled centrally - Easy scaling: Adding a new service only requires configuring a route on the gateway - Protocol unification: HTTP externally, gRPC internally

1.2 A Real-Life Analogy for Gateways1.2 A Real-Life Analogy for Gateways

The Reception DeskThe Reception Desk

Imagine visiting a large company:Imagine visiting a large company:

An API gateway is the "reception desk" of a system:An API gateway is the "reception desk" of a system:

------

2. Overview of a Reverse Proxy2. Overview of a Reverse Proxy

2.1 Forward Proxy vs. Reverse Proxy2.1 Forward Proxy vs. Reverse Proxy

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

Forward Proxy: - Deployed on the client side - Accesses external resources on behalf of the client - Typical applications: VPNs, circumvention tools - Example: In a corporate network, you access the internet through a proxy Reverse Proxy: - Deployed on the server side - Receives client requests and forwards them to internal services - Clients only know the proxy exists, not the real servers - Examples: Nginx, HAProxyForward Proxy: - Deployed on the client side - Accesses external resources on behalf of the client - Typical applications: VPNs, circumvention tools - Example: In a corporate network, you access the internet through a proxy Reverse Proxy: - Deployed on the server side - Receives client requests and forwards them to internal services - Clients only know the proxy exists, not the real servers - Examples: Nginx, HAProxy

Comparison:Comparison:

DimensionForward ProxyReverse Proxy
Deployment sideClient sideServer side
ServesClientsServers
Typical useVPN, circumventionLoad balancing, gateway
TransparencyServer sees the proxy IPClient sees the proxy IP
PurposeHide real client, accelerate accessHide real server, load balance

2.2 Core Value of a Reverse Proxy2.2 Core Value of a Reverse Proxy

๐Ÿ“– Konsep Penting๐Ÿ“– Core Concept

Distributes traffic across multiple backend servers to avoid single-point overload. `` Client โ†“ Nginx (Reverse Proxy) โ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Server 1 โ”‚ Server 2 โ”‚ Server 3 โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``Distributes traffic across multiple backend servers to avoid single-point overload. `` Client โ†“ Nginx (Reverse Proxy) โ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Server 1 โ”‚ Server 2 โ”‚ Server 3 โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``

๐Ÿ“– Konsep Penting๐Ÿ“– Core Concept

Hides real server IPs to prevent direct attacks. Security is enforced at the proxy layer. `` Client โ†’ Only sees Nginx's IP Real servers โ†’ Only on the internal network, inaccessible externally ``Hides real server IPs to prevent direct attacks. Security is enforced at the proxy layer. `` Client โ†’ Only sees Nginx's IP Real servers โ†’ Only on the internal network, inaccessible externally ``

๐Ÿ“– Konsep Penting๐Ÿ“– Core Concept

Handles HTTPS encryption/decryption at the proxy layer; backend services use HTTP, reducing backend computational overhead. `` HTTPS Client โ†’ Nginx (encrypt/decrypt) โ†’ HTTP Backend Services โ†‘ SSL termination point ``Handles HTTPS encryption/decryption at the proxy layer; backend services use HTTP, reducing backend computational overhead. `` HTTPS Client โ†’ Nginx (encrypt/decrypt) โ†’ HTTP Backend Services โ†‘ SSL termination point ``

------

3. Nginx: Method for handling Millions of Concurrent Connections3. Nginx: Method for handling Millions of Concurrent Connections

3.1 Master-Worker Process Model3.1 Master-Worker Process Model

Nginx uses a multi-process architecture, not multi-threaded:Nginx uses a multi-process architecture, not multi-threaded:

Master Process (Manager):Master Process (Manager):

Worker Processes (Workers):Worker Processes (Workers):

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

- Strong isolation: One Worker crash does not affect other Workers - Full multi-core utilization: Each Worker runs independently - Avoids multi-threading complexity: No need to deal with locks, race conditions, etc.- Strong isolation: One Worker crash does not affect other Workers - Full multi-core utilization: Each Worker runs independently - Avoids multi-threading complexity: No need to deal with locks, race conditions, etc.

3.2 Event-Driven + Async Non-Blocking3.2 Event-Driven + Async Non-Blocking

This is the core secret of Nginx's high performance:This is the core secret of Nginx's high performance:

Traditional Apache (multi-process/thread model):Traditional Apache (multi-process/thread model):

Nginx (event-driven model):Nginx (event-driven model):

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

- Apache: A restaurant where every diner gets a dedicated waiter (process); many diners require many waiters - Nginx: One super-waiter serving all diners simultaneously, going to whoever needs service rather than standing next to a single diner- Apache: A restaurant where every diner gets a dedicated waiter (process); many diners require many waiters - Nginx: One super-waiter serving all diners simultaneously, going to whoever needs service rather than standing next to a single diner

------

4. Overview of an API Gateway4. Overview of an API Gateway

4.1 Motivation for needing an API Gateway4.1 Motivation for needing an API Gateway

Imagine a system without a gateway:Imagine a system without a gateway:

โš ๏ธ Catatan Keamanan / Peringatanโš ๏ธ Warning / Security Note

- Client complexity: Must configure multiple service addresses - Redundant functionality: Every service must implement authentication and rate limiting - Protocol chaos: Clients must adapt to multiple protocols - Upgrade difficulty: Service upgrades force client-side changes- Client complexity: Must configure multiple service addresses - Redundant functionality: Every service must implement authentication and rate limiting - Protocol chaos: Clients must adapt to multiple protocols - Upgrade difficulty: Service upgrades force client-side changes

With an API gateway:With an API gateway:

4.2 Core Features of an API Gateway4.2 Core Features of an API Gateway

FeatureDescriptionTypical Scenario
Route forwardingForwards requests to different services based on URL, headers, etc./api/users โ†’ User Service, /api/orders โ†’ Order Service
Load balancingDistributes traffic when a service has multiple instancesUser Service has 3 instances, round-robin request distribution
AuthenticationCentrally validates JWT, OAuth tokensUnauthenticated users cannot access /api/admin
Rate limiting & circuit breakingControls traffic caps to prevent service overloadMax 1000 requests/second; beyond that returns 429
Protocol translationHTTP externally, can translate to gRPC internallyClient uses HTTP, gateway translates to gRPC for internal calls
Canary releaseRoutes a portion of traffic to a new version by header or ratio5% of users experience the new version, 95% use the old
Logging & monitoringCentrally records request logs for analysis and troubleshootingRecord request latency, status codes, response sizes

------

5. Gateway in Practice: Approach to building a Complete Gateway Architecture5. Gateway in Practice: Approach to building a Complete Gateway Architecture

5.1 Full Architecture Diagram5.1 Full Architecture Diagram

CODE
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Client (Browser/App) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ HTTPS โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Outer Layer: CDN + WAF โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ CDN (Content Delivery Network) โ”‚ โ”‚ โ”‚ โ”‚ - Static asset caching (images, CSS, JS) โ”‚ โ”‚ โ”‚ โ”‚ - Nearby access, reduced latency โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ WAF (Web Application Firewall) โ”‚ โ”‚ โ”‚ โ”‚ - Protection against SQL injection, XSS attacks โ”‚ โ”‚ โ”‚ โ”‚ - Block malicious bots and crawlers โ”‚ โ”‚ โ”‚ โ”‚ - CC attack protection โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Middle Layer: API Gateway (Nginx/Kong) โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Layer 1: SSL Termination + Security โ”‚ โ”‚ โ”‚ โ”‚ - HTTPS / TLS 1.3 โ”‚ โ”‚ โ”‚ โ”‚ - HSTS, security response headers โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Layer 2: Authentication & Authorization โ”‚ โ”‚ โ”‚ โ”‚ - JWT Token validation โ”‚ โ”‚ โ”‚ โ”‚ - OAuth 2.0 / SSO integration โ”‚ โ”‚ โ”‚ โ”‚ - API Key management โ”‚ โ”‚ โ”‚ โ”‚ - Permission checks (RBAC) โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Layer 3: Traffic Control โ”‚ โ”‚ โ”‚ โ”‚ - Rate limiting โ€” token bucket / leaky bucket algorithms โ”‚ โ”‚ โ”‚ โ”‚ - Circuit breaking โ€” prevent fault propagation โ”‚ โ”‚ โ”‚ โ”‚ - Degradation โ€” fallback when a service is unavailable โ”‚ โ”‚ โ”‚ โ”‚ - Canary release โ€” traffic splitting by ratio โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Layer 4: Routing & Load Balancing โ”‚ โ”‚ โ”‚ โ”‚ - Path-based Routing โ”‚ โ”‚ โ”‚ โ”‚ - Host-based Routing โ”‚ โ”‚ โ”‚ โ”‚ - Header-based Routing โ”‚ โ”‚ โ”‚ โ”‚ - Load balancing algorithms โ€” round-robin / weighted / โ”‚ โ”‚ โ”‚ โ”‚ least connections / IP hash โ”‚ โ”‚ โ”‚ โ”‚ - Service Discovery integration โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Layer 5: Protocol Translation & Data Processing โ”‚ โ”‚ โ”‚ โ”‚ - SSL Termination โ€” HTTPS โ†” HTTP โ”‚ โ”‚ โ”‚ โ”‚ - Protocol translation โ€” HTTP โ†” gRPC / WebSocket โ”‚ โ”‚ โ”‚ โ”‚ - Request/Response transformation โ€” JSON โ†” XML โ”‚ โ”‚ โ”‚ โ”‚ - Data compression โ€” Gzip / Brotli โ”‚ โ”‚ โ”‚ โ”‚ - Caching โ€” static assets and API responses โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Inner Layer: Microservice Cluster โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ User Svc โ”‚ โ”‚ Order Svc โ”‚ โ”‚ Product Svc โ”‚ โ”‚ Payment Svc โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ Service Discovery & Config Center (etcd) โ”‚ โ”‚ - Service registration & discovery โ”‚ โ”‚ - Health checks โ”‚ โ”‚ - KV config storage โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

5.2 Routing & Load Balancing5.2 Routing & Load Balancing

One of the gateway's core responsibilities is getting requests to the right place. This involves two key capabilities: routing (which server to go to) and load balancing (how to distribute traffic).One of the gateway's core responsibilities is getting requests to the right place. This involves two key capabilities: routing (which server to go to) and load balancing (how to distribute traffic).

๐Ÿ“– Konsep Penting๐Ÿ“– Core Concept

Imagine an e-commerce system where different URLs map to different services: - /api/users/* โ†’ User Service - /api/orders/* โ†’ Order Service - /api/products/* โ†’ Product Service - /api/pay/* โ†’ Payment Service Nginx configuration example: ``nginx server { listen 80; server_name api.example.com; # User Service location /api/users/ { proxy_pass http://user-service; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } # Order Service location /api/orders/ { proxy_pass http://order-service; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } # Product Service location /api/products/ { proxy_pass http://product-service; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } # Payment Service (requires higher security) location /api/pay/ { # Restrict IP access allow 10.0.0.0/8; deny all; proxy_pass http://payment-service; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } ``Imagine an e-commerce system where different URLs map to different services: - /api/users/* โ†’ User Service - /api/orders/* โ†’ Order Service - /api/products/* โ†’ Product Service - /api/pay/* โ†’ Payment Service Nginx configuration example: ``nginx server { listen 80; server_name api.example.com; # User Service location /api/users/ { proxy_pass http://user-service; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } # Order Service location /api/orders/ { proxy_pass http://order-service; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } # Product Service location /api/products/ { proxy_pass http://product-service; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } # Payment Service (requires higher security) location /api/pay/ { # Restrict IP access allow 10.0.0.0/8; deny all; proxy_pass http://payment-service; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } ``

๐Ÿ“– Konsep Penting๐Ÿ“– Core Concept

When a service has multiple instances, how do you choose? | Strategy | Principle | Use Case | Pros | Cons | | :-------------------- | :-------------------------------------------------------------- | :-------------------------- | :-------------------------------- | :---------------------------------------- | | Round-robin | Assigns to each server in order | Servers with similar specs | Simple and fair | Doesn't consider current server load | | Weighted round-robin | Assigns by weight ratio; higher weight = more traffic | Servers with uneven specs | Fully utilizes high-perf servers | Requires sensible weight configuration | | Least connections | Assigns to the server with the fewest active connections | Long-lived connections, video streaming | Dynamically adapts to load changes | Requires real-time connection tracking | | IP hash | Hashes client IP; same IP always goes to the same server | Session persistence needed | Guarantees session consistency | A heavy-traffic IP can create a hotspot | Nginx configuration example: ``nginx # Weighted round-robin upstream backend_weighted { server 10.0.1.10:8080 weight=3; # High performance, handles more traffic server 10.0.1.11:8080 weight=2; server 10.0.1.12:8080 weight=1; # Lower performance, handles less traffic } # Least connections upstream backend_least_conn { least_conn; server 10.0.1.10:8080; server 10.0.1.11:8080; server 10.0.1.12:8080; } # IP hash (session persistence) upstream backend_ip_hash { ip_hash; server 10.0.1.10:8080; server 10.0.1.11:8080; server 10.0.1.12:8080; } ``When a service has multiple instances, how do you choose? | Strategy | Principle | Use Case | Pros | Cons | | :-------------------- | :-------------------------------------------------------------- | :-------------------------- | :-------------------------------- | :---------------------------------------- | | Round-robin | Assigns to each server in order | Servers with similar specs | Simple and fair | Doesn't consider current server load | | Weighted round-robin | Assigns by weight ratio; higher weight = more traffic | Servers with uneven specs | Fully utilizes high-perf servers | Requires sensible weight configuration | | Least connections | Assigns to the server with the fewest active connections | Long-lived connections, video streaming | Dynamically adapts to load changes | Requires real-time connection tracking | | IP hash | Hashes client IP; same IP always goes to the same server | Session persistence needed | Guarantees session consistency | A heavy-traffic IP can create a hotspot | Nginx configuration example: ``nginx # Weighted round-robin upstream backend_weighted { server 10.0.1.10:8080 weight=3; # High performance, handles more traffic server 10.0.1.11:8080 weight=2; server 10.0.1.12:8080 weight=1; # Lower performance, handles less traffic } # Least connections upstream backend_least_conn { least_conn; server 10.0.1.10:8080; server 10.0.1.11:8080; server 10.0.1.12:8080; } # IP hash (session persistence) upstream backend_ip_hash { ip_hash; server 10.0.1.10:8080; server 10.0.1.11:8080; server 10.0.1.12:8080; } ``

------

6. Gateway Security: Approach to guarding the System's Front Door6. Gateway Security: Approach to guarding the System's Front Door

6.1 Authentication & Authorization6.1 Authentication & Authorization

Traditional approach (each service authenticates independently):Traditional approach (each service authenticates independently):

Gateway-unified authentication:Gateway-unified authentication:

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

Authenticate at the gateway, authorize at the service: - Authentication: Who are you? (Validate token, obtain user identity) - Authorization: What can you do? (Determine permissions based on user role) Like a company reception desk: reception authenticates your identity (ID card), but specific permissions are determined by each department.Authenticate at the gateway, authorize at the service: - Authentication: Who are you? (Validate token, obtain user identity) - Authorization: What can you do? (Determine permissions based on user role) Like a company reception desk: reception authenticates your identity (ID card), but specific permissions are determined by each department.

6.2 HTTPS & SSL Termination6.2 HTTPS & SSL Termination

Why HTTPS?Why HTTPS?

  1. Security: Prevents data from being stolen in transitSecurity: Prevents data from being stolen in transit
  2. Compliance: Modern browsers show "Not Secure" warnings for HTTP sitesCompliance: Modern browsers show "Not Secure" warnings for HTTP sites
  3. SEO: Search engines prioritize HTTPS sitesSEO: Search engines prioritize HTTPS sites
  4. SSL termination approach:SSL termination approach:

    • Only configure HTTPS and certificates at the gateway layerOnly configure HTTPS and certificates at the gateway layer
    • The gateway handles TLS handshakes and encryption/decryptionThe gateway handles TLS handshakes and encryption/decryption
    • Communication between gateway and backend services uses plain HTTP (internal network is trusted)Communication between gateway and backend services uses plain HTTP (internal network is trusted)
    • Backend services focus on business logic without handling TLSBackend services focus on business logic without handling TLS
    ๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

    - Simplified management: Certificates only configured on the gateway, not on backends - Reduced overhead: Backend services don't need to handle TLS handshakes - Unified updates: Certificate renewal only needs to happen on the gateway- Simplified management: Certificates only configured on the gateway, not on backends - Reduced overhead: Backend services don't need to handle TLS handshakes - Unified updates: Certificate renewal only needs to happen on the gateway

    ------

    7. Rate Limiting & Circuit Breaking: Approach to preventing the System from Being Overwhelmed by "Traffic Floods"7. Rate Limiting & Circuit Breaking: Approach to preventing the System from Being Overwhelmed by "Traffic Floods"

    7.1 Rate Limiting Algorithm Comparison7.1 Rate Limiting Algorithm Comparison

    AlgorithmCore IdeaBurst trafficUse CaseComplexity
    Token bucketBucket holds tokens; a request needs a token to passAllows some burstingAPI rate limiting, bandwidth controlMedium
    Leaky bucketRequests enter the bucket and are processed at a steady rateEnforces smoothing; bursts are queued or rejectedScenarios requiring strict steady processingMedium
    Sliding windowCounts requests within a time windowStrictly counts by window; excess is rejectedPrecise counting (e.g., "max 100 per minute")High

    7.2 Nginx Rate Limiting Configuration in Practice7.2 Nginx Rate Limiting Configuration in Practice

    nginx
    # Define rate-limiting zones (place in the http block) # 1. IP-based rate limiting (leaky bucket algorithm) # zone=mylimit:10m โ€” zone name and memory size (10 MB โ‰ˆ 160k IPs) # rate=10r/s โ€” 10 requests per second limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s; # 2. IP-based connection limit (prevents a single IP from opening too many connections) limit_conn_zone $binary_remote_addr zone=addr:10m; # 3. Endpoint-based rate limiting (not per-IP; protects the backend as a whole) limit_req_zone $server_name zone=server_limit:10m rate=100r/s; server { listen 80; server_name api.example.com; # User Service โ€” normal rate limiting location /api/users/ { # Apply rate limiting # burst=20 โ€” bucket capacity, allows 20 burst requests # nodelay โ€” don't delay burst requests (process or reject immediately) limit_req zone=mylimit burst=20 nodelay; # Limit connections per IP limit_conn addr 10; proxy_pass http://user-service; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } # Order Service โ€” stricter rate limiting location /api/orders/ { # Stricter: 5 requests per second limit_req_zone $binary_remote_addr zone=order_limit:10m rate=5r/s; limit_req zone=order_limit burst=10 nodelay; proxy_pass http://order-service; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } # Handling after rate limiting # When a request is rate-limited, return 429 Too Many Requests error_page 429 /429.html; location = /429.html { internal; return 429 '{"error": "Too Many Requests", "message": "Rate limit exceeded. Please try again later."}'; add_header Content-Type application/json; } }
    
    ๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

    - Normal endpoints: 10 requests/second, allow 20 burst - Critical endpoints (payment, orders): 5 requests/second, allow 10 burst - Global protection: Total across all requests no more than 100/second- Normal endpoints: 10 requests/second, allow 20 burst - Critical endpoints (payment, orders): 5 requests/second, allow 10 burst - Global protection: Total across all requests no more than 100/second

    7.3 Circuit Breaking: Preventing Fault Propagation7.3 Circuit Breaking: Preventing Fault Propagation

    How a circuit breaker works:How a circuit breaker works:

    1. Closed state: Requests are forwarded normally; error rate is trackedClosed state: Requests are forwarded normally; error rate is tracked
    2. Open state: When the error rate exceeds the threshold, the circuit breaker opens, immediately returning errors without forwarding requestsOpen state: When the error rate exceeds the threshold, the circuit breaker opens, immediately returning errors without forwarding requests
    3. Half-open state: After a period, a small number of requests are allowed through as probes; if successful, the circuit breaker closesHalf-open state: After a period, a small number of requests are allowed through as probes; if successful, the circuit breaker closes
    4. ๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

      A circuit breaker is like an electrical fuse: when current is too high, the fuse blows automatically, protecting the entire circuit from burning out. Similarly, when a backend service has a high error rate, the circuit breaker "trips," failing fast to prevent the fault from spreading across the entire system.A circuit breaker is like an electrical fuse: when current is too high, the fuse blows automatically, protecting the entire circuit from burning out. Similarly, when a backend service has a high error rate, the circuit breaker "trips," failing fast to prevent the fault from spreading across the entire system.

      ------

      8. Summary: Core Thinking in Gateway Design8. Summary: Core Thinking in Gateway Design

      8.1 Review of Core Principles8.1 Review of Core Principles

      PrincipleMeaningKey Practices
      RoutingGet requests to the right placePath-based, host-based, header-based routing
      Load balancingDistribute traffic across serversRound-robin, weighted, least connections, IP hash
      SecurityGuard the system's front doorAuthentication & authorization, HTTPS, WAF
      Rate limitingPrevent being overwhelmed by trafficToken bucket, leaky bucket, sliding window
      Circuit breakingPrevent fault propagationFail fast, degradation strategies
      ObservabilityMonitoring and troubleshootingLogging, metrics, distributed tracing

      8.2 Technology Selection Advice8.2 Technology Selection Advice

      ๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

      `` Choosing a gateway: โ”‚ โ”œโ”€ Only need reverse proxy & load balancing? โ”‚ โ”œโ”€ Yes โ†’ Nginx (first choice) โ”‚ โ””โ”€ No โ†’ Continue โ”‚ โ”œโ”€ Need a rich plugin ecosystem? โ”‚ โ”œโ”€ Yes โ†’ Kong (built on Nginx) โ”‚ โ””โ”€ No โ†’ Continue โ”‚ โ”œโ”€ Spring Cloud ecosystem? โ”‚ โ”œโ”€ Yes โ†’ Spring Cloud Gateway โ”‚ โ””โ”€ No โ†’ Nginx ```` Choosing a gateway: โ”‚ โ”œโ”€ Only need reverse proxy & load balancing? โ”‚ โ”œโ”€ Yes โ†’ Nginx (first choice) โ”‚ โ””โ”€ No โ†’ Continue โ”‚ โ”œโ”€ Need a rich plugin ecosystem? โ”‚ โ”œโ”€ Yes โ†’ Kong (built on Nginx) โ”‚ โ””โ”€ No โ†’ Continue โ”‚ โ”œโ”€ Spring Cloud ecosystem? โ”‚ โ”œโ”€ Yes โ†’ Spring Cloud Gateway โ”‚ โ””โ”€ No โ†’ Nginx ``

      ------

      9. Glossary9. Glossary

      TermExplanation
      Reverse ProxyA proxy deployed on the server side that receives client requests and forwards them to internal services. Clients only know the reverse proxy, not the real server addresses.
      Forward ProxyA proxy deployed on the client side that accesses external resources on behalf of the client. The server sees the proxy's IP, not the real client. Typical applications: VPNs, circumvention tools.
      API GatewayAn intermediary layer between clients and backend services that provides routing, authentication, rate limiting, logging, and more โ€” the "unified front door" of a microservice architecture.
      Load BalancingDistributing request traffic across multiple servers to avoid overloading a single server, improving system availability and performance.
      SSL TerminationHandling HTTPS encryption/decryption at the gateway layer; backend services use HTTP, reducing backend computational overhead and simplifying certificate management.
      Rate LimitingLimiting the number of requests per unit of time to prevent the system from being overwhelmed by traffic bursts. Common algorithms: token bucket, leaky bucket, sliding window.
      Circuit BreakingAutomatically cutting off calls to a failing dependency to prevent fault propagation, while providing a fallback strategy.
      Session PersistenceEnsuring requests from the same client are always routed to the same backend server, used in scenarios requiring session state.
      Health CheckPeriodically checking the health of backend services, automatically removing faulty nodes to ensure traffic is only sent to healthy instances.
      Canary ReleaseRouting a small portion of traffic to a new version, verifying stability, then gradually increasing the ratio to reduce release risk.
      WAFWeb Application Firewall โ€” protects against SQL injection, XSS, CC attacks, and other web security threats.
      CDNContent Delivery Network โ€” deploys edge nodes globally to accelerate access to static assets.