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

evanfarrellPRO

a year ago

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

0 Replies

evanfarrellPRO

a year ago

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


a year 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?


evanfarrellPRO

a year ago

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


evanfarrellPRO

a year ago

can start there


a year 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


evanfarrellPRO

a year ago

should be good then


evanfarrellPRO

a year ago

what needs to change on the django side?


a year 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?


evanfarrellPRO

a year ago

correct


a year ago

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


evanfarrellPRO

a year 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


a year ago

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


evanfarrellPRO

a year ago

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


a year 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


evanfarrellPRO

a year ago

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

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


a year ago

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


evanfarrellPRO

a year ago

got it, ty


a year ago

happy to help!


evanfarrellPRO

a year ago

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


a year ago

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