Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A managed cloud database failed over during peak traffic. The database was back in 60 seconds, but your Spring Boot services returned errors for 12 minutes. Walk through what likely went wrong and how you would fix it.

The database recovered in 60 seconds, but the services kept using what they had: pooled connections to the dead primary with no socket timeout, so threads hung until the operating system gave up on the TCP connection, and an old DNS answer cached in the JVM. Request threads then piled up waiting for those connections, and when things recovered, synchronised retries hammered the new primary. We fix it with connectTimeout and socketTimeout in the JDBC URL, a short DNS TTL, bounded waits and circuit breakers, jittered backoff, and regular failover drills that measure application recovery time.

Why this question is asked

This incident is common enough to be almost a rite of passage. The cloud provider's status page says the failover completed in a minute, yet dashboards show 12 minutes of errors, and the postmortem has to explain the other 11. Interviewers like it because the answer is not in the database at all; it is in timeouts, caches and thread pools that only matter on a bad day. In this answer, we will reconstruct the timeline, then examine each likely cause, dead connections, DNS caching, thread pile-ups and retry storms, with the fix for each, and finish with how to prove it is fixed.

1Reconstructing the Timeline

A managed database failover typically works like this: the provider detects the failed primary, promotes a standby and updates the DNS record of the endpoint to the new address. Clients that open new connections to the endpoint after that reach the new primary. The 60 seconds the provider reports is the time until that new primary accepts connections.

Our services, however, do not open new connections often. HikariCP keeps a pool of long-lived connections, and Tomcat keeps a pool of request threads. Twelve minutes of errors means something kept those pools stuck on the old state long after the database was ready. The number itself is a strong clue, as we will see.

A likely timeline
TimeDatabaseService
0:00Primary failsQueries on pooled connections stop getting replies
0:01New primary accepts writesThreads still blocked reading dead sockets
0:02All 200 request threads busy; new requests time out
0:30 to 2:00Some instances resolve the new address, others use cached DNS
11:00 to 15:00OS TCP retransmissions give up; dead connections finally error
12:00Load spike from retriesSynchronised retries slow recovery further
The database part ends at 0:01; everything after is in the application.

2Cause 1: Threads Hanging on Dead Sockets

When the old primary vanished, the connections in the pool were not closed cleanly; nobody sent a reset. A thread that sends a query on such a connection waits for a reply. Without a socket timeout, the PostgreSQL JDBC driver waits indefinitely, and the operating system keeps retransmitting the unacknowledged data. On Linux, the default tcp_retries2 of 15 means it gives up only after roughly 15 minutes. That matches a 12-minute outage suspiciously well.

The fix is to bound every wait in the driver. connectTimeout limits how long opening a connection may take, and socketTimeout limits how long any read may wait, so a dead connection fails in seconds and Hikari evicts it. TCP keepalive helps idle connections fail too, and Hikari's keepaliveTime tests idle connections in the background.

Properties
# application.properties (timeouts in the URL are seconds, Hikari values are ms)
spring.datasource.url=jdbc:postgresql://orders-db.cluster.example:5432/orders?connectTimeout=5&socketTimeout=30&tcpKeepAlive=true
# how long a request waits for a pooled connection
spring.datasource.hikari.connection-timeout=3000
# test idle connections every minute
spring.datasource.hikari.keepalive-time=60000
# recycle every connection after 10 minutes
spring.datasource.hikari.max-lifetime=600000
spring.datasource.hikari.validation-timeout=2000

Notice that socketTimeout must be longer than the slowest legitimate query in that pool, here 30 seconds. Long reports belong in a separate pool with a longer timeout, rather than raising it for everyone. Some teams also use the cloud provider's own JDBC wrapper, which knows the cluster topology and reconnects faster on failover; check what your provider offers.

3Cause 2: The Old Address in the DNS Cache

Even once connections fail and Hikari opens new ones, it must resolve the endpoint name again. If the JVM cached the old answer, new connections go to the dead address. The networkaddress.cache.ttl property controls this; the default is 30 seconds without a security manager, but container base images, older configurations or a sidecar resolver can cache much longer. The operating system or a local DNS cache can add its own delay.

So we set the TTL explicitly to a few seconds and check the whole chain, JVM, container and host, against the TTL the provider puts on the endpoint record.

Java
// at the very start of main(), before anything resolves a name
java.security.Security.setProperty("networkaddress.cache.ttl", "5");
java.security.Security.setProperty("networkaddress.cache.negative.ttl", "1");

4Cause 3: Requests Piling Up Behind the Database

