Skip to content

Non-atomic notifications support#84

Merged
gsmetal merged 1 commit intomasterfrom
feat/non-atomic-notifications
Mar 26, 2026
Merged

Non-atomic notifications support#84
gsmetal merged 1 commit intomasterfrom
feat/non-atomic-notifications

Conversation

@gsmetal
Copy link
Copy Markdown
Member

@gsmetal gsmetal commented Mar 26, 2026

Problem

The FOR UPDATE lock on artery_model_infos is acquired inside the model's before_commit callback, meaning it is held from before_commit until the model transaction commits. Under high concurrency (30-50 Sidekiq threads writing the same model), threads queue on this lock for 7-11 seconds, causing database timeouts.

Solution

Add a per-model option non_atomic_notification that moves artery message creation from before_commit (inside the model transaction) to after_commit (in a separate short transaction). This reduces lock hold time from "remainder of the model transaction" to ~2-5ms (just the INSERT + UPDATE + COMMIT of the artery message).

# High-concurrency model — lock in a separate short transaction
artery_model source: true, non_atomic_notification: true

# Default — lock inside model transaction (fully atomic)
artery_model source: true

Trade-offs

  • Default (non_atomic_notification: false): artery message creation is atomic with the model change. If message creation fails, the entire transaction rolls back. No risk of missing notifications.
  • non_atomic_notification: true: artery message creation happens after the model transaction commits. If it fails, the model change is already persisted but the notification may be lost. Retry logic (up to 3 attempts) mitigates transient failures; persistent failures are reported via Artery.handle_error.

This should help to handle very-loaded models and reduce lock wait time
@gsmetal gsmetal requested a review from IgRich March 26, 2026 10:43
@gsmetal gsmetal self-assigned this Mar 26, 2026
@gsmetal gsmetal marked this pull request as ready for review March 26, 2026 10:43
@gsmetal gsmetal merged commit 7b87a25 into master Mar 26, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant