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

db is locking due to too many connections. do I have options within railway to scale?
Solution:
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. ...
Jump to solution
19 Replies
Percy
Percy3mo ago
Project ID: bb751571-dd10-43ee-8a83-386587f4256b
Ev from Spicerack
bb751571-dd10-43ee-8a83-386587f4256b
Brody
Brody3mo 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?
Ev from Spicerack
unclear. any tricks to setting up the railway pgbouncer template? can start there
Brody
Brody3mo 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
Ev from Spicerack
should be good then what needs to change on the django side?
Brody
Brody3mo 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?
Ev from Spicerack
correct
Brody
Brody3mo ago
do you have an idea of why you are hitting the 100 connection limit? how large are your pools? are you running replicas?
Ev from Spicerack
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
Brody
Brody3mo ago
oh then maybe your app is opening a database connection for each request? and you arent using a client pool at all?
Ev from Spicerack
I suppose not - I'm just using whatever is batteries included with django. So perhaps we're just opening too many connections
Solution
Brody
Brody3mo 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
Ev from Spicerack
then i would work on getting an in client database pool going
how would I go about doing this / where can I learn?
Brody
Brody3mo ago
i cant see there not being django documentation for this, their docs are pretty good imo
Ev from Spicerack
got it, ty
Brody
Brody3mo ago
happy to help!
Ev from Spicerack
is there a way to connect to a postgres instance directly through railway or does it need to happen via command line?
Brody
Brody3mo ago
use the DATABASE_URL variable's value from the databases service variables
Want results from more Discord servers?
Add your server
More Posts