Check out my latest project PullPreview - Deployment previews for your pull requests, on servers you control

Tip: How to make Rails properly handle database failovers on AWS RDS

Problem: a client had multiple rails apps pointing to a few RDS databases, in multi-AZ mode. However, after initiating a database failover, the apps would appear to be stuck trying to reach the old database master, instead of gracefully killing the stale connection, and reconnecting.

After a few tries and a lot of digging, here is the configuration for Rails' DATABASE_URL that appears to work for our multi-AZ PostgreSQL RDS instances:


Or in a config/database.yml:

host: rds-dbhost
port: 5432
username: dbuser
password: dbpassword
database: dbname
pool: 5
encoding: unicode
reconnect: true
connect_timeout: 2
keepalives_idle: 30
keepalives_interval: 10
keepalives_count: 2
checkout_timeout: 5
reaping_frequency: 10


Relevant documentation: