r/java • • Oct 15 '26

Does shrinking a JDBC connection pool actually save PostgreSQL memory? I measured it.

At a previous company, I spent quite a bit of time tuning a custom-built JDBC connection pool. One of the most effective memory optimizations I found was automatically shrinking the pool during idle periods. PostgreSQL uses a process-per-connection model, so keeping unnecessary connections open has a real memory cost.

I wanted to revisit this with HikariCP and measure both the memory savings and the cost of reopening connections.

I ran two Spring Boot instances sharing PostgreSQL 16, comparing three pool configurations under the same burst → idle → burst workload.

Results:

  • Default pool: 20 connections remained open, PostgreSQL memory ~57.5 MiB.
  • Shrinking pool: connections dropped to 2, PostgreSQL memory ~28.5 MiB.
  • Savings: ~29 MiB of PostgreSQL memory.

The tradeoff was connection reopening. Median acquisition time increased from under 3 ms to around 200 ms when traffic returned.

One interesting detail: setting minimumIdle=1 alone wasn't enough when idle retirement was disabled. The pool still held all 20 connections after the burst.

The experiment used a deliberately short idle timeout to make the behavior observable, not as a production recommendation.

With many application instances maintaining independent connection pools, the potential savings can add up.

To clarify the practical motivation: this was a deliberately small experiment to isolate the effect, not just an exercise in saving 29 MiB on one server.

At my previous company, we had many application VMs connecting to a shared PostgreSQL database. Shrinking idle pools across those instances produced substantial aggregate memory savings. For example, 30 instances with 32 connections each means 960 persistent connections. If idle retirement brings that down to an average of 16 per instance, that's 480 fewer PostgreSQL connections consuming resources.

Actual savings depend on the workload, but this is where the approach becomes particularly valuable.

Full article, measurements, and reproducible experiment:

https://pvrlabs.xyz/articles/spring-boot-jdbc-pool-reclaim.html

Do you use fixed-size pools in production, or allow idle connections to retire?

38 Upvotes

48 comments sorted by

View all comments

Show parent comment

1

u/Objective-Ninja-5211 Oct 16 '26

While that's nice, and I didn't miss your roundabout "yes", can you honestly say your post or article come anywhere close or are in any way related to real world optimization - the non-trivial production you mention?

You're showing that having to acquire new connections takes less time than reusing pooled ones, and that maintaining connections takes resources.

While it's great to have some exact numbers to compare, I really don't think this is of any help to anyone working on optimizing massive systems - as I'd assume they would be familiar with the concept of connection pools.

The underlying problem is not trivial, no one said it is, but your article really doesn't have anything to do with real world engineering problems.

The only reason I mention this in the first place is because you keep mentioning them while the article is more or less dealing with the first few pages of the Hikari and PG docs.

Calling implementing PgBouncer a simplistic solution but speaking as if reducing the maintained connection count is some bespoke optimization is a bit strange, but maybe that's just me.

2

u/fykup Oct 16 '26

Speaking in your terms, I have nothing against PgBouncer, and I never said PgBouncer itself was a simplistic solution. What's simplistic is recommending it without first understanding the application's connection management.

What I've seen in the industry is that plenty of engineers don't even know how to configure connection pools properly. Of course their half-assed applications can benefit from PgBouncer's connection sharing.

But before introducing another component, perhaps it's worth understanding why your application is maintaining hundreds of unnecessary connections in the first place.

And no, I never claimed idle connection reclamation was some groundbreaking invention. I measured a practical optimization that I've seen deliver substantial savings in production. You seem determined to argue against claims I never made.