本專案是一個工業級的 High Concurrency Real-time Bidding System(高並發即時競標系統),旨在模擬「雙 11」或「黑色星期五」等極端流量場景。系統經過精心設計,能夠處理大量的並發寫入請求 (Thundering Herd),提供毫秒級的 Real-time Feedback(即時回饋),並確保強大的 Data Consistency(資料一致性)以防止 Overselling(超賣)。
- High Concurrency Writes:處理數千個同時發生的競標請求。
- Real-time Feedback:以低於 200ms 的延遲將排名更新廣播給所有客戶端。
- Strong Consistency:使用 ACID Transactions 和 Row-level Locks 保證
Orders <= Stock。
- Event-Driven Architecture:利用 Apache Kafka 進行流量整形 (Traffic Shaping) 和緩衝。
- Real-time Updates:Socket.io 將即時排名和拍賣狀態推播給客戶端。
-
Dynamic Scoring Algorithm:根據 Price(價格)、Time(時間)和 User Weight(使用者權重)計算分數。
$$Score = \alpha \cdot P + \frac{\beta}{T+1} + \gamma \cdot W$$ - Scalable Worker Service:可根據 CPU 負載自動擴展 (Auto-scale) 的批次處理 Workers。
- Optimized Storage:使用 Redis ZSET 進行高速排名,PostgreSQL 進行可靠的持久化存儲。
- Admin Dashboard:即時監控 QPS、最高出價者和拍賣控制。
本系統採用基於 Microservices 的架構,並在 Monorepo 中進行管理。
flowchart TB
subgraph Client_Layer["Client Layer"]
User("User / Virtual Users")
Admin("Admin Dashboard")
end
subgraph Interface_Layer["Interface Layer"]
API["API Gateway (Fastify)"]
Socket["Socket.io Service"]
end
subgraph Async_Layer["Buffering Layer"]
Kafka{{"Apache Kafka"}}
end
subgraph Core_Layer["Processing Layer"]
Worker["Worker Service (Batch)"]
end
subgraph Data_Layer["Storage Layer"]
Redis[("Redis ZSET")]
DB[("PostgreSQL")]
end
Client_Layer ~~~ Interface_Layer ~~~ Async_Layer & Core_Layer ~~~ Data_Layer
User -- HTTP POST /bid --> API
User <-- WebSocket --> Socket
API -- "Produce Event" --> Kafka
Kafka -- "Consume (Batch)" --> Worker
Worker -- "Calc & Update" --> Redis
Worker -- "Push Updates" --> Socket
Worker -- "Final Settlement" --> DB
Socket -- Broadcast --> User
Socket -- Monitor Data --> Admin
| 領域 | 技術 | 描述 |
|---|---|---|
| Language | TypeScript | 用於端到端 Type Safety 的共享類型。 |
| Frontend | React, Vite, TailwindCSS | 使用 shadcn/ui 的高效能 SPA。 |
| Backend | Node.js (Fastify) | 低開銷、高吞吐量的 API Gateway。 |
| Messaging | Apache Kafka | 用於緩衝的工業級 Message Queue。 |
| Database | PostgreSQL | 具備 ACID 合規性的主要資料庫。 |
| ORM | Drizzle ORM | Type-safe 且 Zero-runtime Overhead 的 ORM。 |
| Cache | Redis | 用於 Leaderboards 的 In-memory 資料結構存儲。 |
| Infra | Docker, Kubernetes (GKE) | Containerization 和 Orchestration。 |
- Node.js (v18+)
- pnpm (v9+)
- Docker & Docker Compose
-
安裝依賴 (Install Dependencies)
pnpm install
-
啟動基礎設施與服務 (Start Infrastructure & Services)
-
首次啟動或需重置環境 (Fresh Start): 此指令會執行
scripts/setup-dev.ts清除 Redis/Kafka 資料並重置資料庫,適合初次執行或需要乾淨環境時使用。pnpm dev:fresh
-
日常開發 (Daily Development): 若不希望清除現有資料(如保留商品與訂單紀錄),僅需重啟服務,請使用:
pnpm dev
請訪問
http://localhost:5173使用 Web 應用程式 -
-
資料庫管理 (Database Management)
- 產生 Migrations:
pnpm db:generate - 執行 Migrations:
pnpm db:migrate - 開啟 Drizzle Studio:
pnpm db:studio
- 產生 Migrations:
我們擁有全面的測試套件,包括 Functional、Load 和 Stress Tests。
執行自動化編排腳本,模擬包含 Panic Bidding 的完整拍賣生命週期。
pnpm test:full有關詳細的測試場景,請參閱 Test Plan。
本系統設計為使用 Autopilot 模式部署於 Google Kubernetes Engine (GKE) 以實現 Auto-scaling。
- Infrastructure: GKE Autopilot, Cloud SQL, Cloud Memorystore.
- Scaling: 基於 CPU Utilization 的 Horizontal Pod Autoscaler (HPA)。
有關詳細的部署說明,請參閱 Deployment Specification。
.
├── apps
│ ├── api # Fastify API Gateway
│ ├── socket # Socket.io Real-time Service
│ ├── web # React Frontend Application
│ └── worker # Node.js Background Worker
├── packages
│ ├── db # Database Schema & Drizzle Config
│ └── shared # Shared Types & Utilities
├── scripts # Automation & Test Scripts
├── tests # k6 Load Test Scripts
└── docker-compose.yml
Author: RiceTooCold License: ISC