While a few threads hang on dead sockets, new requests keep arriving. Each one takes a request thread and waits for a database connection. With the default Hikari connectionTimeout of 30 seconds, a request can sit for half a minute before failing, and 200 Tomcat threads fill up in moments. Now even endpoints that do not touch the database, such as health checks, cannot get a thread, the load balancer marks instances unhealthy, and the outage spreads.

The fixes are to fail fast and to isolate. A short connectionTimeout, a few seconds, returns an error quickly instead of holding a thread. A circuit breaker, for example Resilience4j around the repository calls, opens after a burst of database failures and rejects calls immediately for a short time, so threads stay free and the service can still answer health checks and serve cached data. A bulkhead, a limit on how many threads may wait on the database at once, keeps one dependency from consuming the whole pool.

5Cause 4: The Retry Storm After Recovery

When the new primary becomes reachable, every instance and every client retries at once: users press reload, mobile apps retry, our own code retries, and a message consumer replays its backlog. The new primary, with a cold cache, receives several times its normal load in the same second, slows down, and the retries time out and retry again.

The fix is jittered exponential backoff: each retry waits longer than the last, with a random part so that thousands of clients do not retry in step, and a cap on attempts. Retries belong at one layer, not at every layer, because three layers each retrying three times turn one request into 27. Connection pools should also ramp up rather than opening their maximum size instantly.

Java
RetryConfig config = RetryConfig.custom()
.maxAttempts(4)
.intervalFunction(IntervalFunction.ofExponentialRandomBackoff(200, 2.0, 0.5)) // about 200, 400, 800 ms, randomised
.retryExceptions(TransientDataAccessException.class)
.build();

6Proving It With Failover Drills

The lesson of this incident is that database recovery time and application recovery time are different numbers, and only the second one matters to users. So we run failover drills regularly, for example monthly in staging and quarterly in production during low traffic, using the provider's forced failover, while synthetic traffic runs. We measure how long until error rates return to normal, per service, and track that number over time. A target such as application recovery within 90 seconds of database recovery makes regressions visible, for instance when a new service ships without socketTimeout.

7Follow-up questions

Why did some instances recover faster than others?

Instances whose pooled connections happened to be idle, or which were recycled by maxLifetime just after the failover, opened fresh connections and resolved DNS anew. Instances with busy connections stuck on dead sockets waited for TCP to give up. That unevenness is typical and is itself a hint that timeouts are missing.

Would a connection proxy have helped?

A proxy such as RDS Proxy or PgBouncer keeps client connections open while it reconnects to the new primary behind the scenes, which can shorten the visible outage considerably. It does not remove the need for timeouts and retries in the application, but it reduces how much the application sees.

How do you test this without a cloud failover?

In integration tests, Toxiproxy can sit between the service and the database and simulate a dead connection that never answers, which reproduces the hanging-thread problem in seconds. It is a cheap way to verify every service has socket timeouts.

8Conclusion

In this answer, we saw why a 60-second database failover can become 12 minutes of errors. Pooled connections hung on dead sockets until TCP gave up after about 15 minutes, the JVM or OS kept the old DNS answer, request threads piled up behind 30-second waits, and synchronised retries overloaded the new primary. The fixes are driver and pool timeouts, a short DNS TTL, fast failure with circuit breakers and bulkheads, jittered backoff at one layer, and drills that measure application recovery, not database recovery.

What to say in the interviewAbout 80 seconds aloud

Open with

The database was fine after 60 seconds, so I would look at what the services kept from before. Most likely, pooled connections pointed at the dead primary and had no socket timeout.

Then, if they want more

A thread sending a query on one just waits while Linux retransmits, and with the default settings TCP gives up after roughly 15 minutes, which matches 12 minutes of errors. So I would add connectTimeout and socketTimeout to the JDBC URL, enable keepalives, and use Hikari's keepaliveTime and a sensible maxLifetime.

Second, DNS: the endpoint name moved, but the JVM or container may have cached the old address, so I would set networkaddress.cache.ttl to a few seconds. Third, thread exhaustion: with a 30-second Hikari connectionTimeout, every request held a Tomcat thread, the pool filled, even health checks failed and instances were pulled from the load balancer.

I would shorten the wait to a few seconds and add a circuit breaker and a bulkhead around the database. Fourth, the retry storm: once the primary was back, everyone retried at once, so I would use exponential backoff with jitter at one layer only.

Finally, I would run regular failover drills with traffic and measure application recovery time per service, because that is the number users actually feel, not the database's 60 seconds.

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.
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 "A managed cloud database failed over during peak traffic. The database was back in 60...": https://scrivio-interview-library.netlify.app/databases/replication-and-ha/b33d2911f3/