Skip to content

Setting Driver or OS TCP Keepalive

Brett Wooldridge edited this page Nov 17, 2024 · 2 revisions

Important

In order to maintain a healthy/reliable pool it is important that TCP keepalive is configured, on the driver, database, and at the operating system level (where access is availalbe).

We recommend configuring all of them, ensuring it is enabled in the driver, ensuring that the database has resonable idle and interval values, and ensuring that the OS has keepalive enabled with resonable defaults.

Driver Support

Driver Property
PostgreSQL tcpKeepAlive=true
MySQL tcpKeepAlive=true
MSSQL See Connection recovery documentation.
Oracle oracle.net.keepAlive=true

On Linux

This is done by editing the /etc/sysctl.conf file:

# detect dead connections after 70 seconds
net.ipv4.tcp_keepalive_time = 60
net.ipv4.tcp_keepalive_intvl = 5
net.ipv4.tcp_keepalive_probes = 3

To activate the settings without rebooting the machine, run sysctl -p.

On newer MacOS versions (tested on 13)

Create the file /Library/LaunchDaemons/sysctl.plist:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTDs/PropertyList-1.0.dtd'>
<plist version='1.0'>
<dict>
	<key>Label</key>
	<string>sysctl</string>
	<key>Program</key>
	<string>/usr/sbin/sysctl</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/sbin/sysctl</string>
		<string>net.inet.tcp.keepidle=60000</string>
		<string>net.inet.tcp.keepintvl=5000</string>
		<string>inet.inet.tcp.keepcnt=3</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
</dict>
</plist>

You will have to reboot to activate the changes.

On older MacOS versions

Edit /etc/sysctl.conf:

# detect dead connections after 70 seconds
net.inet.tcp.keepidle = 60000
net.inet.tcp.keepintvl = 5000
net.inet.tcp.keepcnt = 3

You will have to reboot to activate the changes.

On Windows

Change the TCP keepalive settings by adding these registry keys:

HKEY_LOCAL_MACHINE/System/CurrentControlSet/Services/Tcpip/Parameters/KeepAliveTime
HKEY_LOCAL_MACHINE/System/CurrentControlSet/Services/Tcpip/Parameters/KeepAliveInterval

The registry keys must be of type DWORD and the values are in milliseconds rather than in seconds.

After changing these keys, restart Windows to activate them.