# ADR : Preserve under concurrent writers Status: proposed — template; decision and owner acceptance are not recorded. Date / source revision / owner / supersedes: ## Context and decision trigger Name the exact invariant (for example, capacity cannot go below zero or only a permitted version transition may win). Identify all writers: API requests, scheduled jobs, n8n executions, workers and operator replay. State expected contention, ordering scope, consistency needs and acceptable user-visible conflicts. If no shared mutable invariant exists, record why concurrency control is unnecessary. ## Race scenarios Describe two concrete interleavings with distinct event/request IDs, read/write steps and the invalid final state. A duplicate-key check can pass for both distinct events, so the idempotency ADR does not solve this race. Include out-of-order and stale-version messages where relevant. ## Alternatives and tradeoffs | Option | Appropriate use | Limits to verify | |---|---|---| | Atomic conditional update / uniqueness constraint | A database can express the invariant atomically | Actual constraint/query semantics, conflict responses and multi-row invariants | | Optimistic version / compare-and-set | Contention is manageable and conflicts can be surfaced/retried | Every writer must honor the version; retries must re-read/re-evaluate | | Transaction isolation / row locking | Multi-step database invariant needs transaction protection | Engine/version isolation behavior, deadlocks, lock order and bounded retries | | Keyed queue / serialization | Per-aggregate ordering is an established system boundary | Partitioning/failover and bypass writers; does not automatically make external effects atomic | | Distributed lease/lock with fencing | Cross-process coordination cannot use simpler existing boundaries | Expiry, ownership, stale holders, partition failure and datastore enforcement | A process-local lock, fixed delay or global n8n concurrency setting is not sufficient proof when other processes/writers bypass it. ## Decision to complete Choose the invariant owner and enforcement point; transaction/conditional operation; version/ordering token; supported isolation level; lock/lease scope and fencing if applicable; conflict/error contract; maximum attempts/backoff and retryable versus permanent errors. Do not repeat irreversible external effects inside a transaction retry. Confirm all writers participate or state the remaining gap. ## Validation and acceptance - Use barriers or controlled interleavings to make two writers contend deterministically; assert the final invariant, not merely two successful HTTP responses. - Verify lost-update, over-allocation/duplicate-creation and stale/out-of-order cases relevant to the invariant. - Exercise conflict/deadlock/serialization failure and bounded recovery without losing unrelated writes. - For leases, pause a holder past expiry and demonstrate a stale holder cannot commit protected work. Run against the actual persistence/queue boundary when provider semantics matter; mocked or in-memory tests do not establish its isolation behavior. Record versions, commands and observed final state; no pass is implied by this template. ## Consequences, rollout and rollback Describe contention/latency/availability costs, observability and ownership of conflicts. Backfill version/constraint data safely, maintain compatibility during mixed-version deployment, and avoid rolling back the enforcement while incompatible writers remain active.