# \[Feature Proposal\] Active Record Connection Maximum Lifetime

**URL:** <https://discuss.rubyonrails.org/t/feature-proposal-active-record-connection-maximum-lifetime/79249>\
**Category:** rubyonrails-core\
**Tags:** activerecord\
**Created:** [November 6, 2021, 8:59pm UTC](https://discuss.rubyonrails.org/t/feature-proposal-active-record-connection-maximum-lifetime/79249 "2021-11-06T20:59:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![philmonroe](https://avatars.discourse-cdn.com/v4/letter/p/8dc957/32.png) [@philmonroe](https://discuss.rubyonrails.org/u/philmonroe)\
**Post date:** [November 6, 2021, 8:59pm UTC](https://discuss.rubyonrails.org/t/feature-proposal-active-record-connection-maximum-lifetime/79249/1 "2021-11-06T20:59:28Z")

</div>

We are seeing database scalability issues when working with AWS Aurora Postgresql read replica autoscaling. Specifically, after a new read replica becomes available, connections are not rebalanced across the read replica cluster, causing wasted resources. This largely happens because AWS RDS primary read-only endpoint seems to be implemented using DNS load balancing across the read replicas. Once the connection is established, the opportunity to load balance no longer exists.

A potential solution to this would be allowing the `ActiveRecord::ConnectionAdapters::ConnectionPool` to have an optional `lifetime_timeout`, which would force a connection to be reconnected. It could likely be implemented as simply as disconnecting and removing a connection from the pool when it is checked back in and the connection has been alive for longer than the configured timeout.

I would presume the default behavior for a connection pool would be to not need to recycle connections unless explicitly configured to. The likely values someone would want to set a `lifetime_timeout` to would be in the 10 minutes to 1-hour range. This would allow the system to still benefit from connection pooling/reuse while still allowing for periodic cycling behavior.

There are additional benefits to connection maximum lifetimes with PostgreSQL, such as allowing PostgreSQL to more efficiently manage used memory and caches.

> **[Is a connection max lifetime useful in a connection pool?](https://www.postgresql.org/message-id/CA%2Bmi_8bnvpxHZtb6EgHSHY-xn29W8VJMzjPU3fiCOv1bfjrNuA%40mail.gmail.com)**
>
> Hello, I am designing and implementing a connection pool for psycopg3 \[1\]\[2\]. Some of the inspiration is coming from HikariCP …

Does this seem like a beneficial addition to an `ActiveRecord::ConnectionAdapters::ConnectionPool` or are there alternatives we have missed and should consider? Happy to attempt a PR if the idea seems worthy.

---

<div class="post-metadata">

**Author:** ![ghiculescu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/ghiculescu/32/1145_2.png) [@ghiculescu](https://discuss.rubyonrails.org/u/ghiculescu)\
**Post date:** [April 10, 2026, 5:21am UTC](https://discuss.rubyonrails.org/t/feature-proposal-active-record-connection-maximum-lifetime/79249/2 "2026-04-10T05:21:06Z")

</div>

Looks like this is in Rails 8.1 See [[Feature Proposal] ActiveRecord Connection Lifetime (Age) - #5 by andatki](https://discuss.rubyonrails.org/t/feature-proposal-activerecord-connection-lifetime-age/78620/5) and `max_age` in [ActiveRecord::ConnectionAdapters::ConnectionPool](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html#class-ActiveRecord::ConnectionAdapters::ConnectionPool-label-Obtaining+-28checking+out-29+a+connection)

---

<div class="post-metadata">

**Author:** ![kyrofa](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/kyrofa/32/3830_2.png) [@kyrofa](https://discuss.rubyonrails.org/u/kyrofa)\
**Post date:** [May 21, 2026, 10:47pm UTC](https://discuss.rubyonrails.org/t/feature-proposal-active-record-connection-maximum-lifetime/79249/3 "2026-05-21T22:47:28Z")

</div>

There’s a [backport gem](https://discuss.rubyonrails.org/t/feature-proposal-activerecord-connection-lifetime-age/78620/7) for this as well named [activerecord\_connection\_reaper](https://github.com/miriamtech/activerecord_connection_reaper) that supports older versions of Ruby/Rails.
