Reconnect on connection failure for Ecto and Postgres

Hello there.

I’m using ecto 3.10.3, ecto_sql 3.10.1 and postgrex0.17.2 on a rather simple service. For reason beyond my control my Postgres DB faces some small downtimes every now and then. This trigger alerts that are annoying for my team.

I wanted to make my service more resilient to those downtimes. Specifically, I wanted to have my service trying to reconnect to the DB for a couple of minutes before crashing/logging errors.

I’ve found that postgresx has some configuration options like backoff_type and max_restarts, which I think could help me. However, I see that on ecto_sql those seem to be forced to be backoff_type: :stop and max_restarts: 0.

Is there any other way I can make my system more resilient to small DB downtimes?

Thanks

:wave: @vitorquintanilha

I’m pretty sure reconnections are on by default :slight_smile:

However, I see that on ecto_sql those seem to be forced to be backoff_type: :stop and max_restarts: 0 .

Would you be able to link it? GitHub search doesn’t seem to pick these up: Code search results · GitHub

Can you provide the error message? And the scenario of that messages? Given what you said I’m assuming it is actually breaking your app and on app restart it doesn’t apply the backoff strategy.

afaik backoff_type uses the default of db_connection that is :rand_exp. but anyway, you can override that configuration on your config
https://hexdocs.pm/db_connection/2.4.1/DBConnection.html#start_link/2

you could do:

config :your_app, YourApp.Repo,
  backoff_type: :rand_exp,
  backoff_max: ...,
  backoff_min: ...,
  max_restarts: ...
1 Like