Video in progress
What are `maxLifetime`, `idleTimeout` and `keepaliveTime` in HikariCP for, and what errors do wrong values cause?
maxLifetime replaces a connection after a set time, idleTimeout closes surplus idle connections, and keepaliveTime periodically checks an idle connection. If maxLifetime isn't smaller than the timeout of the network devices or the database itself, the application gets a dead connection.
📖 Answer
A common symptom on prod: the application sits quiet overnight, and in the morning the first requests fail with Connection reset or An I/O error occurred while sending to the backend. The cause is a firewall, NAT or load balancer that silently cut off a TCP connection that sat idle for 30-60 minutes, and Hikari doesn't know about it.
The settings:
spring:
datasource:
hikari:
maximum-pool-size: 10
minimum-idle: 10 # by default equal to max, so idleTimeout doesn't apply
max-lifetime: 1500000 # 25 minutes; a few minutes below the network/DB limit
keepalive-time: 300000 # check an idle connection every 5 minutes
idle-timeout: 600000 # only meaningful when minimum-idle < max
Subtle points:
maxLifetimeis 30 minutes by default. Hikari doesn't close the connections all at once but with a slightly random offset, so they don't all get reopened at the same moment.keepaliveTimemust be smaller thanmaxLifetime, and its minimum value is 30 seconds.idleTimeoutonly works whenminimumIdle < maximumPoolSize. The Hikari authors recommend a fixed-size pool, so I usually set the two equal.- If you're behind PgBouncer, also take its
server_idle_timeoutandclient_idle_timeoutvalues into account.
A very small maxLifetime (say, 1 minute) is bad too: connections get reopened often, with TLS and authentication each time, and a new process in PostgreSQL.
Sign up to mark studied questions and save your progress.