PSA on Ecto "connection not available and request was dropped from queue after 2028ms. This means requests are coming in and your connection pool cannot serve them fast enough"

This is one of those “I don’t know what I’m doing, but I figured out a fix so if you hit the same problem here’s what to try” things. I’ve been experimenting with various deployment strategies for some personal funemployment projects and one is to have a single managed Postgres database on Digital Ocean serving various applications on cheap Droplets; when I say various I’ve now got two things deployed this way and had a frustrating time last night getting the second Postgrex/Ecto connection to work.

connection not available and request was dropped from queue after 2028ms. This means requests are coming in and your connection pool cannot serve them fast enough. You can address this by:

  1. Ensuring your database is available and that you can connect to it
  2. Tracking down slow queries and making sure they are running fast enough
  3. Increasing the pool_size (albeit it increases resource consumption)
  4. Allowing requests to wait longer by increasing :queue_target and :queue_interval

See DBConnection.start_link/2 for more information

After several hours futzing around trying different ways to connect I found that all I needed to do was to add ssl: true to the Ecto Repo configuration.

config :tchest, Tchest.Repo,
  url: System.get_env("TCHEST_DATABASE_URL"),
  ssl: true,
  pool_size: String.to_integer(System.fetch_env!("TCHEST_POOL_SIZE") || "10")

The other working deployment did not have this, but I’ve just checked and it is on Ecto v 3.5.8 / Ecto Sql 3.5.4 and the newer deployment is 3.6.1 for both, which probably explains it.

1 Like

so your app could not connect at all and you were getting this error, or this error was there happening from time to time but your app otherwise worked?

It could not connect at all. I found this earlier thread on intermittent errors which is a different thing (I suspect being insufficient connections available in Postgres itself).

(If I were to try and debug the differences between the Ecto (or other lib) versions I’d start by looking at the parameters to Postgrex.start_link/1 and how/where the connection url is parsed.)