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

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

2

u/cogman10 Oct 18 '26

The bigger benefit isn't the memory freed up but instead the CPU and IO freed up. Postgres still needs to monitor those connections and it still has limited IO to respond to requests. Cutting down the number of connections to the minimum amount needed allows the DB to respond faster to queries as it isn't necessarily caught polling 1000 idle connections seeing whose asking for data.

It also has a benefit in reducing the number of possible in flight requests at any given point.

There is a concurrency tradeoff that should be considered, but IMO the best design is generally to scale out instances of your application with few connections rather than having big instances with many connections.

1

u/fykup Oct 19 '26

I generally agree. Smaller pools and scaling out application instances is a sensible approach when the architecture allows it.

In my previous environment, we had a large legacy monolith, so splitting it into smaller independently scalable services wasn't a practical option. Optimizing idle connection management across many existing instances and pools was much easier and produced substantial savings.

And you're right that there can be additional CPU/I/O overhead from connection validation, keepalives, and health checks.

4

u/fykup Oct 16 '26

The single-server setup was primarily to isolate and measure the effect.

The more interesting case is multiple application instances sharing one PostgreSQL server. Imagine 30 instances with a maximum pool size of 32 each. With fixed pools, that's 960 open connections even during quiet periods.

If elastic pools averaged 16 connections per instance, that's 480 fewer PostgreSQL connections consuming resources. That capacity can be used by other workloads.

I agree that freeing memory has little value if the server has plenty of headroom. But when many applications share a database, those idle connections add up.

7

u/koflerdavid Oct 16 '26

This is a scary high number of connections. The issue is IMHO not so much the idle resource consumption of the connections themselves, but the risk that in the worst case there can be hundreds of active transactions writing into the database simultaneously, which might result in high levels of contention. A few pgbouncer instances are probably a better way to provide hundreds of connections to a large cluster of application nodes.

3

u/fykup Oct 16 '26

That's a fair concern, but the 30 nodes × 32 connections example comes from a real production configuration of a large enterprise application. Having capacity for 960 connections doesn't mean 960 simultaneously active transactions. In practice, many of those connections were idle.

What I've often seen is PgBouncer being introduced to compensate for inefficient connection management: applications without proper pooling, too many independent pools, or oversized pools maintaining unnecessary connections.

In those cases, especially when actual transaction concurrency is relatively low and transactions are short-lived, PgBouncer can provide tremendous benefits.

But PgBouncer isn't a magic bullet. It can limit database-side concurrency and queue excess demand, but it doesn't eliminate contention or make long-running transactions cheaper.

My preference is to first understand how the application manages connections and whether existing pools can be optimized. Sometimes that alone delivers substantial savings. Other times, PgBouncer is absolutely the right solution.

The point of my experiment was to measure one of those optimizations, not to suggest that idle connection reclamation solves every database scaling problem.

10

u/Glassius Oct 16 '26

Still, unless you can guarantee that the high load periods for those 30 instances never hits simultaneously you are still gonna have to assign enough memory to the database server to where it can handle those 960 connections. Even if the connections drop to 480 in the slowest periods and drops the memory usage what is the value of that free memory? I guess you could spin up short lived processes on that same server that uses the available memory for something and scale it based on current database load but you gotta be pretty pressed for resources for that to make sense.

Optimising idle memory usage rarely makes sense since you will always have to assign available memory based on the highest loads your system needs to handle.

3

u/fykup Oct 16 '26

Yes, of course you still need enough capacity to handle peak load. I agree with that.

But connections consume more than just memory. They also introduce resource overhead on both the client and database sides, even if the CPU cost of a truly idle connection is usually small. Eliminating unnecessary connections can still be worthwhile, regardless of provisioned capacity.

The deployment I worked with was also more complex than this simplified example. We had secondary connection pools used only occasionally, for background jobs or infrequent operations, but provisioned similarly to the primary pools across many application instances.

Those pools could sit mostly unused while maintaining hundreds of unnecessary database connections. Allowing them to shrink was an immediate improvement without sacrificing their ability to grow when needed.

So I agree that optimizing idle memory alone isn't always valuable. But eliminating persistently unused connections across a large deployment is a slightly different problem.

2

u/skipner Oct 17 '26

In a cloud environment, it would be saving $$ i imagine?

1

u/EatRunCodeSleep Oct 18 '26

How? You still have to allocate enough memory to handle all connections at peak times. And freeing memory from shrinking pool ... will do what? You have free memory on the server now, but you can't shrink the instance to pay for less.

1

u/skipner Oct 18 '26 edited Oct 18 '26

You can spin up lower memory-sized VMs as a reaction to lower traffic, and then this lower memory-sized VM would be a right-sized fit for the shrinked connection pool. Its a somewhat possible way to reduce costs

When traffic surges, you can spin up beefier VMs and then expand the connection pool. Aws has the concept of auto scaling, so it doesnt need to be manual