m0p1
m0p1

Reputation: 43

What is a database Connection Limit and how does ActiveRecord handle it?

What is a database "Connection Limit" as seen here:

https://addons.heroku.com/marketplace/heroku-postgresql

If I have a ruby app that queries the database and 100 requests are made at the same time how does the database Connection Limit come into play?

Are 80 requests rejected? Queued?

Is each query a separate Connection or are multiple queries handled by a single connection?

I am using ActiveRecord. Thanks in advance!

Upvotes: 2

Views: 1524

Answers (2)

user944938
user944938

Reputation: 1020

Active Record uses connection pooling mechanism via threads to manage database connections.A pool of database connections is created and then shared among the applications that need to access the database. When an application needs database access, it requests a connection from the pool. When it is finished, it returns the connection to the pool, where it becomes available for use by other applications or requests. Active records implements via connectionpool class and it is thread safe. As per your example if the number of connection execeed and the pool is full, they will be queued and timeout will be set. After the request is completed other requests are processed. By default, connection pool is set to 5 and timeout is 5 sec.

Upvotes: 2

catsby
catsby

Reputation: 11342

Connection limit here means the maximum number of backend connections you can open against the database.

In your example, the count depends on the concurrency of your application. If your application is single threaded and you're on 1 dyno, then those requests will be processed sequentially and you'll likely only use 1 database connection.

If you're using something like Unicorn with say 4 workers, that's approximately 4 database connections.

This DevCenter expands on connections:

Upvotes: 1

Related Questions