Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django persistent database connection

I'm using django with apache and mod_wsgi and PostgreSQL (all on same host), and I need to handle a lot of simple dynamic page requests (hundreds per second). I faced with problem that the bottleneck is that a django don't have persistent database connection and reconnects on each requests (that takes near 5ms). While doing a benchmark I got that with persistent connection I can handle near 500 r/s while without I get only 50 r/s.

Anyone have any advice? How can I modify Django to use a persistent connection or speed up the connection from Python to DB?

like image 680
HardQuestions Avatar asked Jul 14 '09 13:07

HardQuestions


People also ask

How does Django handle database connections?

Connection managementDjango opens a connection to the database when it first makes a database query. It keeps this connection open and reuses it in subsequent requests. Django closes the connection once it exceeds the maximum age defined by CONN_MAX_AGE or when it isn't usable any longer.


Video Answer


1 Answers

Django 1.6 has added persistent connections support (link to doc for latest stable Django ):

Persistent connections avoid the overhead of re-establishing a connection to the database in each request. They’re controlled by the CONN_MAX_AGE parameter which defines the maximum lifetime of a connection. It can be set independently for each database.

like image 145
Cesar Canassa Avatar answered Oct 01 '22 16:10

Cesar Canassa