首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Django的CONN_MAX_AGE设置pgbouncer的理想设置

使用Django的CONN_MAX_AGE设置pgbouncer的理想设置
EN

Stack Overflow用户
提问于 2014-12-11 16:23:45
回答 1查看 6.6K关注 0票数 25

我正在运行一个多tennant网站,在那里我想减少为每个请求创建一个PostgreSQL连接的开销。Django的CONN_MAX_AGE允许这样做,代价是为PostgreSQL创建了大量打开的空闲连接(8个工作线程* 20个线程= 160个连接)。对于每个连接10MB的情况,这会消耗大量内存。

主要目的是减少连接时间开销。因此我的问题是:

Django 1.6设置:

代码语言:javascript
复制
DATABASES['default'] = {
    'ENGINE':   'django.db.backends.postgresql_psycopg2',

     ....

    'PORT': '6432'
    'OPTIONS': {'autocommit': True,},
    'CONN_MAX_AGE': 300,
}

ATOMIC_REQUESTS = False   # default

Postgres:

代码语言:javascript
复制
max_connections = 100

PgBouncer:

代码语言:javascript
复制
pool_mode = session     # Can this be transaction?
max_client_conn = 400   # Should this match postgres max_connections?
default_pool_size = 20
reserve_pool_size = 5
EN

回答 1

Stack Overflow用户

发布于 2016-02-18 13:18:30

这是我用过的一个设置。

在同一台机器上运行的保镖,如黑角,芹菜等。

pgbouncer.ini:

代码语言:javascript
复制
[databases]
<dbname> = host=<dbhost> port=<dbport> dbname=<dbname>

[pgbouncer]
: your app will need filesystem permissions to this unix socket
unix_socket_dir = /var/run/postgresql
; you'll need to configure this file with username/password pairs you plan on
; connecting with.
auth_file = /etc/pgbouncer/userlist.txt

; "session" resulted in atrocious performance for us. I think
; "statement" prevents transactions from working.
pool_mode = transaction

; you'll probably want to change default_pool_size. take the max number of
; connections for your postgresql server, and divide that by the number of
; pgbouncer instances that will be conecting to it, then subtract a few
; connections so you can still connect to PG as an admin if something goes wrong.
; you may then need to adjust min_pool_size and reserve_pool_size accordingly.
default_pool_size = 50
min_pool_size = 10
reserve_pool_size = 10
reserve_pool_timeout = 2
; I was using gunicorn + eventlet, which is why this is so high. It
; needs to be high enough to accommodate all the persistent connections we're
; going to allow from Django & other apps.
max_client_conn = 1000
...

/etc/pgbouncer/userlist.txt:

代码语言:javascript
复制
"<dbuser>" "<dbpassword>"

Django settings.py:

代码语言:javascript
复制
...
DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgresql_psycopg2',
        'NAME': '<dbname>',
        'USER': '<dbuser>',
        'PASSWORD': '<dbpassword>',
        'HOST': '/var/run/postgresql',
        'PORT': '',
        'CONN_MAX_AGE': None,  # Set to None for persistent connections
    }
}
...

如果我没记错的话,基本上可以有任意数量的“持久”连接到pgbouncer,因为pgbouncer会在Django完成后将服务器连接释放回池中(只要您对pool_mode使用transactionstatement )。当Django尝试重用它的持久连接时,pgbouncer负责等待到Postgres的可用连接。

票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27418264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档