Module 12 · Shared contract
Shared Capstone Specification
Một business problem duy nhất để hai implementation WebFlux và Vert.x so sánh công bằng.
1. Domain
| Entity | Fields chính |
|---|---|
| User | id, status |
| Order | id, userId, status, total, idempotencyKey |
| OrderItem | sku, quantity, unitPrice |
| InventoryReservation | orderId, sku, qty, status |
| Payment | paymentId, orderId, status, amount |
| OrderEvent | orderId, type, timestamp, payload |
2. API Contract
POST /orders
GET /orders/{id}
GET /users/{id}/orders
POST /orders/{id}/cancel
GET /orders/{id}/events (SSE)
POST /orders dùng Idempotency-Key.
3. Create Order flow
Validate → Find User → Reserve Inventory → Price → Payment → Persist Order → Publish Event → 201
4. Failure rules
| Failure | Behavior |
|---|---|
| User not found | 404. |
| Insufficient inventory | 409. |
| Payment timeout | 503/domain timeout. |
| Duplicate idempotency key | Return same outcome nếu request tương đương. |
| DB fail sau payment | Compensation/reconciliation. |
| Publish event fail | Outbox/retry strategy. |
5. Schema
CREATE TABLE orders (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT NOT NULL,
status VARCHAR(32) NOT NULL,
total NUMERIC(18,2) NOT NULL,
idempotency_key VARCHAR(128) UNIQUE NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);6. Timeouts
- User 500 ms.
- Inventory 700 ms.
- Payment 1500 ms.
- Overall create-order 3 s.
7. Shared test scenarios
- Happy path.
- User 404.
- Inventory conflict.
- Payment timeout.
- Duplicate key.
- Cancel.
- SSE events.
- Slow DB.
- Client disconnect.
- Concurrent duplicate POST.
8. Parity
Cùng API, schema, business rules, timeout budget, downstream mock và load workload. Chỉ implementation model khác.