How to Choose a Database Failover Plan Your Host Actually Supports
A database failover plan is only real if your host's infrastructure can actually perform the switch you have designed. Before you write a runbook, find out what redundancy you are paying for and what the provider will do when your primary node dies. The plan and the platform have to agree; anything else is a document that fails at the worst moment.
Start by finding out what your database failover plan can rely on
Ask three questions of your provider, in writing. Is there a standby instance of your database in a different failure domain? Is the switch automatic, and who or what triggers it? What is the connection endpoint you are expected to use, and does it move with the primary? The answers determine which of the patterns below you can build.
You can usually learn a lot without asking anyone. Check whether you connect to a single hostname or an address that resolves to one machine. A managed cluster normally hands you a stable endpoint and hides the nodes behind it. A single virtual machine with a database installed on it is just that: one machine, no failover, however the marketing page reads. Resolve your database hostname and look at what comes back.
$ getent hosts db.example.internal
10.20.0.14 db.example.internal
$ dig +short db.example.internal
10.20.0.14
One address is not proof of a single point of failure, because an endpoint can sit behind a proxy that routes to whichever node is primary. But it is a prompt to ask. If the answer is "that is the database server," you are on a single instance and the rest of this article is about what you can do around it.
Also check the replication status from inside the database. On a managed service you may not have shell access, but you often have a read replica you can query, and the primary will report its connected standbys.
SELECT client_addr, state, sync_state, replay_lag
FROM pg_stat_replication;
If that returns rows, something is replicating. If it returns nothing on a server you believe is replicated, either replication is broken or the standby is not where you think it is. Either way, you now know more than the dashboard told you.
Match the failover mechanism to what the provider supports
Managed clusters with an automatic failover
When the provider runs the cluster, the failover is theirs. Your job is to make the application survive it. The most common failure is not the database dying but connections not reconnecting: a pool holds sockets to the old primary, the endpoint moves, and the pool keeps retrying an address that no longer accepts writes. Set a connection lifetime on the pool so stale sockets are retired, and make sure your client library retries on connection errors rather than surfacing them to the user.
In a PostgreSQL connection string, the relevant controls are target_session_attrs and a connect timeout. The first tells the driver to prefer a node that accepts writes, which matters when the endpoint resolves to several addresses.
postgresql://app:[email protected]:5432/app
?target_session_attrs=read-write
&connect_timeout=5
&sslmode=require
Keep the timeout short enough that a dead node is abandoned quickly, and long enough that a slow but healthy one is not. Test it by stopping the primary yourself if you are allowed, or by asking the provider to fail the cluster over during a maintenance window. A failover you have never exercised is a hypothesis.
Replicas you manage yourself
If the provider gives you two servers and replication but no automatic promotion, you own the switch. That means a promotion command, a way to repoint the application, and a decision about what happens to writes that were in flight. The promotion itself is a single command on the standby.
# on the standby, after confirming the primary is unreachable
pg_ctl promote -D /var/lib/postgresql/data
Then you repoint the application, either by changing the hostname in configuration and reloading, or by moving a floating address or DNS record. DNS is the slowest option because of caching, so keep the record's time to live short well before you need it, and do not rely on it for sub-minute recovery. A proxy or a floating IP is faster if your host offers one.
The hard part is not promotion, it is the split brain risk. If the old primary comes back and still believes it is primary, you have two writers. Fencing, meaning making sure the old node cannot accept writes, is what separates a failover plan from a data corruption incident. Some providers fence at the network layer; if yours does not, you must do it manually, and your runbook should say so in the first line.
No replication at all
If your host offers a single database instance, your failover plan is a restore plan. That is not a failure of imagination, it is the honest description. Know where your backups live, how often they are taken, and how long a restore takes. Then reduce the blast radius: keep the database on its own server so a web node can be replaced without touching it, and make sure the application can start and serve read-only pages while the database is unavailable. A maintenance page that says "we will be back" is better than a stack trace.
Write the runbook around decisions, not commands
A database failover plan earns its keep when it tells the person on call what to decide, not just what to type. Include the signal that triggers the plan, who is allowed to declare a failover, and the point at which you stop trying to save the old primary. Include the check that confirms the new primary is genuinely accepting writes before you send traffic to it, and the check that confirms the old one is fenced. Include the step where you tell the provider, because they may be mid-incident on the same hardware and their view matters.
Rehearse it. Pick a quiet period, promote the standby, watch the application recover, then rebuild replication in the other direction. The first time you run the procedure should not be the first time anyone has read it. Record how long each step took, because that number is what you tell people when they ask how long an outage will last.
Next, pull up your database connection configuration and your provider's documentation side by side. Confirm whether you have a standby, whether promotion is automatic, and what the endpoint does during a switch. If any of those answers is "I am not sure," that is the gap to close first, before you write a single line of the runbook.
