Appearance
Resilience: reconnect Postgres / Redis sau restart dependency
Version: 1.0
Date: 2026-11-23
Status: DECIDED DIRECTION (D-023 — Owner OK 2026-11-23)
1. Mục tiêu
Mọi process web hoặc worker trong repo MINAP (Platform service hoặc Application) nếu giữ long-lived client tới PostgreSQL và/hoặc Redis phải tự phục hồi sau khi dependency đó restart — không bắt buộc restart process ứng dụng khi DB/Redis đã healthy trở lại.
Áp dụng khi:
- Service dùng Prisma (hoặc client SQL tương đương) tới Postgres
- Service dùng Redis (ioredis / node-redis / …) cho lock, cache, signal, queue, …
- Dual-process (web + worker) như File Service (DEC-016) — cả hai process phải tuân thủ
2. Quyết định (D-023)
| # | Yêu cầu | Ghi chú |
|---|---|---|
| R1 | Không coi “restart container khi Redis/Postgres flap” là chiến lược duy nhất | Ops restart vẫn hữu ích; in-process recovery là bắt buộc |
| R2 | Client PostgreSQL: phát hiện lỗi transient (mất kết nối / pool stale) → reconnect → retry thao tác (bounded) | Ví dụ Prisma: P1001, P1002, P1017, ECONNREFUSED, … |
| R3 | Client Redis: retryStrategy / reconnect không bỏ cuộc sớm; nếu client vào trạng thái terminal (end sau quit) → tạo lại singleton trên lần dùng tiếp | Lab mặc định: ioredis (ADR-001 File Service) |
| R4 | Trước thao tác Redis quan trọng (lock, signal), ensure connected/ready | Tránh fire-and-forget trên client chết |
| R5 | Health/readiness: lúc dependency down → degraded; sau khi dependency up → probe ok mà không cần restart process | Không giả định probe “ok mãi” trong lúc outage |
| R6 | Có test (unit tối thiểu) cho phân loại transient + recreate/retry helper | Reference: platform-file MINAP-2026-012 |
3. Phạm vi / ngoài phạm vi
Trong phạm vi
- Platform repos:
platform-core,platform-file,platform-api, … khi có DB/Redis - Application A/B/C (và worker của App) khi tự giữ Postgres/Redis client
- Shared helpers nếu sau này đưa vào Platform SDK — vẫn phải thỏa R1–R6
Ngoài phạm vi (vẫn OPEN / change control)
- Redis HA (Sentinel / Cluster) — Q-BR-063 còn OPEN
- Postgres HA / failover topology
- Thay thế Infisical / secrets (D-016)
- Đổi public API contract chỉ vì reconnect
4. Pattern tham chiếu (Node / Prisma / ioredis)
Reference implementation: repo platform-file
| Thành phần | Path gợi ý |
|---|---|
| ADR service | platform-file/specs/001-file-service-core/adrs/adr-004-db-redis-reconnect.md |
| Transient classifier | src/lib/db/is-transient-db-error.ts |
| Retry helper | src/lib/db/with-db-retry.ts |
Prisma $extends $allOperations | src/lib/db/prisma.ts |
Redis recreate-on-end + ensureRedisConnected | src/lib/redis/client.ts |
Stack khác (Go, Python, …): cùng nghĩa R1–R6 — không bắt buộc copy API TypeScript; bắt buộc hành vi recovery tương đương.
5. Checklist khi scaffold / Gate 1B–1C
Khi Spec / Plan / Tasks đề cập Postgres hoặc Redis:
- [ ] ADR hoặc mục plan ghi rõ reconnect policy (trỏ D-023 / trang này)
- [ ] Singleton client không “kẹt” sau dependency restart
- [ ] Bounded retry chỉ cho lỗi transient — không retry lỗi nghiệp vụ (
P2002, …) - [ ] Worker + web (nếu dual-process) đều dùng cùng policy
- [ ] Unit test cho helper;
pnpm check(hoặc gate local repo) xanh trước khi báo Owner xong
6. Agent rule
Kit AI: platform-ai/.cursor/rules/84-db-redis-reconnect.mdc — Agents phải áp dụng khi thêm/sửa client DB hoặc Redis.
7. Liên kết
- Sổ quyết định: /architecture/11-decisions-and-open-questions (D-023)
- Engine Postgres: /architecture/database/91-engine-mysql-vs-postgresql (D-022)
- Store ngoài (Redis ghi chú): /architecture/database/90-external-stores
- Tooling gate: /framework/15-repo-tooling-baseline