Ruby on Rails application with frozen connection on PostgreSQL on RDS AWS

Viewed 414

I recent migrate my database from Azure MSSQL to PostgreSQL on AWS on my Ruby on Rails application.

After deploying the application runs normally but after some small time, the application wasn't accessible anymore. That's no information on the logs of application or of the Puma. It's just freezing.

When I try to access the application from a web browser it continues loading the page and nothing came to logs.

I can fix it removing the socket and restarting Puma, but I'm pretty sure that's isn't the right solution.

I also change the adapter and the configuration on config/database.yml.

# PostgreSQL - RDS AWS
staging:
  adapter: postgresql
  host: my-instance.us-east-1.rds.amazonaws.com 
  database: my-db
  username: my-user
  password: my-password
  port: 5432
  pool: 5
  timeout: 5000
  connect_timeout: 5
  read_timeout: 5

# MSSQL - Azure
#staging:
#  adapter: sqlserver
#  host: my-instance.database.secure.windows.net
#  database: my-db
#  username: my-user@my-db
#  password: my-password
#  port: 1433
#  azure: true
#  tds_version: 8.0
#  mode: dblib

And I'm using Puma v3.9.1 with this configuration:

threads 2, 4 
workers 0

on_worker_boot do
  ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
  end 
end

Any ideas? Thank you!

1 Answers

When you have a mysterious problem:

Try to understand it by changing things that are easy to change while telling as much as possible about what is and is not related to it. Think of it like 20 questions where you systemically get closer and closer.

For example: You noticed the problem after changing multiple things, Azure->AWS, MSSQL->PostgreSQL, old setup -> new setup, etc. Either of those changes could be related or unrelated to the problem and you want to know for sure which is what.

First make really, really, really sure you are testing against the webserver and database that you think you are testing against. Change hings in configuration files and make sure you get errors when you should get errors.

You say it works at first and stops working after a while, that sounds like it could be something running out of resources, for example memory, storage or open files. When it has locked up try running (on the machine running Puma), 'dmesg' and look for any kind of errors and warnings, 'df -h' to see disk usage, 'top' to see memory and cpu usage. Try upgrading the instance temporarily to 'large' or whatever is significantly more than you have now.

If you suspect it is related to switching to Postgres (and it is not too much work) you could try reverting the database configuration to point at the MSSQL database in Azure while still running Puma on AWS. You could try temporarily switching to SQLite just to see if the problem is still there. You could try running the old setup on Azure and just changing the database to Postgres there.

You say that nothings gets printed in the logs, I would expect most database issues to show up in the log at least after the request times out. Try changing from Puma to Unicorn (it should be quick and easy) and see if the problem changes or if you get something in the logs at least.

These steps should at least get you much closer to finding the problem.

Related