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

Bài 23: Performance & Scaling — CDN, Caching & Queue Architecture

Performance optimization — CDN cho images/mockups, multi-layer caching, image processing optimization, queue architecture, database scaling, auto-scaling policies.

🏗️ Kiến trúc — Bài 23 Bài 23: Performance & Scaling — CDN, Caching & Queue Architecture

Kiến trúc Hệ thống Fashion Design & Print-on-Demand — Từ Domain Analysis đến Production

Phần 7: Operations, Security & Scale

xdev.asia

1. Performance Hotspots trong POD

  • Image-heavy traffic (mockups, previews, thumbnails)
  • Search/filter catalog lớn
  • Burst traffic theo campaign/season
  • Background jobs: rendering, AI, sync channel

2. CDN Strategy

Origin (S3/Object Storage)
    -> CDN Edge Cache
       -> Image transformation layer (WebP/AVIF, resize)
          -> Browser cache
Asset typeTTLCache key
Mockup immutable30-90 ngàyhash-based URL
PDP images7 ngàyproductId+variant+size
Editor assets1-24huser scope

3. Multi-layer Caching

L1: CDN edge
L2: API cache (Redis)
L3: Application in-memory cache
L4: DB query cache/materialized views
class ProductReadCache {
  async get(productId: string) {
    const key = `pdp:${productId}`;
    const cached = await redis.get(key);
    if (cached) return JSON.parse(cached);

    const value = await this.repo.getPdpView(productId);
    await redis.set(key, JSON.stringify(value), 'EX', 300);
    return value;
  }
}

4. Queue Architecture

Queues
  - q.realtime.preview (high priority)
  - q.print.production (high priority)
  - q.mockup.batch (medium)
  - q.channel.sync (medium)
  - q.analytics.enrichment (low)

Use dead-letter queues per domain.
interface QueuePolicy {
  maxRetry: number;
  backoffMs: number[];
  timeoutMs: number;
  concurrency: number;
}

5. Database Scaling

  • Read replicas cho product/search reads
  • Partitioning theo `shop_id` hoặc `created_at` với bảng lớn
  • Connection pooling (PgBouncer)
  • Index strategy theo query patterns

6. Auto-scaling Policies

WorkloadScale triggerRange
API podsCPU + P95 latency4-60
Queue workersQueue depth + lag2-200
GPU inferenceRequests/sec + VRAM1-20

7. SLO đề xuất

  • P95 PDP API latency < 250ms
  • P95 checkout API latency < 400ms
  • Image first byte (CDN) < 100ms
  • Queue lag (critical) < 30s

8. Tổng kết

  • CDN + image optimization là đòn bẩy hiệu năng lớn nhất cho POD

  • Queue priority giúp bảo vệ workflow quan trọng khi tải cao

  • DB scaling cần đi cùng query/index discipline

  • SLO-driven autoscaling giúp cân bằng chi phí và chất lượng dịch vụ