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?

39 Upvotes

48 comments sorted by

View all comments

Show parent comment

1

u/fykup Oct 19 '26

Yes, that's actually exactly what the experiment tests. I used HikariCP, not a custom pool, and compared its default configuration against idle connection retirement using minimumIdle=1 and idleTimeout=10s.

The custom pool was part of my previous professional experience, not something I built or proposed for this experiment.

The article includes all three configurations, measurements, and reproduction code.

1

u/gnahraf Oct 19 '26

Sorry for my confusion. I read your post, not the article. My bad.

Anyway, thanks for reminding that the idleTimeout setting (or whatever it is in other pools one uses) might need to be adjusted from the default. Your data shows its impactful.

Another thing that might be with exploring is the effect of random skews involving which app instance the load balancer picks. If there are only a 2 or 3 instances, one instance will occasionally have way more idle connections than the others (because, by chance, it just served a disproportionate number of requests). Meanwhile, a good number of idle jdbc connections open on the other app instances (the ones that served disproportionately fewer requests) have closed, or have "TTL"s that are about to be closed. (Probably easier to game it in python, i.e. model it, than to setup an actual test). Here, too small a TTL value might exact a bigger cost than one expects.

So we know the total no. of idle connections across all apps increases as you add more app instances. You might be able to calculate (or just assume) an average for this idle-overhead given N apps. (If you assume an average, then you're baking in some assumptions, which you may or may not know, about your traffic profile -- but that's ok.) The question is, for a given N and a given idle-connection TTL setting, how much is this total no. of idle connections expected to deviate from the average over a period T. Its variance / SD, to be specific.

1

u/fykup Oct 19 '26

Thanks, that's an interesting direction!

One thing I observed in practice was that the imbalance wasn't just caused by incoming traffic. We also had background jobs, which weren't evenly distributed across application instances. At any given moment, some nodes needed considerably fewer database connections than others.

That's actually where the optimization came from. Allowing idle connections to retire was a relatively simple way to reclaim resources without redesigning load balancing or background job distribution, which would have been a much larger undertaking with uncertain benefits.

I agree that modeling the variance across instances could be interesting, especially with different traffic patterns and idle timeout settings.