Chuyển đến nội dung chính

Bài 8: Reverse Proxy và API Gateway

Reverse Proxy: SSL Termination, Compression, Security. API Gateway Pattern: routing, authentication, rate limiting, throttling. So sánh Nginx, Envoy, Kong, AWS API Gateway. Service Mesh concepts (Istio, Linkerd). BFF (Backend for Frontend) pattern.

🏗️ Kiến trúc — Bài 8 Bài 8: Reverse Proxy và API Gateway

System Architecture: From Zero to Hero

Phần 2: Các Thành Phần Hạ Tầng (Infrastructure Components)

xdev.asia

Giới thiệu

Reverse Proxy và API Gateway là hai thành phần đứng giữa client và backend servers. Chúng cung cấp security, routing, và cross-cutting concerns mà mọi hệ thống production đều cần.


1. Reverse Proxy

1.1 Forward Proxy vs Reverse Proxy

Forward Proxy (đại diện cho client):
  Client ──► Proxy ──► Internet ──► Server
  Ví dụ: VPN, Corporate proxy

Reverse Proxy (đại diện cho server):
  Client ──► Internet ──► Reverse Proxy ──► Backend Servers
  Ví dụ: Nginx, HAProxy, Cloudflare

1.2 Chức năng chính

┌──────────────────────────────────────────────┐
│              Reverse Proxy                    │
│  ┌──────────────────────────────────────┐    │
│  │ SSL/TLS Termination                   │    │
│  │ → HTTPS decrypt tại proxy             │    │
│  │ → Backend dùng HTTP (nhanh hơn)       │    │
│  ├──────────────────────────────────────┤    │
│  │ Compression (gzip, brotli)            │    │
│  │ → Nén response trước khi gửi client   │    │
│  ├──────────────────────────────────────┤    │
│  │ Static File Serving                   │    │
│  │ → Serve HTML, CSS, JS, images         │    │
│  ├──────────────────────────────────────┤    │
│  │ Caching                               │    │
│  │ → Cache responses, giảm backend load  │    │
│  ├──────────────────────────────────────┤    │
│  │ Security                              │    │
│  │ → Hide backend topology               │    │
│  │ → Rate limiting, IP blocking, WAF     │    │
│  └──────────────────────────────────────┘    │
└──────────────────────────────────────────────┘

2. API Gateway

2.1 API Gateway Pattern

                    ┌─────────────────┐
  Mobile App ──────►│                 │──► User Service
  Web App ─────────►│   API Gateway   │──► Order Service
  Partner API ─────►│                 │──► Payment Service
  IoT Device ─────►│                 │──► Notification Service
                    └─────────────────┘

2.2 Chức năng API Gateway

