I’m running DRF as my backend, connecting to Postgres. I’m running out of DB connections at times (with not much traffic) and I’m trying to optimize settings. Some advice that I’ve gotten:
CONN_HEALTH_CHECKS=True
in Django settings - I can’t acutally do this one yet, as I’m not on Django 4.1 yetI’m having trouble with the first item. Some tutorials (https://docs.digitalocean.com/developer-center/deploy-a-django-app-on-app-platform/#step-4-deploying-to-digitalocean-with-app-platform) show a run
command like gunicorn --worker-tmp-dir /dev/shm django_app.wsgi
, and I could add the worker count option here, but gunicorn isn’t found during the deployment, so now I’m dubious that it’s even being used.
My primary question is how to set the number of workers for gunicorn on Django with App Platform, but if you have other advice for not running out of DB connections with Django, I’m allllll ears!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Enter your email to get $200 in credit for your first 60 days with DigitalOcean.
New accounts only. By submitting your email you agree to our Privacy Policy.
Hi there,
I believe that when using the DigitalOcean’s App Platform with Django, it automatically uses
gunicorn
as the application server if you haven’t specified a custom start command. However, you have full control over the configuration by using the “Run Command” setting in the App Platform.To set the number of workers you can update the “Run Command” setting. Here, you can specify your custom
gunicorn
command:You can adjust the
--workers
number as required.If you are still getting the error that
gunicorn
is not found, could you share yourrequirements.txt
file here?Regarding your database connections issue, in your Django settings, you can use persistent database connections. This can help reduce the overhead of establishing a new connection every time one is requested:
The
CONN_MAX_AGE
setting specifies the lifetime of a database connection in seconds. Setting it to a positive value like300
(5 minutes) allows Django to use a persistent connection for that duration.Also, if you’re experiencing high connection loads, consider using a connection pooler like
pgbouncer
:Also keep in mind that the number of
gunicorn
workers is not directly related to the number of database connections. However, each worker can handle a request, and if each request results in a new database connection, they can quickly add up, especially if you’re not using persistent connections or a connection pooler.Let me know how it goes!
Best,
Bobby