postgres jdbc driver failover questions

55 views Asked by At

I have 2 DB instances, 1 master node1 1 read replica node2.

I have 2 apps. 1 for writing app1 one for reading app2

Here is the connection string for app1 jdbc:postgresql://node1,node2/db?targetServerType=primary.

Here is the connection string for app2 jdbc:postgresql://node1,node2/db?targetServerType=preferSecondary .

When node1 fails, and node2 gets promoted to master, will jdbc driver in app1 auto connect to the node2 for writing? Will the jdbc driver in app2 start checking if a read replica exist and switch traffic if so (when node1 come up)?

1

There are 1 answers

1
Laurenz Albe On

The client connected to the failed primary server will connect to the promoted standby when it tries to reconnect. (But you must write the application so that it tries to reconnect.)

The client connected to the standby will stay connected when the standby is promoted. It won't switch over to a new standby server, unless you do that deliberately in your application. To test if you are connected to a standby server, you can run

SELECT pg_is_in_recovery();

That will return TRUE on the standby.