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!
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:
gunicorn --worker-tmp-dir /dev/shm --workers 4 django_app.wsgi
You can adjust the --workers number as required.
If you are still getting the error that gunicorn is not found, could you share your requirements.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:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
...
'CONN_MAX_AGE': 300,
}
}
The CONN_MAX_AGE setting specifies the lifetime of a database connection in seconds. Setting it to a positive value like 300 (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:
https://docs.digitalocean.com/products/databases/postgresql/how-to/manage-connection-pools/
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.