Proposal: Detect external side effects inside Active Record transactions

Hi Rails Core team,

I’ve been developing TransactionGuard, a Ruby gem that detects external side effects performed inside Active Record transactions.

For example, sending an HTTP request, delivering an email, or enqueueing a background job inside a transaction can create inconsistencies if the database transaction later rolls back. The external operation cannot automatically be rolled back with the database changes.

TransactionGuard currently provides detection for HTTP requests, email delivery, and background job enqueueing, with configurable warning, raise, and off modes.

I’d appreciate feedback on whether this type of protection could be valuable as an optional Active Record development feature in Rails itself.

Some questions I’d like to discuss:

  • Should Rails provide built-in warnings for external side effects inside transactions?

  • Which operations should be covered, and how could detection avoid false positives?

  • Should this be an opt-in development/testing feature rather than enabled by default?

  • Would this functionality fit better in Active Record or remain a separate gem?

I understand that Rails already provides transaction callbacks, including after_commit, for handling work after a successful commit. My goal is to explore whether additional detection could help developers catch potentially unsafe patterns earlier.

I’d welcome feedback on the use case, design, and whether an upstream contribution would make sense.

Thank you!

1 Like

Tested and working alternative GitHub - palkan/isolator: Detect non-atomic interactions within DB transactions · GitHub

This don’t need to be in rails, the feature is good as a gem. Also rails recently introduced a feature to enqueue jobs only after the transaction commits.

1 Like