## 🛠️ 知識體系與方法論

### 架構模式目錄

#### 通訊模式
- **Sync RPC**：gRPC（HTTP/2 + Protobuf）、REST with timeout/circuit breaker
- **Async Messaging**：Kafka（log-based）、RabbitMQ（queue-based）、NATS（lightweight pub/sub）
- **Event-Driven**：Event Sourcing + CQRS、Outbox Pattern、Transactional Messaging
- **Streaming**：Flink/Spark Streaming 用於即時聚合與CEP

#### 資料模式
- **Replication**：Primary-Replica（async/sync semi-sync）、Multi-Leader、Leaderless（Dynamo-style）
- **Partitioning**：Hash/Range/Directory-based sharding、consistent hashing with virtual nodes
- **Caching**：Cache-aside、Read-through/Write-through、Write-behind、TTL + explicit invalidation

#### 可靠性模式
- **Resilience4j / Hystrix patterns**：Circuit Breaker、Bulkhead、Retry with jitter、Timeout
- **Load Management**：Token bucket、Leaky bucket、Adaptive concurrency limiting
- **Disaster Recovery**：Active-Passive、Active-Active、RPO/RTO 規劃、跨區域複製延遲

### 共識與協調
| 場景 | 推薦方案 | 注意事項 |
|------|---------|---------|
| 強一致配置管理 | etcd/ZooKeeper (Raft/ZAB) | 寫入吞吐有限，適合 metadata |
| 高吞吐日誌複製 | Kafka ISR | 需處理 unclean leader election 風險 |
| 分散式鎖 | Redis Redlock / DB advisory lock | 必須設定 TTL + fencing token |
| 跨服務交易 | Saga (choreography/orchestration) | 需補償交易 + 冪等消費者 |

### 可觀測性堆疊
```
Metrics  → Prometheus + Grafana（RED/USE method）
Logs     → 結構化 JSON + 集中式收集（ELK/Loki）
Traces   → OpenTelemetry → Jaeger/Tempo
Alerts   → 基於 SLO 的 multi-window burn rate alerts
```

### 設計評審檢查清單
1. ☐ 服務邊界是否對齊業務能力（Bounded Context）？
2. ☐ API 是否向後相容？版本策略為何？
3. ☐ 每個外部依賴是否有 timeout + circuit breaker？
4. ☐ 寫入路徑是否具備冪等性？
5. ☐ 資料庫連線池、執行緒池是否有界（bounded）？
6. ☐ 部署是否支援 rolling update + 自動回滾？
7. ☐ 機密管理是否使用 Vault/KMS 而非環境變數明文？
8. ☐ 混沌測試是否覆蓋網路延遲、節點終止、依賴超時？

### 效能分析框架
1. **定義目標**：P50/P95/P99 延遲、峰值 QPS、資料量增長曲線
2. **建立基線**：負載測試（k6/Locust/Gatling）+ 生產 trace 取樣
3. **定位瓶頸**：USE method（Utilization/Saturation/Errors）逐層排查
4. **驗證修復**：A/B 或金絲雀發布，確認無回歸

### 參考文獻與原則
- *Designing Data-Intensive Applications* (Kleppmann) — 分散式系統聖經
- *Site Reliability Engineering* (Google SRE Book) — 可靠性實踐
- *Building Microservices* (Newman) — 服務拆分與通訊
- Fallacies of Distributed Computing — 每次設計前默念
- 12-Factor App — 雲原生應用基線

### 語言與框架偏好（依情境）
- **高併發服務**：Go（goroutine + channel 模型）、Rust（零成本抽象）
- **企業整合**：Java/Kotlin + Spring Boot（成熟生態）
- **快速原型**：Python + FastAPI（注意 GIL 對 CPU-bound 的限制）
- **基礎設施即程式碼**：Terraform + Helm + ArgoCD GitOps 流程