Skip to content

Gateway Resilience

1. Tổng quan

Traefik cung cấp các mẫu resilience tích hợp sẵn — circuit breaker, rate limiting, retry, và health check chủ động — không cần plugin hay code tùy biến.

Nguồn: packages/gateway/config/dynamic/middlewares.yml (circuit breaker, rate limiting), infrastructure/deployments/develop/*/docker-compose.yml (health check)

2. Luồng Resilience

3. Circuit Breaker

Cấu hình

Nguồn: packages/gateway/config/dynamic/middlewares.yml (dòng 44–49)

yaml
circuit-breaker:
  circuitBreaker:
    # vế tỷ lệ 5xx hiện bị comment out trong source:
    # expression: "ResponseCodeRatio(500, 600, 0, 600) > 0.30 || NetworkErrorRatio() > 0.10 || LatencyAtQuantileMS(95.0) > 3000"
    expression: "NetworkErrorRatio() > 0.10 || LatencyAtQuantileMS(95.0) > 3000"
    checkPeriod: 5s
    fallbackDuration: 15s
    recoveryDuration: 30s

Phân tích Expression

Điều kiệnKích hoạtHoạt động?
NetworkErrorRatio() > 0.10Hơn 10% request dẫn đến network error
LatencyAtQuantileMS(95.0) > 3000P95 latency vượt 3 giây
ResponseCodeRatio(500, 600, 0, 600) > 0.30Hơn 30% response là lỗi 5xxBị comment out trong source

Expression dùng logic OR — bất kỳ điều kiện hoạt động đơn lẻ nào kích hoạt circuit breaker. Vế tỷ lệ 5xx có nhưng bị comment out trong middlewares.yml.

Tham số Thời gian

Tham sốGiá trịMô tả
checkPeriod5sTần suất expression được đánh giá
fallbackDuration15sCircuit ở trạng thái mở bao lâu trước khi thử hồi phục
recoveryDuration30sPha hồi phục kéo dài bao lâu trước khi đóng hoàn toàn

Máy Trạng thái

Trạng tháiHành vi
ClosedVận hành bình thường — request đi qua
OpenMọi request fail nhanh với 503 trong 15 giây
RecoveringRequest giới hạn được phép để test hồi phục trong 30 giây

Hàm Expression Có sẵn

HàmMô tả
NetworkErrorRatio()Tỷ lệ network error trên tổng request
ResponseCodeRatio(from, to, dividedByFrom, dividedByTo)Tỷ lệ response code trong các khoảng
LatencyAtQuantileMS(quantile)Latency tại quantile cho trước (0-100)

4. Rate Limiting

Rate Limit Chung

Nguồn: packages/gateway/config/dynamic/middlewares.yml (dòng 53–59)

Áp dụng cho hầu hết dịch vụ — 200 request/giây theo IP client:

yaml
rate-limit:
  rateLimit:
    average: 200
    burst: 400
    sourceCriterion:
      ipStrategy:
        depth: 1

Rate Limit riêng cho Auth

Nguồn: packages/gateway/config/dynamic/middlewares.yml (dòng 62–69)

Giới hạn nghiêm hơn cho endpoint xác thực — 30 request/phút theo IP client:

yaml
rate-limit-auth:
  rateLimit:
    average: 30
    burst: 60
    period: 1m
    sourceCriterion:
      ipStrategy:
        depth: 1

Rate Limiting theo IP

Cả hai rate limiter dùng sourceCriterion.ipStrategy.depth: 1 để trích IP client thực từ header X-Forwarded-For. Điều này cần thiết vì Traefik nằm sau Nginx — không có cài đặt này, mọi request sẽ trông như đến từ IP của Nginx và dùng chung một bucket rate limit.

Response Rate Limit

Khi rate limit bị vượt, Traefik trả về 429 Too Many Requests.

5. Health Check Chủ động

Nguồn: Docker labels trong infrastructure/deployments/develop/*/docker-compose.yml

Traefik định kỳ kiểm tra endpoint health của dịch vụ:

yaml
# Docker label theo từng dịch vụ
- "traefik.http.services.identity.loadbalancer.healthcheck.path=/v1/api/identity/health"
- "traefik.http.services.identity.loadbalancer.healthcheck.interval=30s"
Tham sốGiá trịMô tả
path/v1/api/<service>/healthEndpoint health check (IGNIS tích hợp sẵn)
interval30sKiểm tra mỗi 30 giây

Tất cả 7 dịch vụ backend (identity, commerce, sale, finance, inventory, payment, signal) có health check cấu hình ở khoảng 30 giây. Nếu một dịch vụ fail health check, Traefik ngừng route traffic tới nó cho đến khi hồi phục.

6. Retry (Theo từng dịch vụ)

Retry có thể cấu hình theo từng dịch vụ qua Docker labels. Không dịch vụ nào hiện cấu hình retry — cái này có sẵn cho dùng tương lai:

yaml
# Ví dụ: retry tối đa 3 lần cho dịch vụ commerce
- "traefik.http.middlewares.retry-commerce.retry.attempts=3"
- "traefik.http.middlewares.retry-commerce.retry.initialinterval=100ms"
- "traefik.http.routers.commerce.middlewares=retry-commerce@docker,rate-limit@file,circuit-breaker@file,security-headers@file"

7. Trang liên quan

Tài liệuMô tả
Tổng quan GatewayThẻ định danh + service catalog
MiddlewaresĐịnh nghĩa middleware đầy đủ từ source
ObservabilityPrometheus metrics, access logs, Grafana
Vận hànhDeploy, runbook, phân loại cảnh báo
Quyết địnhADRs

Proprietary and Confidential. Unauthorized copying, distribution, or use of this software is strictly prohibited.