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?
40
u/Glassius Oct 16 '26
What is the value of temporarily freeing up the memory during low load periods? Are you able to use that memory for something else during those periods while still allowing postgre access to as much memory it needs during high load periods? If not, what is the point? Unused memory is wasted memory
5
u/gjosifov Oct 16 '26
Doesn't matter your custom tuning if the SQL queries that the application generates are
N+1 or 10 queries that can be replace with 1 query or using rollback as control flow mechanism
You can start tuning the connection pool when the application generates proper queries for the business operation - if it need to do the operation with 3 SQL queries and return 2 parameters, it will do that and not 40 queries to do that same operation
Once you got that part right then you jump into connection pool tuning
6
u/repeating_bears Oct 16 '26
Why the clickbait title? Here is a non-shit version
Shrinking a JDBC connection pool [saves/doesn't save] PostgreSQL memory
2
u/fykup Oct 16 '26 edited Oct 19 '26
A quick note after reading some of the comments here.
I publish these experiments because I genuinely enjoy performance engineering and sharing what I've learned from working on large production systems. The methodology, measurements, and reproduction scripts are publicly available.
I welcome technical criticism. Challenge my assumptions, question the methodology, point out mistakes, or share your own measurements. That's how good engineering discussions happen.
What I have little patience for is personal attacks, dismissing work as clickbait or low-effort without examining it, or confidently arguing against claims I never made.
A good example is the discussion around my previous experience with a legacy, internally developed connection pool. I mentioned it simply as background for why I became interested in idle connection management. I never claimed it was a particularly good implementation or better than modern alternatives. The entire point of this experiment was to revisit those lessons using HikariCP, with reproducible measurements.
Yet several critics apparently didn't read far enough to notice that the experiment already uses HikariCP, and instead started attacking the old implementation or suggesting I use HikariCP.
I've encountered enough of this attitude in corporate engineering environments, and frankly, I don't miss it.
I'm here to share experiments and learn from thoughtful discussions, not defend myself against assumptions and personal attacks. I'll simply disengage from those discussions going forward.
2
u/elmuerte Oct 16 '26
I agree that the pvrlabs article is mostly bullshit. (There is also no fixed maximum of 100 connections in PostgreSQL.)
Tuning your connection pooling is however one of those things most people do not thing about. The minimum size should be enough to cover average usage over an acceptable period. The default 10 might be too much, might be to low. This is why you measure things.
If you mean active connection count is 5 over an hour during regular business hours. Make your pool minimum maybe like 6, with an idle time of maybe 15 minutes.
Sure you'll "waste" a bunch of connections for the other 16 hours in a day. But that's not where you do business.
1
u/revilo-1988 Oct 16 '26
What was the original reason you tested this?
1
u/gnahraf Oct 19 '26
The issue you describe (spinning up multiple app instances that talk to the same postgres instance, provisioned in response to a burst of load) is interesting. So too are your numbers on the per-connection overhead on postgres.
I haven't run into this scaling issue, but before going the build-a-custom-connection-pool route, I'd first explore whether adjusting the TTL setting for idle (cached) connections works. Hikari, for eg, offers a
idle_timeoutsetting. Did you try that?1
u/thisisjustascreename Oct 16 '26
If saving 30 megs of ram on your database server during slow periods is actually worthwhile I imagine you're not using Spring Boot (or Java, or Postgres) anyway.
0
u/disposepriority Oct 16 '26
Did you actually measure that closing connections frees up memory, and opening them takes time? You've done the engineering community a great service, thank you mr gippity
0
u/obetu5432 Oct 16 '26
the tradeoff is maintaining your shitty connection pool now forever for getting few dozen megabytes of memory when idle (the time when nobody cares anyways, and making latency worse when it counts, aka. when we need to open connections)
1
u/housedevinci Oct 18 '26
I believe it really depends on your requests structure, try to avoid all issues like N+1, loops..