OperationalError: sorry, too many clients already

Nope, I haven’t set that up yet. I have been trying everything else first because I believe that using pgbouncer would just mask the underlying cause.

Looking at the logs in postgres, it looks like the issue first appeared after switching from gunicorn to Daphne, on a day with a lot of traffic.

Now that I’ve gotten rid of channels, I think the next move could be to try and go back to gunicorn.

If that doesn’t work either, the last resort would be to use conn max age 0 and pgbouncer.

Looking at my output from pg_stats, is there anything that jumps out to you? I have yet to understand how to interpret the values in the query column. To my understanding, they are the last query made by the connection before going idle?

The conversation from the other thread made it clear that you need to set conn_max_age = 0. I’m not sure why you think it’s just going to “mask the underlying cause”.

Using pgbouncer will reduce (eliminate?) the latency involved in opening the connection each time.

My understanding is that I need to do that if I’m using channels, right?

But under “normal” use, i.e. no channels, I would expect Django to properly close old connections.

Because I eliminated every usage of Channels, which allegedly was the culprit as it wasn’t closing the connections, and the issue still happened; therefore I assumed that if my app had some connection leak somewhere, using pgbouncer would only delay the issue.

Anyway, I switched back from daphne to gunicorn. We’ll see how it goes tomorrow with traffic.

You are still using Daphne. I don’t believe Channels, by itself, is the root cause. My impression from the other thread is that it’s a Daphne issue at its root.

Well, after one day of using gunicorn with 5 workers, even while having slightly less than a hundred simultaneous users all sending concurrent requests, the number of connections never seemed to surpass 58.

I guess Daphne really was the cause of this.

To be accurate, it’s using Daphne with a conn_max_age > 0 that is the cause. Daphne with conn_max_age = 0 shouldn’t exhibit this behavior.

1 Like