سؤال

Recently I have started getting mySQL "too many connection" errors at times of high traffic. My rails app runs on a mongrel cluster with 2 instances on a shared host. Some recent changes that might be driving it:

  • Traffic to my site has increased. I am now averaging about 4K pages a day.
  • Database size has increased. My largest table has ~ 100K rows. Some associations could return several hundred instances in the worst case, though most are far less.
  • I have added some features that increased the number and size of database calls in some actions.

I have done a code review to reduce database calls, optimize SQL queries, add missing indexes, and use :include for eager loading. However, many of my methods still make 5-10 separate SQL calls. Most of my actions have a response time of around 100ms, but one of my most common actions averages 300-400ms, and some actions randomly peak at over 1000ms.

The logs are of little help, as the errors seem to occur randomly, or at least the pattern does not appear related to the actions being called or data being accessed.

Could I alleviate the error by adding additional mongrel instances? Or are the mySQL connections limited by the server, and thus unrelated to the number of processes I divide my traffic across?

Is this most likely a problem with my coding, or should I be pressing my host for more capacity/less load on the shared server?

هل كانت مفيدة؟

المحلول

Are you caching anything? It's an important part of alleviating application and database load. The Rails Guides have a section on caching.

نصائح أخرى

ActiveRecord has pooled database connections since Rails 2.2, and it's likely that that's what's causing your excess connections here. Try turning down the value of pool in your database.yml for that environment (it defaults to 5).

Docs can be found here.

Something is wrong. A Mongrel instance processes 1 request at a time so if you have 2 Mongrel instances then you should not be seeing more than 2 active MySQL connections (from the mongrels at least)

You could log or graph the output of SHOW STATUS LIKE 'Threads_connected' over time.

PS: this is not very many Mongrels. if you want to be able to service more than 2 simultaneous requests then you'll want more. ...if memory is tight, you can switch to Phusion Passenger and REE.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top