What is a good value for CONN_MAX_AGE in Django?

DjangoDatabase ConnectionDjango 1.6

Django Problem Overview


Django 1.6 now supports CONN_MAX_AGE to pool database connections.

By default, the value is 0 (no pooling). What is a sensible value for this option?

Django Solutions


Solution 1 - Django

This value depends on the traffic of your site, the more traffic the more seconds to retain the connection, I'd recommend setting a relatively small value like 60 and tuning it accordingly to the usage pattern.



Edit (2018):

Like @jcyrss pointed out, this method has its quirks, for future reference I'd recommend handing out pooling to something like pgbouncer instead.

Solution 2 - Django

Not as simple as "the more traffic the more seconds to retain the connection".

It also depend on how you run Django.

Now, one popular way to launch Django in gunicorn+greenlet(evenlet or gevent). And if you set CONN_MAX_AGE to 60 (even 5 in my case), you may get complain 'too many connections' from DB server.

See this for details.

https://github.com/benoitc/gunicorn/issues/996

https://serverfault.com/questions/635100/django-conn-max-age-persists-connections-but-doesnt-reuse-them-with-postgresq

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrendan NeeView Question on Stackoverflow
Solution 1 - DjangoMGPView Answer on Stackoverflow
Solution 2 - DjangoJcyrssView Answer on Stackoverflow