Appearance
ADR-2526: Idempotent Self-Service Participant Expense Claims and Wallet Disbursements
- Status: Accepted
- Date: 2026-11-20
- Authors: @fderuiter
- Deciders: @fderuiter
- Requirements: PRD-PAT-002, PRD-PAY-006, PRD-SYS-182
1. Context & Problem Statement
Participants submitting travel or meal reimbursement receipts (POST /api/v1/payments/participant/me/expenses) or requesting wallet balance payouts (POST /api/v1/payments/participant/me/disburse) from mobile networks frequently encounter transient packet loss, slow cellular connections, or repeated button taps (PRD-PAT-002, PRD-PAY-006, PRD-SYS-182). Without persisted server-side idempotency keys scoped to each participant wallet, retrying an in-flight reimbursement claim or balance disbursement could create duplicate ParticipantExpenseClaim or ParticipantDisbursement ledger records, double-credit auto-approved reimbursements, or double-debit available wallet balances.
2. Decision Drivers & Constraints
- Financial Ledger Integrity: Replaying a participant expense claim or payout request with the same
Idempotency-Keymust return the original evaluation or disbursement outcome without mutatingParticipantWalletbalances twice or dispatching duplicate ACH/virtual-card transfers. - Per-Wallet Tenant & Caller Isolation (ADR-2817): Idempotency keys must be scoped to
(WalletId, IdempotencyKey)so keys remain isolated per caller-bound participant wallet without cross-participant correlation. - Concurrency Safety: Simultaneous duplicate submissions racing across connections must be guarded by database-level unique constraints and deterministic conflict recovery rather than best-effort in-memory caches.
- Clinical Kernel Boundary Compliance (
K-XS-1,K-TIME-3): Public DTOs inCadence.Apps.Paymentsmust avoid rawStudyId/SiteIdvalue-type drift and derive timestamps exclusively viaIClock.
3. Options Considered
Option 1: Persisted IdempotencyKey Columns with Unique (WalletId, IdempotencyKey) Indexes (Selected)
- Overview: Persist nullable
IdempotencyKey(varchar(255)) onParticipantExpenseClaimandParticipantDisbursementwith unique composite indexes on(WalletId, IdempotencyKey), acceptIdempotency-Keyvia HTTP header or request payload, short-circuit matching replays prior to mutation, and recover cleanly from concurrentDbUpdateExceptionunique-index races. Pair with client-side per-draftIdempotency-Keypersistence and post-success form reset inapps/subject-portal. - Pros:
- Guarantees exactly-once ledger mutation across restarts, horizontal replicas, and concurrent retries.
- Preserves full 21 CFR Part 11 audit trail fidelity without emitting duplicate disbursement events on replay.
- Backward-compatible for callers omitting
Idempotency-Key(NULLkeys are not constrained against each other in PostgreSQL unique indexes).
- Cons:
- Requires an EF Core schema migration (
AddSelfServiceIdempotencyKeys) onpayments_participant_expense_claimsandpayments_participant_disbursements.
- Requires an EF Core schema migration (
Option 2: Ephemeral In-Memory / Distributed Cache Deduplication Window
- Overview: Store recent
Idempotency-Keyresponses in process memory or short-TTL cache without persisting the key on the financial ledger entities. - Pros:
- Avoids an EF Core schema migration.
- Cons:
- Loses deduplication guarantees across process restarts or cache evictions, risking duplicate regulated payouts.
4. Decision Outcome
- Chosen Option: Option 1
- Justification: Persisting
IdempotencyKeydirectly onParticipantExpenseClaimandParticipantDisbursementwith database-enforced(WalletId, IdempotencyKey)uniqueness provides durable, restart-safe financial idempotency aligned with ADR-2817 caller isolation.
5. Consequences & Trade-offs
- Positive Impact: Mobile participants can safely retry receipt claims and confirmed
DIRECT_ACHorVIRTUAL_CARDpayouts over unreliable connections with zero duplicate ledger mutations. - Negative Impact / Technical Debt: Adds two indexed
varchar(255)columns to the Payments persistence schema. - Mitigation Strategy: Synchronized across
docs/schema/payments.mdanddocs/openapi/payments_openapi.jsonand verified by sentinel drift gates.
6. Implementation & Verification
- Affected Repositories / Services:
apps/payments(PaymentEntities.cs,PaymentConfigurations.cs,PaymentEndpoints.cs, migration20261121150826_AddSelfServiceIdempotencyKeys),apps/subject-portal(features/wallet.js,views/WalletView.vue),docs/openapi/payments_openapi.json, anddocs/schema/payments.md. - Verification Plan: Automated xUnit idempotency replay and wallet isolation tests in
tests/Cadence.UnitTests/Payments/ParticipantSelfServiceAndOcrTests.cs, Vitest unit tests inapps/subject-portal/tests/expense-ocr-autodisburse.test.js, and Playwright E2E verification inapps/subject-portal/tests/e2e/wallet-recovery-and-touch-targets.spec.js.