Quick revision
The short answer
A failover goes through four steps: detection, when health checks or a failover manager decide the primary is down after a timeout; election, when the most up-to-date eligible standby is promoted and the old primary is fenced; redirection, when DNS, a virtual IP, a proxy or the cloud endpoint is pointed at the new primary; and recovery in the application. The Java application survives by dropping dead connections quickly (timeouts, validation, a bounded maxLifetime), retrying failed transactions idempotently, and not caching the old DNS address.
What to remember
In this answer, we followed a failover from detection, through election of the most up-to-date standby and fencing of the old primary, to redirection via DNS, a virtual IP, a proxy or a multi-host connection string. We then saw what the Java application must do: bound every wait with driver and pool timeouts so dead connections fail fast, retry whole transactions idempotently with backoff, and keep the JVM's DNS cache short.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A failover goes through four steps: detection, when health checks or a failover manager decide the primary is down after a timeout; election, when the most up-to-date eligible standby is promoted and the old primary is fenced; redirection, when DNS, a virtual IP, a proxy or the cloud endpoint is pointed at the new primary; and recovery in the application. The Java application survives by dropping dead connections quickly (timeouts, validation, a bounded maxLifetime), retrying failed transactions idempotently, and not caching the old DNS address.
Why this question is asked
Every replicated database will eventually fail over, planned or not, and the database part is usually the quick part. What decides whether users notice is how fast each step happens and whether the application recognises the change. Interviewers ask this to see whether we understand the whole chain, from a server dying to the next successful write, and the parts that belong to us as Java developers. In this answer, we will follow a failover through detection, election, fencing and redirection, then look at what the connection pool, the transaction code and the JVM must do for the application to recover in seconds rather than minutes.
1The Starting Point and the Four Steps
We start with a primary that takes writes and one or more standbys that replay its changes. The application connects to the primary through a name or address and holds a pool of open connections to it. When the primary dies, every one of those connections is now pointing at nothing, and every transaction in flight fails.
- Primary failscrash, hardware or network loss
- Detectionhealth checks miss for 10 to 30 s
- Electionpick the most up-to-date standby
- Fence the old primaryso it cannot accept writes
- Promote the standbyit starts accepting writes
- RedirectionDNS, VIP, proxy or endpoint updated
- Application reconnectsdrops dead connections, retries
7 steps in order. Play them, or step through them one at a time.
Each step has its own timeout; the total is what users feel.
2Detection: Deciding the Primary Is Really Down
Something must notice the failure. That is a failover manager such as Patroni for PostgreSQL or Orchestrator for MySQL, a cluster manager, or the cloud provider's control plane for a managed database. It checks the primary every few seconds and declares it dead only after several missed checks, typically 10 to 30 seconds in total.
The reason for waiting is that a short network hiccup looks exactly like a crash from outside. Failing over too eagerly causes needless outages and, worse, can create two primaries. So detection is always a trade-off between speed and false alarms.
3Election and Fencing
Next, the manager chooses which standby to promote. It must be eligible, healthy and allowed by configuration, and among those it picks the most up-to-date one, the standby that has received the most WAL or the most GTIDs, so that as little data as possible is lost. With synchronous replication, only a standby that was synchronous qualifies, which guarantees no acknowledged commit is lost.
Before or during promotion, the old primary must be fenced, meaning prevented from accepting writes if it comes back. Without fencing, a primary that was only unreachable, not dead, can return and accept writes alongside the new one, which is called split brain. Fencing can mean powering the node off, revoking its network access or having it demote itself when it loses contact with the cluster.
4Redirection: Pointing Clients at the New Primary
The application still has the old address. Something must send new connections to the new primary. The options are a DNS record that is updated (managed cloud databases usually do this, keeping the same endpoint name), a virtual IP that moves to the new server, a proxy in front of the database such as PgBouncer, HAProxy or ProxySQL that changes its backend, or a multi-host connection string in the driver that finds whichever host is primary.
Notice that each option has its own delay. DNS has a time-to-live and clients cache it; a virtual IP moves almost instantly; a proxy switches as soon as its health check notices. That delay adds directly to the outage.
5What the Java Application Must Do
Now the part that is ours. The connection pool, HikariCP in Spring Boot, is full of connections to the dead primary. If a thread borrows one and runs a query, it may hang waiting for a reply that never comes. So the driver needs timeouts: connectTimeout bounds how long opening a connection may take, and socketTimeout bounds how long a read may wait, so a dead connection fails in seconds rather than after the operating system's TCP retransmission limit, which can be around 15 minutes on Linux. Hikari's connectionTimeout limits how long a request waits for a free connection, its keepaliveTime tests idle connections in the background, and a maxLifetime well below any proxy or server idle limit keeps connections recycled.
In-flight transactions fail; there is no way around that. Their effects are either fully committed on the old primary, and replicated or lost, or rolled back. So the application retries the whole transaction on a fresh connection, with backoff. Retrying is only safe if the operation is idempotent, meaning doing it twice has the same effect as once, which is why writes carry an idempotency key, for example a client-generated order id with a unique constraint.
For this to work, @Retryable must sit outside the transaction, so each attempt starts a new transaction on a new connection; both are applied through proxies, and their order matters, so check your configuration.
6The JVM's DNS Cache
One last trap is that the JVM caches DNS lookups. The networkaddress.cache.ttl security property controls how long. When no security manager is installed, the default is 30 seconds, but some environments or older setups cache much longer, even forever. If the cluster endpoint is a DNS name, a long cache keeps new connections going to the old address. We set a short TTL explicitly, for example 5 seconds in the java.security file or with Security.setProperty early in startup, so the redirection actually reaches us.
7Follow-up questions
Why not set socketTimeout very low, like one second?
Because socketTimeout applies to every read on the connection, including legitimate long queries. A report that runs for ten seconds would be killed. We set it above the longest expected query for that pool, often 30 to 60 seconds, and use separate pools for long-running work.
Do managed cloud databases remove the need for any of this?
They handle detection, election, fencing and DNS. They cannot fix the application's pool, timeouts, retries or DNS caching. That is why a managed database can be back in a minute while the application stays broken for much longer.
What happens to a transaction that was committing at the moment of failure?
The client may get an error even though the commit happened on the old primary. Whether the new primary has it depends on replication mode. This uncertainty is exactly why retries must be idempotent: the retry checks whether the order already exists rather than blindly inserting it again.
8Conclusion
In this answer, we followed a failover from detection, through election of the most up-to-date standby and fencing of the old primary, to redirection via DNS, a virtual IP, a proxy or a multi-host connection string. We then saw what the Java application must do: bound every wait with driver and pool timeouts so dead connections fail fast, retry whole transactions idempotently with backoff, and keep the JVM's DNS cache short.
Open with
A failover has four phases. First detection: a failover manager like Patroni or the cloud control plane notices the primary has stopped answering health checks, but waits for several misses, often 10 to 30 seconds, so a network blip does not trigger it.
Then, if they want more
Second election: it picks the most up-to-date eligible standby, only a synchronous one if we run synchronous replication, promotes it, and fences the old primary so it cannot come back and take writes, which would be split brain.
Third redirection: DNS for the endpoint is updated, a virtual IP moves, a proxy like PgBouncer, HAProxy or ProxySQL switches backend, or the JDBC URL lists both hosts with targetServerType=primary. Fourth is the application, and that is our job.
The Hikari pool is full of dead connections, so I need connectTimeout and socketTimeout in the JDBC URL so threads do not hang on dead sockets, a sensible Hikari connectionTimeout and keepaliveTime, and maxLifetime so connections are recycled.
In-flight transactions will fail, so I retry the whole transaction with backoff, and that is only safe if writes are idempotent, for example with a request id and a unique constraint. Finally I keep the JVM DNS cache, networkaddress.cache.ttl, short, around five seconds, so we actually follow the new address.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What happens during a database failover, from the primary failing to the application...": https://scrivio-interview-library.netlify.app/databases/replication-and-ha/d2c114878c/