# Fixing Database Connections in Django

DevFeed: [Fixing Database Connections in Django](<https://devfeed.tech/articles/fixing-database-connections-in-django-41129.md>)

Original publisher: [Read original article](<https://www.craigkerstiens.com/2013/03/07/Fixing-django-db-connections/>)

Author: Map

Published: 2013-03-07T20:55:56Z

Content type: tutorial

Language: en

Sources: [Craig Kerstiens](<https://devfeed.tech/sources/craig-kerstiens.md>)

Topics: [Django](<https://devfeed.tech/topics/django.md>), [Database](<https://devfeed.tech/topics/database.md>), [PostgreSQL](<https://devfeed.tech/topics/postgresql.md>), [Cloud](<https://devfeed.tech/topics/cloud.md>), [SSL](<https://devfeed.tech/topics/ssl.md>), [Heroku](<https://devfeed.tech/topics/heroku.md>)

Tags: [cloud](<https://devfeed.tech/tags/cloud.md>), [connection-pooling](<https://devfeed.tech/tags/connection-pooling.md>), [database](<https://devfeed.tech/tags/database.md>), [django](<https://devfeed.tech/tags/django.md>), [heroku](<https://devfeed.tech/tags/heroku.md>), [performance](<https://devfeed.tech/tags/performance.md>), [postgresql](<https://devfeed.tech/tags/postgresql.md>), [python](<https://devfeed.tech/tags/python.md>), [ssl](<https://devfeed.tech/tags/ssl.md>)

## AI overview

The article explains that Django's per-request database connections can add substantial latency, especially in cloud environments where SSL negotiation contributes to connection time. It presents connection pooling and persistent connections as ways to reduce that overhead and discusses Django packages that support pooling.

## Source excerpt

If you're looking to get better performance from your Django apps you can check out Pro Django, PostgreSQL High Performance, or read some my earlier posts on Postgres Performance. All of these are of course good things to do - you can also start by correcting an incredibly common but also painful performance issue, that until 1.6 is unaddressed in Django. Django's current default behavior is to establish a connection for each request within a Django application. In many cases any particularly in distributed cloud environments this is a large time sink of your response time. An example application running on Heroku shows a typical connection time of 70ms. A large part of this time is the SSL negotiation that occurs in connecting to your database, which is a good practice to ensure security of your data. Regardless, this is a long time in simply establishing a connection. As a point of comparisson its commonly encourage that most queries to your database are under 10ms. An example that highlights this in a small lightweight application shows the bulk of a request time being within a connection displayed by New Relic: One option to remedy this is by running a connection pooler on your Database side such as Pgpool or PgBouncer. In fact Ask the Pony already highlighted these potential gains. While running an external DB they're essentially testing the benefits of conncetion pooling. This is an obvious gain and can be in a much more lightweight format. Connection Pooling in Django As Django establishes a connection on each request it has an opportunity to both pool connections and persist connections. There are two major options for pooling, each works quite well with Django and provides some dramatic improvements. While the first request may take the 70ms of connection time, subsequent requests show absolutely no connection time since the connection already exists. This is highlighed by these two comparissons of before and after in actually the times it grabs a connectio