简体   繁体   English

并发数据库连接Heroku Unicorn rails app

[英]Concurrency database connections Heroku Unicorn rails app

I'm running a rails app on Heroku. 我在Heroku上运行一个rails应用程序。

I have one dyno. 我有一个dyno。 I'm using the Hobby Basic database, the $9 a month one, with a connection limit of 20. 我正在使用Hobby Basic数据库,每月9美元,连接限制为20。

My app is running on Unicorn. 我的应用程序在Unicorn上运行。 But it's still really slow when multiple database calls are being made. 但是,当进行多个数据库调用时,它仍然非常慢。

This is what I have in my unicorn.rb file: 这是我在unicorn.rb文件中的内容:

# config/unicorn.rb
worker_processes 3
timeout 30
preload_app true

before_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

This article talks about managing the concurrent connections: https://devcenter.heroku.com/articles/concurrency-and-database-connections 本文讨论管理并发连接: https//devcenter.heroku.com/articles/concurrency-and-database-connections

But, i'm still confused. 但是,我仍然感到困惑。 With my current setup, how do I allow for multiple database connections at the same time? 使用我当前的设置,如何同时允许多个数据库连接? And at the maximum connections my database allows (20)? 在我的数据库允许的最大连接数(20)? I would really appreciate it if someone here that has dealt with scaling a rails app on Heroku could point me in the right direction. 如果有人在Heroku上处理扩展rails应用程序的某些人可能会指出我正确的方向,我将非常感激。

Its fair to make an assumption that the database could be a bottleneck when you use the limited hobby plan. 当您使用有限的业余爱好计划时,假设数据库可能成为瓶颈是公平的。 There are likely some optimizations you can do to help improve connection pooling regardless ( like code below). 您可以采取一些优化措施来帮助改善连接池(如下面的代码所示)。 However when this is truly the bottleneck, you would expect to find timeouts occuring often, not just slowness. 然而,当这确实是瓶颈时,你会发现经常发现超时,而不仅仅是缓慢。 In order to get a better sense of where your issues lie, I recommend adding the NewRelic plugin next. 为了更好地了解问题所在,我建议您接下来添加NewRelic插件。

The NewRelic Stark plan (recommended) - or the Wayne plan ( a bit too limited in my opinion) is free and will help visualize you Apps performance and trace Errors. NewRelic Stark计划(推荐) - 或者Wayne计划(在我看来有点太有限)是免费的,可以帮助您查看应用程序性能和跟踪错误。 You will likely find that dyno queuing time and memory consumption is contributing to the slowness you are getting, and there are a few ways this could improve without ever messing with database connections. 您可能会发现dyno排队时间和内存消耗导致您获得的速度变慢,并且有一些方法可以在不弄乱数据库连接的情况下提高。 Improving your code response time and limiting your database hits (via caching) are good areas to investigate. 改善代码响应时间并限制数据库命中(通过缓存)是需要研究的好方法。

With that said, I do recommend adding the following code to your unicorn file as described in the article you mention. 话虽如此,我建议您将以下代码添加到您的unicorn文件中,如您提到的文章中所述。

after_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
  end

  # other settings
  if defined?(ActiveRecord::Base)
    config = ActiveRecord::Base.configurations[Rails.env] ||
            Rails.application.config.database_configuration[Rails.env]
    config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
    config['pool']            =   ENV['DB_POOL'] || 2
    ActiveRecord::Base.establish_connection(config)
  end
end

Heroku provides managed Postgres databases. Heroku提供托管的Postgres数据库。 Different tiered databases have different connection limits. 不同的分层数据库具有不同的连接限制。 The Starter Tier “Dev” and “Basic” databases are limited to 20 connections. Starter Tier“Dev”和“Basic”数据库限制为20个连接。

and one connect required by on worker. 和一个工人所需的连接。 if you have workers more than 20, you can use connection pool like pgbouncer deal the problem. 如果你有超过20的工人,你可以像pgbouncer一样使用连接池处理问题。 if you use pgbouncer , you shold use transaction pool mode in your environment so server can reused between workers. 如果您使用pgbouncer,则在您的环境中使用事务池模式,以便服务器可以在工作程序之间重用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM