Django persistent database connection


Question

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 to modify django to use persistent connection? Or speed up connection from python to DB

Thanks in advance.

1
54
7/14/2009 1:47:00 PM

Accepted Answer

Django 1.6 has added persistent connections support (link to doc for django 1.9):

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.

28
4/8/2016 8:03:40 AM

Try PgBouncer - a lightweight connection pooler for PostgreSQL. Features:

  • Several levels of brutality when rotating connections:
    • Session pooling
    • Transaction pooling
    • Statement pooling
  • Low memory requirements (2k per connection by default).

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon