fix(dataplane): close idempotency dedup race with transactional advisory lock claim#2747
Merged
mekilis merged 1 commit intoJul 20, 2026
Conversation
the duplicate check for idempotency keys ran in the async workers as check-then-insert, so two creates with the same key inside the race window could both pass the check and both be persisted and delivered as non-duplicates. move the authoritative duplicate check into the event repository's CreateEvent transaction, guarded by a transaction-scoped postgres advisory lock (pg_advisory_xact_lock) keyed on project id + idempotency key. the lock serializes same-key writers across all instances and the re-check under the lock sees rows committed by earlier holders, so exactly one row per key is persisted with is_duplicate_event=false. an advisory lock is used instead of a unique index because the events table can be partitioned by (project_id, created_at), which a unique index on (project_id, idempotency_key) cannot span. the workers' existing pre-checks are kept as a cheap short-circuit for sequential duplicates; events already flagged duplicate skip the claim. failure policy: fail closed, a lock or lookup error aborts the create and the worker retries.
|
Current version of PR was reviewed by /review-bugbot on Jul 20, 15:50 GMT+1. It flagged 0 findings. Bugbot on commit |
mekilis
enabled auto-merge
July 20, 2026 15:48
mekilis
deleted the
smart/pde-929-events-idempotency-dedup-races-on-near-simultaneous-submits
branch
July 20, 2026 15:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pg_advisory_xact_lock(hashtextextended('events-idempotency:<project>:<key>', 0)); the re-check under the lock sees rows committed by earlier holders and flipsIsDuplicateEvent, so exactly one row per key persists as non-duplicate. Lock or lookup errors fail closed and the worker retries.(project_id, idempotency_key)cannot satisfy.Design notes
FindFirstEventWithIdempotencyKey, non-duplicate rows only) diverges from the claim's (FindEventsByIdempotencyKey, any row). They only disagree if a key's original non-duplicate row was deleted while duplicate rows survive; the claim errs toward flagging the new event duplicate, which is the safe direction.Test plan
go test ./internal/events/(concurrent same-key creates yield exactly one non-duplicate row; lock/lookup error path fails closed)Fixes PDE-929