如何用 PostgreSQL 設計可安全重試的客戶資料接收流程
Designing Retry-Safe Customer Intake with PostgreSQL
PostgreSQL 客戶資料接收流程可透過重試鍵、租戶範圍及請求指紋,避免同一操作因網絡逾時重試而重複建立記錄,並拒絕同一重試鍵配上不同資料。設計以資料庫唯一性約束和交易式 outbox 分隔資料提交與訊息派送,另須獨立保障預約容量,並測試並行請求及派送故障。文中的 SQL 僅為示例,並非經驗證的生產實作。
Disclosure: This draft was generated with AI assistance for GILNEX editorial review. The SQL is an illustrative example, not a verified production implementation. References checked November 27, 2026.
A customer taps Submit, the network times out, and the browser retries. Did the first request fail, or did the response disappear after the server committed? A reliable intake system must handle both possibilities without producing two appointments, two notifications, or two CRM records.
Define the operation, not just the request
A retry key identifies one intended operation. Generate it once for the submission and reuse it after a timeout. Scope it to the authenticated tenant and operation type. Derive the tenant on the server; never trust a tenant identifier supplied by a browser.
Persist a normalized request fingerprint. If the same key arrives with a different payload, reject it as a conflict rather than silently accepting the changed request. A different key does not automatically mean the same customer; business-level duplicate detection is a separate concern.
Make the database enforce uniqueness
For a hypothetical PostgreSQL intake service, consider this minimal table:
CREATE TABLE intake_requests (
tenant_id uuid NOT NULL,
request_key uuid NOT NULL,
payload_hash text NOT NULL,
response_id uuid NOT NULL,
PRIMARY KEY (tenant_id, request_key)
);
Within one transaction, attempt an insert using ON CONFLICT (tenant_id, request_key) DO NOTHING RETURNING response_id. If a row is returned, this transaction owns the initial operation. Create the corresponding lead and an outbox record in the same transaction. If no row is returned, retrieve the committed record in a subsequent statement at READ COMMITTED, compare its fingerprint, and return the existing response identifier if the payload matches.
At higher isolation levels, handle serialization failures with bounded transaction retries. Do not assume that an empty RETURNING result means the other transaction failed.
Separate database commit from delivery
Sending a notification before the transaction commits creates a failure window: a customer could receive confirmation for a record that never persisted. A transactional outbox makes the intent durable alongside the lead. A separate dispatcher claims outbox records, delivers them, and records the result.
Delivery is still not magically exactly once. If the worker crashes after an external provider accepts a message but before the receipt is stored, the result is ambiguous. Use provider-supported idempotency where available, reconcile against provider receipts, and avoid blind retries when the effect is uncertain.
Protect the booking invariant
A request key cannot prevent two different customers from trying to reserve the same appointment. Enforce capacity separately, for example with a unique constraint for a single-capacity resource and time slot, or an atomic capacity reservation for a multi-capacity resource. Validate timezone, cancellation, rescheduling, and authorization rules independently.
Test the failures you expect to see
Run concurrent requests with the same key and payload; then repeat with different payloads. Simulate a lost HTTP response after commit, a crash before commit, a crash after external delivery, and two distinct keys competing for one slot. Test cross-tenant access and verify that logs omit sensitive customer payloads.
Acceptance is observable: one durable lead for the repeated operation, no cross-tenant disclosure, a conflict for mismatched payload, no capacity violation, and a documented reconciliation path for uncertain delivery.
The goal is not to claim perfect delivery. It is to make every ambiguous state visible, bounded and recoverable.
References: PostgreSQL INSERT, Transaction isolation, DEV AI guidelines.
來源:DEV Community · dev.to