Search code examples
ruby-on-railscachingnetwork-programmingdnsfailover

Refresh Ruby on Rails DNS cache without restarting the service


I'm looking for similar parameter as networkaddress.cache.ttl for JVMs. Setting this value ensures that the DNS entries are refreshed after some interval.

Particularly, this is needed for connection to database and failover purposes.

If I change IP of hostname mysql-master in /etc/hosts (or DNS server), Rails still tries to use old IP address.

The only one thing that helps is Rails restart which is an expensive option.

Many thanks for advice


Solution

  • The root cause is that DNS is not cached in Rails, but once a connection is established, DNS is no longer in the equation. So if DNS transparently changed, Rails is still attached to old IP. And that's still the issue.

    The trick is to use https://github.com/sonots/mysql2-reconnect_with_readonly gem. So it can reconnect the sessions.

    On bare metal you need to do SET GLOBAL read_only = ON; or SET GLOBAL read_only = OFF; if taking mysql as an example to route the connections to another instance.

    On AWS RDS and probably other cloud providers is automatically solved for you.

    More details how new IP can be forced to be resolved from DNS: https://github.com/ankane/distribute_reads/issues/24