Course contentsShow
← Databases & SQL
Lesson 1889 of 1,95318. Application Integration PatternsFree lesson

Optimizing for High-Latency Networks

Adjust connection timeout and keep-alive settings for database connections traversing cross-region network infrastructure.

In this lesson, you will learn how to fine-tune your connection parameters to maintain stable database sessions when crossing high-latency, cross-region network infrastructure.

When your application server is in one cloud region (e.g., US-East) and your database resides in another (e.g., AP-Southeast), every packet exchange is subject to increased Round Trip Time (RTT). Standard connection settings are often too aggressive, leading to "false negatives" where the client prematurely kills a healthy but slow connection, or "zombie connections" where the network drops the state but the application thinks it is still connected.

Adjusting Connection Timeouts

Standard connectTimeout values (often 5–10 seconds) are insufficient for high-latency inter-region links. If the network experiences a momentary spike or jitter, the connection attempt may time out before the TCP handshake completes. Increase your connectTimeout to accommodate the 99th percentile of your cross-region RTT. Furthermore, ensure your socketTimeout is sufficiently high so that long-running queries aren't severed simply because the network propagation delay makes the response appear "stalled" to the driver.

Tuning TCP Keep-Alives

By default, idle TCP connections can be silently dropped by stateful firewalls or load balancers in the middle of a cross-region path. To prevent this, you must configure TCP keep-alives at the OS or driver level to send periodic "heartbeat" packets to keep the connection state active in intermediate devices.

/* Example configuration for a JDBC-based connection string */
jdbc:postgresql://db-host:5432/mydb?
  connectTimeout=30&
  socketTimeout=60&
  tcpKeepAlive=true&
  tcpKeepAliveCount=3&
  tcpKeepAliveInterval=30

By setting tcpKeepAlive=true, you ensure the driver sends a packet after a period of inactivity. If the gateway device receives these, it won’t reap the idle connection. Setting the tcpKeepAliveInterval to a value lower than your network’s idle timeout (usually 300 seconds for most cloud load balancers) ensures the connection remains warm, preventing the overhead of re-establishing a TLS handshake for every interaction.

Key Takeaway: When traversing high-latency links, increase connection timeouts to prevent premature failure and enable TCP keep-alives to signal active connections through intermediary network infrastructure.