我正在运行一个多tennant网站,在那里我想减少为每个请求创建一个PostgreSQL连接的开销。Django的CONN_MAX_AGE允许这样做,代价是为PostgreSQL创建了大量打开的空闲连接(8个工作线程* 20个线程= 160个连接)。对于每个连接10MB的情况,这会消耗大量内存。
主要目的是减少连接时间开销。因此我的问题是:
Django 1.6设置:
DATABASES['default'] = {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
....
'PORT': '6432'
'OPTIONS': {'autocommit': True,},
'CONN_MAX_AGE': 300,
}
ATOMIC_REQUESTS = False # default
Postgres:
max_connections = 100
PgBouncer:
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
发布于 2016-02-18 13:18:30
这是我用过的一个设置。
在同一台机器上运行的保镖,如黑角,芹菜等。
pgbouncer.ini:
[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:
"<dbuser>" "<dbpassword>"
Django settings.py:
...
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
使用transaction
或statement
)。当Django尝试重用它的持久连接时,pgbouncer负责等待到Postgres的可用连接。
https://stackoverflow.com/questions/27418264
复制相似问题