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

Bài 3: Kiến trúc tổng quan Full-Stack — Microservices + Micro Frontend + BFF

Blueprint kiến trúc toàn diện: Frontend (Micro Frontend Shell + Remote Apps), BFF Layer, API Gateway, Backend Microservices, Message Broker, Database per Service. Luồng request end-to-end và các điểm tích hợp chính.

🏗️ Kiến trúc — Bài 3 Bài 3: Kiến trúc tổng quan Full-Stack — Microservices + Micro Frontend + BFF

Thiết kế hệ thống Microservices & Micro Frontend — Từ cơ bản đến Production

Phần 1: Nền tảng — Evolution of Architecture

xdev.asia

Giới thiệu

Sau khi hiểu lộ trình tiến hóa kiến trúc và DDD, bài này vẽ ra blueprint tổng quan cho hệ thống Full-Stack Microservices + Micro Frontend — bản đồ kiến trúc mà chúng ta sẽ đi sâu vào từng phần trong suốt series.

Full-Stack Architecture Blueprint — Microservices + Micro Frontend


1. Architecture Overview

1.1 Full-Stack Architecture Blueprint

┌─────────────────────────────────────────────────────┐
│                    CLIENTS                          │
│            (Browser, Mobile, IoT)                   │
└──────────────────────┬──────────────────────────────┘
                       │
┌──────────────────────┴──────────────────────────────┐
│              CDN (Static Assets)                    │
│         CloudFront / CloudFlare / Vercel            │
└──────────────────────┬──────────────────────────────┘
                       │
┌──────────────────────┴──────────────────────────────┐
│           MICRO FRONTEND LAYER                      │
│  ┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐ ┌──────────┐  │
│  │Shell │ │Produc│ │ Cart │ │Order │ │ Account  │  │
│  │ App  │ │ MFE  │ │ MFE  │ │ MFE  │ │   MFE    │  │
│  └──┬───┘ └──┬───┘ └──┬───┘ └──┬───┘ └────┬─────┘  │
└─────┼────────┼────────┼────────┼──────────┼─────────┘
      └────────┴────────┴────────┴──────────┘
                       │
┌──────────────────────┴──────────────────────────────┐
│              BFF LAYER (Optional)                   │
│        Backend for Frontend Aggregation             │
└──────────────────────┬──────────────────────────────┘
                       │
┌──────────────────────┴──────────────────────────────┐
│              API GATEWAY                            │
│     (Kong / APISIX / Envoy Gateway)                 │
│  Auth │ Rate Limit │ Routing │ Load Balance         │
└──┬────────┬────────────┬────────────┬───────────────┘
   │        │            │            │
┌──┴──┐ ┌───┴───┐  ┌────┴────┐  ┌────┴────┐
│User │ │Product│  │  Cart   │  │  Order  │
│ µS  │ │  µS   │  │   µS    │  │   µS    │
│     │ │       │  │         │  │         │
│┌───┐│ │┌────┐ │  │ ┌─────┐ │  │ ┌─────┐ │
││PG ││ ││PG  │ │  │ │Redis│ │  │ │ PG  │ │
│└───┘│ │└────┘ │  │ └─────┘ │  │ └─────┘ │
└─────┘ └───────┘  └─────────┘  └────┬────┘
                                     │
┌────────────────────────────────────┴────────────────┐
│              MESSAGE BROKER                         │
│         (Kafka / RabbitMQ / NATS)                   │
│    Events: OrderPlaced, PaymentConfirmed, etc.      │
└─────────────────────────────────────────────────────┘

2. Các Layer chính

2.1 Micro Frontend Layer

Shell Application (Container) điều phối các Remote Micro Frontend applications. Mỗi MFE là một ứng dụng độc lập, được build và deploy riêng biệt.

Trách nhiệm Shell App:

  • Layout chung (Header, Footer, Sidebar)
  • Routing & Navigation
  • Authentication state
  • Error boundaries

2.2 BFF Layer

Backend for Frontend aggregates data từ nhiều microservices, transform thành format phù hợp cho từng micro frontend. Đặc biệt hữu ích khi:

  • Frontend cần data từ nhiều services trong 1 request
  • Mobile vs Web cần data format khác nhau
  • Cần caching layer riêng cho frontend

2.3 API Gateway

Entry point cho tất cả API calls. Xử lý cross-cutting concerns:

  • Authentication/Authorization: Verify JWT tokens
  • Rate Limiting: Bảo vệ backend khỏi abuse
  • Request Routing: Route đến đúng service
  • Load Balancing: Phân phối traffic
  • SSL Termination: Xử lý HTTPS

2.4 Microservices Layer

Mỗi microservice own một Bounded Context, có database riêng, deploy độc lập. Giao tiếp qua REST/gRPC (sync) hoặc Message Broker (async).

2.5 Message Broker / Event Bus

Backbone cho async communication giữa các services. Đảm bảo loose coupling và event-driven data flow.


3. Request Flow End-to-End

User clicks "Add to Cart" trên Product MFE:

1. Product MFE → Shell App (event: addToCart)
2. Shell App → API Gateway (POST /api/cart/items)
3. API Gateway → Auth check → Route đến Cart Service
4. Cart Service → Validate product (call Product Service via gRPC)
5. Cart Service → Save to Redis
6. Cart Service → Publish event "ItemAddedToCart" lên Kafka
7. Response → API Gateway → Shell App
8. Shell App → Cart MFE (update cart badge via custom event)
9. Recommendation Service (async) → Listen event → Update recommendations

4. Technology Stack Overview

LayerTechnologyWhy
Micro FrontendReact + Module FederationMature ecosystem, large community
Shell AppReact + React RouterSPA routing, lazy loading
Design SystemTailwind CSS + StorybookConsistent UI, component library
BFFNode.js (Fastify)Same language as frontend, fast I/O
API GatewayKong / APISIXPlugin ecosystem, high performance
MicroservicesNode.js / GoNode cho CRUD, Go cho high-perf
Message BrokerApache KafkaDurability, replayability, high throughput
DatabasesPostgreSQL, Redis, ElasticsearchPolyglot persistence
AuthKeycloakOpen-source, full OAuth2/OIDC
CI/CDGitHub Actions + ArgoCDGitOps, Kubernetes-native
ObservabilityOpenTelemetry + Grafana StackVendor-neutral, full-stack tracing

Tóm tắt

Bài này cung cấp bản đồ kiến trúc tổng quan cho toàn series. Mỗi layer sẽ được đi sâu trong các phần tiếp theo. Hiểu big picture giúp bạn không bị lạc khi đi vào chi tiết từng component.


Bài tiếp theo: Bài 4: Service Decomposition — Bounded Context & Service Boundaries