A while back, I wrote a little rails app for Thai food ordering at my work. My colleagues place orders using the system and then bring money to the nominated orderer of the week. Once all orders are in and paid for (this is also tracked in the app), the orderer rings up our favourite Thai restaurant (Laddas) and places the order.
I have the app running from fairly cheap shared hosting. At peak times during the ordering, I guess that they’d be 15 or so people simultaneously using the app. We’ve used it many times without problems. Thus, I was quite surprised and displeased (as were my colleagues), when my hosting account was suspended and we couldn’t see what had been ordered this morning. A hasty email to my hosting provider revealed that my account had been suspended due to high load and “ruby flooding”. They were kind enough to un-suspend my account and we completed the ordering process.
I remembered seeing something about production mode in ‘environment.rb‘. Some googling confirmed my hunch – in development mode, rails apps are much more resource intensive. Caching is not used, and every single file needs to be reloaded every time it is required. After changing my app to production mode, it seemed to run noticeably faster. Michael and I ran ‘top’ and it looked like each request used less CPU.
So, should you be in a similar situation, this is how to change your app to production mode on fast-cgi Apache shared hosting:
- Confirm that ‘database.yml‘ in your app’s ‘config‘ directory has a section for production mode, and that it has up to date database connection details.
- Edit ‘environment.rb‘ in your app’s ‘config‘ directory.
- Add this line:
ENV['RAILS_ENV'] ||= 'production'
- Run ‘ps -u [your_user_name]’ to find if you have any ‘dispatch.fgi‘ processes running.
- If so, kill all of them (they’ll restart and use your new config settings).
- Browse to your app, it should now run faster.