We're getting too many database connections and need to scale - what options are available?
evanfarrell
PROOP

2 years ago

db is locking due to too many connections. do I have options within railway to scale?

20 Replies

evanfarrell
PROOP

2 years ago

bb751571-dd10-43ee-8a83-386587f4256b


2 years ago

yes you can increase the connection limit, i think postgres has a default of 100 iirc, but counter question, do you need more conncetions or should you add in a connection pooler like pgbouncer?


evanfarrell
PROOP

2 years ago

unclear. any tricks to setting up the railway pgbouncer template?


evanfarrell
PROOP

2 years ago

can start there


2 years ago

my pgbouncer template assumes you already have a database service in the project thats named Postgres and as long as you have that it should not need any initial setup from a perspective of deploying the template


evanfarrell
PROOP

2 years ago

should be good then


evanfarrell
PROOP

2 years ago

what needs to change on the django side?


2 years ago

well lets slow down a tad here, i had assumed you where connecting to the database from a serverless environment and that was causing you to exhaust the 100 connections, but this is just a django app on railway?


evanfarrell
PROOP

2 years ago

correct


2 years ago

do you have an idea of why you are hitting the 100 connection limit? how large are your pools? are you running replicas?


evanfarrell
PROOP

2 years ago

No replicas, not sure how large pools would be (if they exist at all). Default django app with default postgres instance, both on railway. We had sub-100 but close to that concurrent users and starting getting the connections dropped


2 years ago

oh then maybe your app is opening a database connection for each request? and you arent using a client pool at all?


evanfarrell
PROOP

2 years ago

I suppose not - I'm just using whatever is batteries included with django. So perhaps we're just opening too many connections


2 years ago

okay well then pgbouncer isnt needed in this context.

as a temporary fix you can increase the limit by running ALTER SYSTEM SET max_connections = 200; then restart the deployment.
run SHOW MAX_CONNECTIONS; to confirm the new limit after the redeploy.

then i would work on getting an in client database pool going


evanfarrell
PROOP

2 years ago

then i would work on getting an in client database pool going

how would I go about doing this / where can I learn?


2 years ago

i cant see there not being django documentation for this, their docs are pretty good imo


evanfarrell
PROOP

2 years ago

got it, ty


2 years ago

happy to help!


evanfarrell
PROOP

2 years ago

is there a way to connect to a postgres instance directly through railway or does it need to happen via command line?


2 years ago

use the DATABASE_URL variable's value from the databases service variables


Loading...