Add native PgBouncer support to alleviate DB connection limit errors

This commit is contained in:
Keannu Bernasol 2024-09-05 13:16:23 +08:00
parent 24233506c4
commit b664cb33c4
4 changed files with 48 additions and 11 deletions

View file

@ -27,7 +27,10 @@ DB_PASSWORD = ''
DB_HOST = 'postgres' DB_HOST = 'postgres'
DB_PORT = '5432' DB_PORT = '5432'
DB_SSL_MODE = 'disable' DB_SSL_MODE = 'disable'
DB_DISABLE_SERVER_SIDE_CURSORS = 'False' # If you're using an external connection bouncer (eg. PgBouncer), set this to True # DB connection bouncer
USE_BOUNCER = 'True'
DB_BOUNCER_HOST = 'pgbouncer'
DB_BOUNCER_PORT = '6432'
# Redis # Redis
# Used for DB cache and Celery broker # Used for DB cache and Celery broker

View file

@ -223,16 +223,26 @@ SPECTACULAR_SETTINGS = {
WSGI_APPLICATION = 'config.wsgi.application' WSGI_APPLICATION = 'config.wsgi.application'
# If you're using an external connection bouncer (eg. PgBouncer), server side cursors must be disabled to avoid any issues
USE_BOUNCER = get_secret("USE_BOUNCER")
if USE_BOUNCER:
DISABLE_SERVER_SIDE_CURSORS = True
DB_HOST = get_secret("DB_BOUNCER_HOST")
DB_PORT = get_secret("DB_BOUNCER_PORT")
else:
DISABLE_SERVER_SIDE_CURSORS = False
DB_HOST = get_secret("DB_HOST")
DB_PORT = get_secret("DB_PORT")
DATABASES = { DATABASES = {
"default": { "default": {
"ENGINE": "django.db.backends.postgresql", "ENGINE": "django.db.backends.postgresql",
# Set to True to avoid issues with pgbouncer when pooling connections 'DISABLE_SERVER_SIDE_CURSORS': DISABLE_SERVER_SIDE_CURSORS,
'DISABLE_SERVER_SIDE_CURSORS': get_secret("DB_DISABLE_SERVER_SIDE_CURSORS"),
"NAME": get_secret("DB_DATABASE"), "NAME": get_secret("DB_DATABASE"),
"USER": get_secret("DB_USERNAME"), "USER": get_secret("DB_USERNAME"),
"PASSWORD": get_secret("DB_PASSWORD"), "PASSWORD": get_secret("DB_PASSWORD"),
"HOST": get_secret("DB_HOST"), "HOST": DB_HOST,
"PORT": get_secret("DB_PORT"), "PORT": DB_PORT,
"OPTIONS": { "OPTIONS": {
"sslmode": get_secret("DB_SSL_MODE") "sslmode": get_secret("DB_SSL_MODE")
}, },

View file

@ -51,11 +51,20 @@ services:
volumes: volumes:
- db-data:/var/lib/postgresql/data - db-data:/var/lib/postgresql/data
# DB Bouncer
pgbouncer:
image: bitnami/pgbouncer:latest
environment:
- POSTGRESQL_HOST=postgres
- PGBOUNCER_DATABASE=${DB_DATABASE}
- POSTGRESQL_USERNAME=${DB_USERNAME}
- POSTGRESQL_PASSWORD=${DB_PASSWORD}
depends_on:
- postgres
# Redis Server # Redis Server
redis: redis:
image: redis:latest image: redis:latest
ports:
- "${REDIS_PORT}:6379"
# Stripe CLI Webhook Listener # Stripe CLI Webhook Listener
stripe-listener: stripe-listener:

View file

@ -17,6 +17,8 @@ services:
- .:/code - .:/code
depends_on: depends_on:
- postgres - postgres
- pgbouncer
- redis
# Django Celery Worker # Django Celery Worker
celery: celery:
@ -32,6 +34,7 @@ services:
depends_on: depends_on:
- django - django
- postgres - postgres
- pgbouncer
- redis - redis
# Runs multiple worker instances # Runs multiple worker instances
scale: 4 scale: 4
@ -45,9 +48,10 @@ services:
volumes: volumes:
- .:/code - .:/code
depends_on: depends_on:
- celery
- django - django
- celery
- postgres - postgres
- pgbouncer
- redis - redis
# Django Celery Monitor # Django Celery Monitor
@ -62,8 +66,10 @@ services:
volumes: volumes:
- .:/code - .:/code
depends_on: depends_on:
- celery
- django - django
- celery
- postgres
- pgbouncer
- redis - redis
# SQL Database # SQL Database
@ -77,11 +83,20 @@ services:
volumes: volumes:
- db-data:/var/lib/postgresql/data - db-data:/var/lib/postgresql/data
# DB Bouncer
pgbouncer:
image: bitnami/pgbouncer:latest
environment:
- POSTGRESQL_HOST=postgres
- PGBOUNCER_DATABASE=${DB_DATABASE}
- POSTGRESQL_USERNAME=${DB_USERNAME}
- POSTGRESQL_PASSWORD=${DB_PASSWORD}
depends_on:
- postgres
# Redis Server # Redis Server
redis: redis:
image: redis:latest image: redis:latest
ports:
- "${REDIS_PORT}:6379"
# Stripe CLI Webhook Listener # Stripe CLI Webhook Listener
stripe-listener: stripe-listener: