Skip to content

fix(triggers): make event processing idempotent under redelivery and retries (EN-1221) - #184

Closed
flemzord wants to merge 1 commit into
mainfrom
fix/event-idempotence
Closed

fix(triggers): make event processing idempotent under redelivery and retries (EN-1221)#184
flemzord wants to merge 1 commit into
mainfrom
fix/event-idempotence

Conversation

@flemzord

Copy link
Copy Markdown
Member

Problem (H1 + M2 + M4)

The event bus is at-least-once.

  • H1 — Only SAVED_PAYMENT/SAVED_ACCOUNT got a deterministic, dedup-ing Temporal workflow id (listener.go:30-51,155-159, commented "Quick hack"). For every other event type ExecuteWorkflow ran with a server-generated id. A redelivery — or a partial-failure NACK after some triggers in the loop had already started — re-executed triggers and replayed side-effecting send stages (real ledger/wallet/PSP movements).
  • M4ExecuteTrigger built the occurrence with uuid.NewString() in workflow code (workflow_trigger.go:71), producing a different occurrence id on every Temporal replay → published SUCCEEDED/FAILED_TRIGGER events could reference an id absent from the table.
  • M2InsertNewInstance, InsertNewStage, InsertTriggerOccurrence did plain INSERTs with deterministic PKs and unlimited retries. A retry after a lost ack (row committed, result lost) fails forever on the duplicate key → wedged workflow.

Fix

  • listener: deterministic workflow id for all events — taskIDPrefix-triggerID-<objectID│msg.UUID> with REJECT_DUPLICATE. msg.UUID is preserved across redeliveries, so a redelivery is rejected as a duplicate.
  • occurrence id: use the deterministic workflow execution id instead of a random uuid generated in workflow code.
  • insert activities: ON CONFLICT DO NOTHING (deterministic PKs make this safe; the returned struct is rebuilt deterministically).

Tests

New TestHandleMessage/redelivery of a non-payment event is skipped (same msg.UUID delivered twice ⇒ a single occurrence). Existing SAVED_PAYMENT dedup and multi-trigger tests still pass.

Severity: HIGH (H1) + MEDIUM (M2, M4) — grouped by root cause (idempotence).

…retries

The event bus is at-least-once. Previously only SAVED_PAYMENT/SAVED_ACCOUNT
got a deterministic, dedup-ing Temporal workflow id; every other event type
ran with a server-generated id, so a redelivery (or a partial-failure NACK
after some triggers had already started) re-executed triggers and replayed
side-effecting stages such as money movements.

- listener: derive a deterministic workflow id for ALL events
  (taskIDPrefix-triggerID-<objectID|msg.UUID>) with REJECT_DUPLICATE, so a
  redelivery is rejected as a duplicate instead of starting a second run. (H1)

- occurrence id: ExecuteTrigger built the occurrence with uuid.NewString()
  in workflow code, yielding a different id on every Temporal replay. Use the
  (deterministic) workflow execution id instead. (M4)

- insert activities: InsertNewInstance, InsertNewStage and
  InsertTriggerOccurrence now use ON CONFLICT DO NOTHING. With deterministic
  primary keys, a retry after a lost ack (row committed, result lost) would
  otherwise fail forever on the duplicate key and wedge the workflow. (M2)

Adds a redelivery regression test for a non-payment event.
@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@flemzord, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 32 minutes and 28 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4c90f1ce-e9ab-49d7-b484-d30388b41d92

📥 Commits

Reviewing files that changed from the base of the PR and between 271bf8d and eeafbde.

📒 Files selected for processing (6)
  • internal/triggers/activities.go
  • internal/triggers/listener.go
  • internal/triggers/listener_test.go
  • internal/triggers/trigger.go
  • internal/triggers/workflow_trigger.go
  • internal/workflow/activities.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/event-idempotence

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@flemzord

flemzord commented Jun 11, 2026

Copy link
Copy Markdown
Member Author

Tracked in Jira: EN-1221 (Epic EN-1217).

@flemzord flemzord changed the title fix(triggers): make event processing idempotent under redelivery and retries fix(triggers): make event processing idempotent under redelivery and retries (EN-1221) Jun 11, 2026
instance := NewInstance(activity.GetInfo(ctx).WorkflowExecution.ID, workflowID)
// Idempotent: the primary key is the (deterministic) workflow execution id,
// so a Temporal retry after a lost ack must not fail on the duplicate key.
// The returned instance is rebuilt deterministically, so it is correct even

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the conflict path this does not actually return the row that was inserted by the first activity attempt; it returns a freshly rebuilt Instance. NewInstance uses time.Now(), and NewStage below does the same for StartedAt, so after a lost activity ack + retry the workflow can continue with newer timestamps and later UpdateInstance/UpdateStage can overwrite the original created_at/started_at. To make the idempotent path correct, please return the existing row on conflict (for example with a follow-up SELECT when RowsAffected == 0, or a RETURNING-based upsert) instead of returning the newly constructed struct.

@flemzord

flemzord commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

Superseded by #199, which consolidates this change with the related reliability and safety fixes on top of the current main branch.

@flemzord flemzord closed this Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant