Skip to content

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ầuGhi chú
R1Không coi “restart container khi Redis/Postgres flap” là chiến lược duy nhấtOps restart vẫn hữu ích; in-process recovery là bắt buộc
R2Client 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, …
R3Client 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ếpLab mặc định: ioredis (ADR-001 File Service)
R4Trước thao tác Redis quan trọng (lock, signal), ensure connected/readyTránh fire-and-forget trên client chết
R5Health/readiness: lúc dependency down → degraded; sau khi dependency up → probe ok mà không cần restart processKhông giả định probe “ok mãi” trong lúc outage
R6Có test (unit tối thiểu) cho phân loại transient + recreate/retry helperReference: 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ầnPath gợi ý
ADR serviceplatform-file/specs/001-file-service-core/adrs/adr-004-db-redis-reconnect.md
Transient classifiersrc/lib/db/is-transient-db-error.ts
Retry helpersrc/lib/db/with-db-retry.ts
Prisma $extends $allOperationssrc/lib/db/prisma.ts
Redis recreate-on-end + ensureRedisConnectedsrc/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:

  1. [ ] ADR hoặc mục plan ghi rõ reconnect policy (trỏ D-023 / trang này)
  2. [ ] Singleton client không “kẹt” sau dependency restart
  3. [ ] Bounded retry chỉ cho lỗi transient — không retry lỗi nghiệp vụ (P2002, …)
  4. [ ] Worker + web (nếu dual-process) đều dùng cùng policy
  5. [ ] 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 ​

Hub SoT · chi tiết theo repo: /docs-sites · văn xuôi tiếng Việt + thuật ngữ chuyên ngành English