Chức năngMô tả
Request RoutingRoute /users/* → User Service, /orders/* → Order Service
AuthenticationVerify JWT token, API key validation
AuthorizationCheck permissions, RBAC
Rate Limiting100 requests/phút per API key
Request/Response TransformThay đổi format, thêm/xóa headers
Circuit BreakerNgắt requests nếu service down
Logging & MonitoringAccess logs, metrics, tracing
API Versioning/v1/users, /v2/users

2.3 Rate Limiting Algorithms

Token Bucket:
  Bucket chứa N tokens, refill rate = R tokens/giây
  Mỗi request lấy 1 token
  Hết tokens → 429 Too Many Requests

  Ví dụ: 100 tokens, refill 10/s
  T=0:  100 tokens
  T=0:  Burst 100 requests → 0 tokens
  T=1:  10 tokens refilled
  T=10: 100 tokens lại

Sliding Window:
  Đếm requests trong window N giây gần nhất
  Mỗi request mới: count++ nếu count < limit
  → Chính xác hơn Token Bucket

3. BFF (Backend for Frontend)

3.1 Vấn đề

Mobile App cần:  { name, avatar }     ← Ít data, bandwidth thấp
Web App cần:     { name, avatar, posts, friends, settings }  ← Đầy đủ
Admin Panel cần: { name, email, role, audit_log, permissions } ← Khác

Nếu dùng chung 1 API:
  → Mobile nhận thừa data (waste bandwidth)
  → Web phải gọi nhiều APIs (nhiều roundtrips)

3.2 BFF Solution

         ┌────────────┐
Mobile ──►│ Mobile BFF │──► User Service
         └────────────┘──► Post Service
         ┌────────────┐
Web ─────►│  Web BFF   │──► User Service
         └────────────┘──► Post Service
                        ──► Friend Service
         ┌────────────┐
Admin ───►│ Admin BFF  │──► User Service
         └────────────┘──► Audit Service

Mỗi BFF tối ưu response cho client cụ thể.


4. Service Mesh

4.1 Service Mesh là gì?

Không có Service Mesh:
  Service A ──► Service B
  Mỗi service phải tự implement:
  retry, timeout, circuit breaker, mTLS, tracing, metrics

Có Service Mesh:
  Service A ──► Sidecar Proxy A ──► Sidecar Proxy B ──► Service B
  Sidecar proxy xử lý tất cả cross-cutting concerns

┌─────────────────┐       ┌─────────────────┐
│  Pod A          │       │  Pod B          │
│ ┌─────┐ ┌────┐ │       │ ┌────┐ ┌─────┐ │
│ │App A│ │Envoy│◄├───────├►│Envoy│ │App B│ │
│ └─────┘ └────┘ │       │ └────┘ └─────┘ │
└─────────────────┘       └─────────────────┘

4.2 Istio vs Linkerd

FeatureIstioLinkerd
ProxyEnvoylinkerd2-proxy (Rust)
ComplexityCaoThấp
PerformanceModerateTốt hơn
FeaturesRất nhiềuFocused
Best forEnterprise, complex needsSimple service mesh

5. So sánh API Gateway Solutions

SolutionTypeBest for
NginxReverse Proxy + LBSimple routing, static files
KongFull API GatewayPlugin ecosystem, multi-cloud
EnvoyL7 ProxyService mesh, gRPC
AWS API GatewayManagedAWS ecosystem, serverless
TraefikCloud-nativeKubernetes, auto-discovery
APISIXAPI GatewayPerformance, Lua plugins

6. Hands-on: API Gateway với Nginx

# API Gateway configuration

# Rate limiting
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;

# Upstream services
upstream user_service {
    server 10.0.1.1:8001;
    server 10.0.1.2:8001;
}

upstream order_service {
    server 10.0.2.1:8002;
    server 10.0.2.2:8002;
}

server {
    listen 443 ssl http2;
    server_name api.xdev.asia;

    # SSL Termination
    ssl_certificate /etc/ssl/certs/api.crt;
    ssl_certificate_key /etc/ssl/private/api.key;

    # Compression
    gzip on;
    gzip_types application/json;

    # API Routing
    location /api/v1/users {
        limit_req zone=api burst=20 nodelay;
        proxy_pass http://user_service;
        proxy_set_header Authorization $http_authorization;
    }

    location /api/v1/orders {
        limit_req zone=api burst=20 nodelay;
        proxy_pass http://order_service;
        proxy_set_header Authorization $http_authorization;
    }

    # Health check
    location /health {
        return 200 '{"status":"ok"}';
        add_header Content-Type application/json;
    }
}

Tổng kết

ComponentRoleWhen to use
Reverse ProxySSL, caching, securityAlways in production
API GatewayRouting, auth, rate limitingMicroservices architecture
BFFOptimize per client typeMultiple client types
Service MeshCross-cutting concernsLarge microservices (50+ services)

Bài tập

  1. API Gateway Design: Thiết kế API Gateway cho ứng dụng e-commerce với 5 backend services. Xác định routing rules, rate limits, và authentication strategy.

  2. BFF vs Single API: Hệ thống có Mobile App, Web App, và Smart TV App. Mỗi platform cần data khác nhau. Thiết kế BFF architecture với data mapping cho mỗi platform.

  3. Rate Limiting: Implement Token Bucket algorithm bằng pseudocode. Bucket size = 100, refill rate = 10 tokens/giây.