Published on
Sunday, January 1, 2023

Database connection in Django

395 words2 min read
  • avatar
    Viet Anh

By default, Django re-establish a new connection to the database in each request. That will be overhead and increase the request time, especially when your database is not on the same server/cluster as your backend server. Fortunately, Django provides us “persistent connection” option to keep and reuse the database connection. A connection pool can also be set up through middle-wares.

1. Setup persistent connections

The persistent connection can be enabled in Django by setting CONN_MAX_AGE to a positive number (the timeout of the database connection). The default value is 0, meaning that the database connection is established on a new request and closed right after the request finishes. For unlimited persistent connections, set this value to None.

Setting CONN_MAX_AGE to a high value does not mean that the connection to the database will never close. There are other timeouts in the specific DBMS you are using.

Django 4.1 also added CONN_HEALTH_CHECKS option. Setting this value to ****True******** will check the connection before using, preventing the closed connection to be used in the request.

Since each thread maintains its own connection, you may need to care about the number of connections to your database if your limit of connections is low (for example, when you are using the Heroku database service).

2. Setup database connection pool

The pooling mechanism will help you to keep a number of connections (pool size) for shared use in your system. This can be set up by using pgbounder (your application makes requests to pgbounder instead of the actual database. This will be the middleman between your Django application and the database.

Another alternative to pgbounder is a Django middleware, for example, django-db-connection-pool or django-postgrespool2. That kind of middleware can be set up in Django settings, for example, with django-db-connection-pool:

    'default': {
        'ENGINE': 'dj_db_conn_pool.backends.postgresql',
				'POOL_OPTIONS' : {
            'POOL_SIZE': 10,
            'MAX_OVERFLOW': 10,
            'RECYCLE': 24 * 60 * 60