Question

Configuring gunicorn workers on App Platform with Django

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:

  • configure gunicorn to 3-4 workers
  • use persistent connections (Django setting)
  • Use CONN_HEALTH_CHECKS=True in Django settings - I can’t acutally do this one yet, as I’m not on Django 4.1 yet

I’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!


Submit an answer


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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
September 8, 2023

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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel