Skip to content

Avoid replica connection checkout/checkin for every read query - #298

Merged
mateuscruz merged 1 commit into
Nasdaq:mainfrom
bmak-lightspeed:lease_replica_conn
Aug 15, 2026
Merged

Avoid replica connection checkout/checkin for every read query#298
mateuscruz merged 1 commit into
Nasdaq:mainfrom
bmak-lightspeed:lease_replica_conn

Conversation

@bmak-lightspeed

Copy link
Copy Markdown
Contributor

We migrated from Makara to this project, but we found that reads were considerably slower than Makara.

After looking at the code we realized that PrimaryReplicaProxy currently checks out and checks in a replica connection for every read query. Each checkout can cause Active Record to verify the connection by issuing a ; query, adding unnecessary database round trips and latency.

Use replica_pool.connection or replica_pool.lease_connection instead, allowing Rails to manage connection cleanup and check-in at the end of the request or Sidekiq job. This follows the same mechanism Rails uses when obtaining the primary connection, leveraging Rails' existing primary-connection check-in behavior to automatically check in the replica connection as well.

This avoids per-query connection verification while preserving existing connection-pooling behavior and significantly improves read-heavy request performance.

@mateuscruz

Copy link
Copy Markdown
Member

Hello @bmak-lightspeed. Thanks for the contribution. Can you share benchmarks that support your claim, for documentation purposes?

Comment thread lib/active_record_proxy_adapters/primary_replica_proxy.rb Outdated
@bmak-lightspeed

Copy link
Copy Markdown
Contributor Author

Hi @mateuscruz,

Sorry for the delay (I was on vaca).

Disabled

I don't have benchmarks, but here's picture of a flame graph of a request when this patch is NOT enabled. You can see that every read query makes a "PG active? probe" query which translates to a ; query to the db.

Normally queries sent to the PG gem create a span, but since the query is just ; it does not create a span. However, it is still incurring a round trip cost to to go the db and back to make this query.

Screenshot 2026-08-11 at 2 00 49 PM (I had to redact some info)

Enabled

When this change is enabled you can see that only the first read query incurs the checkout cost:
Screenshot 2026-08-11 at 2 12 20 PM

Fwiw this change does assume the user is using Rails which automatically returns the connection back to the pool after each request / job. This could be a separate adapter if we want to also maintain support for non Rails web frameworks.

@bmak-lightspeed

Copy link
Copy Markdown
Contributor Author

fwiw I'd like to backfill this into the v9 stable branch if possible

Comment thread lib/active_record_proxy_adapters/primary_replica_proxy.rb Outdated
@mateuscruz

Copy link
Copy Markdown
Member

@bmak-lightspeed what Ruby and Rails versions is your project running on?

@mateuscruz

Copy link
Copy Markdown
Member

Ugh. Github does not sign rebased commits anymore. @bmak-lightspeed can you rebase locally?

@bmak-lightspeed

Copy link
Copy Markdown
Contributor Author

Just force pushed with your fix.
We're currently using 7.1.6 (yeah, we know we need to update and we're working on it)
It would be nice for this to work for the version we're using atm though.

@mateuscruz

mateuscruz commented Aug 12, 2026

Copy link
Copy Markdown
Member

@bmak-lightspeed some unit tests need to be fixed there.

It would be nice for this to work for the version we're using atm though.

I can backport it.

Comment thread spec/active_record_proxy_adapters/primary_replica_proxy_spec.rb
@bmak-lightspeed

Copy link
Copy Markdown
Contributor Author

As far as I can tell the tests seem to run fine now, but there's some issue with the coverage report. I believe that is unrelated to the change.

@mateuscruz
mateuscruz merged commit fb9f5cc into Nasdaq:main Aug 15, 2026
13 of 14 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.

2 participants