What does database connection limit mean?

Issue

I’m using Django with a Postgres database from heroku.
The heroku database has a

“connection limit of 20”.

I don’t understand what this means.

How is a connection defined? Every time a user visits my site I will get some stuff of the database. Is this already a connection because my website needs to connect to the database to get this data?

And what happens if the total number of connections is reached? Does the website still work?

Solution

Every query you execute, makes a connection to database.

For example if you want to get some data out of your database, this is what happens in general:

1 – You execute your query in django level.

2 – Django will translate your query to SQL.

3 – Django creates a connection to your database.

4 – Your query will be executed in database.

5 – Django will receive the result.

6 – Django will close the connection.

That limitation means that you can have only 20 tasks working on step 3 to 6 at the same time.

And if you exceed this limitation, your application will throw an error or your application tries till it can make the connection (there will be limit for number of tries and you will get an error if you exceed the tries too) which leads your application to get slower or break on some requests.

You either have to upgrade your server/get more servers or you have to optimize your code to makes fewer queries (Combining queries for example).

Answered By – Navid Zarepak

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published