# Peak database connections

**URL:** <https://forum.djangoproject.com/t/peak-database-connections/39681>\
**Category:** Deployment\
**Created:** [March 19, 2025, 3:12am UTC](https://forum.djangoproject.com/t/peak-database-connections/39681 "2025-03-19T03:12:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dauid64](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dauid64/32/17084_2.png) [@dauid64](https://forum.djangoproject.com/u/dauid64)\
**Post date:** [March 19, 2025, 3:12am UTC](https://forum.djangoproject.com/t/peak-database-connections/39681/1 "2025-03-19T03:12:02Z")

</div>

I deployed my Django application on GCP with Cloud Run and Cloud SQL and I’m spending a lot of money on Cloud SQL, because I have to hire 2 vCPUs because of the connection limit that has to be greater than 100, but it’s not even using 10% of the memory and CPU. I tried to configure Django’s default connection pool, but I still have a peak of 149 connections per day.

```auto
DATABASES['default']['OPTIONS'] = {
    'pool': {
        'min_size': 5,
        'max_size': 20
    }
}

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/4/2/425519295fd8b6caaf21525e476d63e2f275a353.png)

---

<div class="post-metadata">

**Author:** ![tomusher](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tomusher/32/18846_2.png) [@tomusher](https://forum.djangoproject.com/u/tomusher)\
**Post date:** [March 19, 2025, 10:24am UTC](https://forum.djangoproject.com/t/peak-database-connections/39681/2 "2025-03-19T10:24:03Z")

</div>

Cloud Run introduces quite a few variables that make this a challenge to debug without knowing more about your setup.

Do you have a value other than `0` set for `CONN_MAX_AGE`? If so, it’s possible that Cloud Run is killing your instances before Django cleans up these connections, so they’re staying open (potentially indefinitely). Generally, it’s safer to run with a `CONN_MAX_AGE` of `0` on Cloud Run.

Alternatively, it could be that you’re receiving that many requests all at once and Cloud Run is configured with a low ‘Maximum concurrent requests per instance’, so each request is creating a new Cloud Run instance, which doesn’t use your pool/existing sessions.

One approach you can use to figure out what’s going on is to run `SELECT * FROM pg_stat_activity;` on your database instance and check for ‘idle’ connections. If these were all created a while ago, it’s likely the first issue. If they all have similar timestamp, then it could be the second issue.

---

<div class="post-metadata">

**Author:** ![dauid64](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dauid64/32/17084_2.png) [@dauid64](https://forum.djangoproject.com/u/dauid64)\
**Post date:** [March 19, 2025, 11:14am UTC](https://forum.djangoproject.com/t/peak-database-connections/39681/3 "2025-03-19T11:14:52Z")

</div>

My configuration in the Django settings database is just that, so yes, the `CONN_MAX_AGE` variable is already set to 0 by default.

I ran the query you indicated and this was the result:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/a/0/a086c95fcbcfcd3367e08a0ef4d142b145a425ac.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/7/8/78e48bf6576962aa7f4a9aa2bd0a5a238445b971.png)

I have a value of 80 in Cloud run for ‘Maximum concurrent requests per instance’.

---

<div class="post-metadata">

**Author:** ![tomusher](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tomusher/32/18846_2.png) [@tomusher](https://forum.djangoproject.com/u/tomusher)\
**Post date:** [March 19, 2025, 11:30am UTC](https://forum.djangoproject.com/t/peak-database-connections/39681/4 "2025-03-19T11:30:59Z")

</div>

Great OK, another couple of things to check then:

Cloud SQL should have an ‘Active Connections’ graph. Does this spike at certain points or stay steady?

Your Cloud Run Metrics has a ’ Container instance count’ graph - what does this peak at? Similarly, if you’re OK sharing it, what does your ‘Request count’ graph look like?

---

<div class="post-metadata">

**Author:** ![dauid64](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dauid64/32/17084_2.png) [@dauid64](https://forum.djangoproject.com/u/dauid64)\
**Post date:** [March 19, 2025, 11:53am UTC](https://forum.djangoproject.com/t/peak-database-connections/39681/5 "2025-03-19T11:53:30Z")

</div>

“Active Connections” graph:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/7/d/7dc419d48bd024a0fd18eaa37b9afa55c66117c6.png)

“Container instance count” graph

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/5/c/5ceeb8878e6f8adef9e9a3f44461027ab9834271.png)

“Request Count” graph:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/b/7/b72b62c2507d41f6dfc20d48ebe009b150efa195.png)

“Maximum concurrent requests” graph

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/6/8/6803a1e70a19535ea307984489904e841f8638d7.png)

Dashboard DB 7 days ago:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/a/d/ad2ea375283ff80bc1b4610f5700472d42c94dd6.png)

---

<div class="post-metadata">

**Author:** ![dauid64](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dauid64/32/17084_2.png) [@dauid64](https://forum.djangoproject.com/u/dauid64)\
**Post date:** [March 19, 2025, 11:55am UTC](https://forum.djangoproject.com/t/peak-database-connections/39681/6 "2025-03-19T11:55:24Z")

</div>

Could it be the case that I remove the connection pool configuration from my Django? Is it really necessary in this case?

---

<div class="post-metadata">

**Author:** ![tomusher](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tomusher/32/18846_2.png) [@tomusher](https://forum.djangoproject.com/u/tomusher)\
**Post date:** [March 19, 2025, 1:08pm UTC](https://forum.djangoproject.com/t/peak-database-connections/39681/7 "2025-03-19T13:08:49Z")

</div>

Your pool probably isn’t helping much in this case - as configured it will try to establish and hold open 5 connections to your database per worker. This means when your instance count spikes to ~20, you also see your connections spike.

This pool is only really useful if you intend to keep the same instance around for a while and intend to serve many requests from it (by setting ‘minimum instances’ in Cloud Run for example).

Looking at your request count and instance count graphs - it seems like Cloud Run is starting new instances for many of your requests, so it could be that even though a single instance is configured to handle 80 requests, it’s hitting the [60% CPU usage target](https://cloud.google.com/run/docs/about-instance-autoscaling) and scaling anyway.

You could consider:

- Increasing the size and workers of the Cloud Run instances created so that a single instance can handle more traffic and therefore more efficiently use the pool.
- Moving pool management outside of the application/Cloud Run by running [pgbouncer](https://www.pgbouncer.org) somewhere else, e.g. in Compute Engine, or by using a database provider that supports pgbouncer (like [Crunchy Bridge](https://docs.crunchybridge.com/how-to/pgbouncer).

---

<div class="post-metadata">

**Author:** ![dauid64](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dauid64/32/17084_2.png) [@dauid64](https://forum.djangoproject.com/u/dauid64)\
**Post date:** [March 19, 2025, 1:13pm UTC](https://forum.djangoproject.com/t/peak-database-connections/39681/8 "2025-03-19T13:13:34Z")

</div>

Thank you very much for your help!

I will first try to use pgbouncer to manage the connection pool through Compute Engine.

---

<div class="post-metadata">

**Author:** ![dauid64](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dauid64/32/17084_2.png) [@dauid64](https://forum.djangoproject.com/u/dauid64)\
**Post date:** [March 19, 2025, 7:07pm UTC](https://forum.djangoproject.com/t/peak-database-connections/39681/9 "2025-03-19T19:07:27Z")

</div>

I’ve been trying all day to implement pgbouncer, but GCP makes it very difficult, without success. I think I’ll choose to increase the resources of my Cloud Run containers. But I found this Google Cloud situation to be somewhat problematic.
