Atomic transactions across multiple MySQL databases for Active Record.
Nested transaction blocks on different databases look atomic, but they are two independent transactions:
PrimaryRecord.transaction do
TrackerRecord.transaction do
TrackerEvent.create!(name: "order placed")
end # the tracker commits here
Order.create!(total: 0) # raises: the order rolls back,
end # the tracker event staysActiveRecord::XA runs them as one transaction using MySQL's XA two-phase commit, so every database commits or none does:
ActiveRecord::XA.transaction(PrimaryRecord, TrackerRecord) do
TrackerEvent.create!(name: "order placed")
Order.create!(total: 0) # raises: both roll back
endInside the block you use models exactly as normal. The example app shows it side by side with plain nested transactions, including crashes and recovery.
- All or nothing across databases. A failure anywhere, at any point before the commit decision, rolls back every database.
- Survives crashes. The decision is written to a durable log before anything commits. If the process dies halfway,
ActiveRecord::XA.recover!finishes the job, the same way on every database. - Plain Rails inside. Validations, callbacks,
requires_newsavepoints,ActiveRecord::Rollback, isolation levels and earlyreturnall behave as they do in a normal transaction. after_commityou can trust. Callbacks run once the data is durable on every database, so a job enqueued fromafter_commitnever sees half a transaction.- Pay only for what you touch. A database the block never queries never joins. If only one is touched, the commit is a single
XA COMMIT ... ONE PHASEwith no log write. - Small, with nothing to run. About 650 lines of code on top of Active Record; no coordinator service, no queue, no extra infrastructure.
# Gemfile
gem "activerecord-xa"bundle install
bin/rails generate xa:install # add --database NAME if the log should live elsewhere
bin/rails db:migrateThe generator writes config/initializers/xa.rb and a migration for the decision log table. List your databases in the initializer:
ActiveRecord::XA.configure do |config|
config.decision_log_class = "ApplicationRecord" # where the decision log lives
config.participants = %w[ApplicationRecord LedgerRecord]
config.on_incomplete_commit = ->(gtrid, failures) { Rails.error.report(ActiveRecord::XA::Error.new("XA in doubt: #{ gtrid }")) }
endThen run recovery on a schedule, for example every minute with Solid Queue:
# config/recurring.yml
production:
xa_recovery:
command: "ActiveRecord::XA.recover!"
schedule: every minuteor from cron with bin/rails xa:recover.
class Transfer
def self.call(from:, to:, amount:)
ActiveRecord::XA.transaction(ApplicationRecord, LedgerRecord) do
from.lock!.decrement!(:balance, amount)
to.lock!.increment!(:balance, amount)
LedgerEntry.create!(from_id: from.id, to_id: to.id, amount:)
end
end
endIf the ledger insert fails, neither balance changes.
ActiveRecord::XA.transaction(ApplicationRecord, TrackingRecord) do
order = Order.create!(order_params)
TrackingEvent.create!(name: "order.created", subject: order)
endActiveRecord::XA.transaction(ApplicationRecord, LedgerRecord) do
invoice = Invoice.create!(attrs)
LedgerEntry.create!(invoice:)
raise ActiveRecord::Rollback if invoice.total.zero? # both roll back, nothing is raised
endorder = ActiveRecord::XA.transaction(ApplicationRecord, LedgerRecord) do
Order.create!(attrs).tap { |o| LedgerEntry.create!(order: o) }
end
def place(attrs)
ActiveRecord::XA.transaction(ApplicationRecord, LedgerRecord) do
order = Order.create!(attrs)
return order if order.free? # commits, as `return` does in a Rails transaction
LedgerEntry.create!(order:)
order
end
endclass Order < ApplicationRecord
after_create_commit { FulfilmentJob.perform_later(self) } # runs once both databases have committed
after_rollback { Rails.logger.info("order #{ id } rolled back") }
endbefore_commit callbacks run while every database is still open, so they may write to either one, and records saved by another database's callbacks get theirs too.
ActiveRecord::XA.transaction(ApplicationRecord, LedgerRecord) do
order = Order.create!(attrs)
Order.transaction(requires_new: true) do # a savepoint inside the XA branch
Coupon.redeem!(code)
rescue Coupon::Expired
raise ActiveRecord::Rollback # undoes only the coupon
end
LedgerEntry.create!(order:)
endA deadlock victim rolls back everywhere and raises ActiveRecord::Deadlocked, so the usual retry works:
attempts = 0
begin
Transfer.call(from:, to:, amount:)
rescue ActiveRecord::Deadlocked
retry if (attempts += 1) < 3
raise
endbegin
ActiveRecord::XA.transaction(ApplicationRecord, LedgerRecord) { ... }
rescue ActiveRecord::XA::PrepareFailed, ActiveRecord::XA::DecisionLogFailed
# Rolled back everywhere. Safe to retry.
rescue ActiveRecord::XA::OutcomeUnknown
# The decision log was unreachable at the critical moment. Nothing was
# rolled back; recovery will finish the transaction one way or the other.
# Do not retry blindly: check whether the work landed first.
endOnce the commit decision is logged the transaction counts as committed. A database that then fails to apply XA COMMIT does not raise: on_incomplete_commit is called and recovery applies it.
report = ActiveRecord::XA.recover!
report.committed # => [#<ActiveRecord::XA::Xid ...>] branches committed from the log
report.rolled_back # => branches with no decision, rolled back (presumed abort)
report.skipped # => branches younger than the grace period
report.errors # => [[subject, exception], ...]Two-phase commit costs extra round trips and extra durable writes. A transaction that writes one row to each of two databases:
| Statements | Durable flushes | |
|---|---|---|
| Nested plain transactions | 6 | 2 |
| ActiveRecord::XA, one database touched | 4 | 1 |
| ActiveRecord::XA, two databases | 12 | 6 |
Measured with bundle exec rake bench (2,000 transactions each, one thread, Ruby 4.0.10, Rails 8.1.4, MySQL 9.7 on the same Apple M2 laptop, innodb_flush_log_at_trx_commit=1, sync_binlog=1):
| Scenario | Mean | p50 | p99 | Transactions/s |
|---|---|---|---|---|
| One database, plain transaction | 0.18 ms | 0.16 ms | 0.47 ms | 5,416 |
| Two databases, nested plain transactions (not atomic) | 0.35 ms | 0.33 ms | 0.74 ms | 2,838 |
| ActiveRecord::XA, one database touched | 0.23 ms | 0.21 ms | 0.69 ms | 4,347 |
| ActiveRecord::XA, two databases | 0.87 ms | 0.82 ms | 1.53 ms | 1,149 |
In short: atomicity across two databases costs about half a millisecond per transaction here, and a transaction that only touches one database costs almost nothing extra. On Linux servers, where each flush reaches stable storage, the gap grows with your storage's fsync latency, since ActiveRecord::XA does three times as many. Run the benchmark on your own hardware before relying on these numbers.
- A normal Rails transaction is opened on each database, but the adapter issues
XA STARTinstead ofBEGIN, lazily, when the database is first queried. - Your block runs.
- While all the transactions are still open, ActiveRecord::XA runs
before_commitcallbacks, thenXA ENDandXA PREPAREon every database. Each database has now promised it can commit. - A commit decision is written to the decision log on a separate connection. This is the point of no return.
XA COMMITruns on every database, then the decision is deleted.after_commitcallbacks run.
Recovery resolves anything a crash leaves prepared: a branch whose transaction has a logged decision is committed, one without is rolled back. Before rolling back, recovery writes an abort marker, so a coordinator that turns out to be alive cannot commit afterwards. docs/ARCHITECTURE.md has the details.
- No isolation across databases. Between the first and last
XA COMMIT(normally microseconds), a reader can see one database's new data without the other's. - Locks wait for recovery. A prepared branch holds its row locks until it is resolved. If a database becomes unreachable after
PREPARE, those rows stay locked until recovery runs, so run it often. - No DDL inside the block. MySQL does not allow implicit commits in an XA transaction.
- ActiveRecord::XA must open the outermost transaction on every participant; it raises
TransactionAlreadyOpenotherwise. That also means transactional tests cannot wrap ActiveRecord::XA code: use truncation for those tests.
- MySQL 8.0.30 or later with InnoDB, row-based or mixed binary logging, and no replication filters on the tables involved.
- Rails 8.1 with
mysql2(Trilogy is hooked the same way but untested). - Ruby 4.0 or later.
- The recovery user needs the
XA_RECOVER_ADMINprivilege. Check that your managed host allows external XA. - The decision log takes a second connection from its pool for a moment at each two-database commit. Size that pool for it.
- Architecture: the commit protocol, recovery, clocks and failure handling
- Install & setup: setup in an app, and working on the gem
- Usage: configuration, errors and the recovery report
- Changelog · Contributing · Security
bin/setup # bundle install, create xa_primary / xa_tracker and the xa user
bundle exec rake # tests, against a real MySQL server
bundle exec rubocop
bundle exec rake bench # the benchmark abovePoint the tests at another server with MYSQL_HOST, MYSQL_TCP_PORT, MYSQL_USER and MYSQL_PASSWORD. Set SQL_LOG=1 to see the XA statements. The tests drop tables and roll back every prepared XA branch they can see, so never run them against a server with data you care about.
MIT, © 2026 Vlad Radulescu, Studio51 Solutions.